gpt4 book ai didi

c# - 属性 'x' 不是实体类型 'y' 的导航属性

转载 作者:太空狗 更新时间:2023-10-29 17:57:51 26 4
gpt4 key购买 nike

我将 EF Core 与 ASP Core 2.0 结合使用。使用最新的身份框架。我在所有页面上得到这个异常。

InvalidOperationException: The property 'User' is not a navigation property of entity type 'Gallery'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names.

ApplicationUser 看起来像:

public class ApplicationUser : IdentityUser<Guid>
{
public ICollection<Gallery> Galleries { get; set; }
}

实体库看起来像:

public class Gallery
{
public int Id { get; set; }
public Guid UserId { get; set; }
public string Title { get; set; }
public int? ArticleId { get; set; }
public string Photos { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }

public Article Article { get; set; }
public ApplicationUser User { get; set; }

[NotMapped]
public List<string> PhotosList
{
get { return Photos?.Split('|').ToList(); }
set { Photos = string.Join("|", value); }
}
}

View Controller 看起来像:

public async Task<IActionResult> All()
{
var databaseContext = db.Galleries.Include(x => x.Article).Include(x => x.User);

return View(await databaseContext.ToListAsync());
}

我不知道为什么它不会在 Article 上崩溃..

数据库是最新的。

最佳答案

添加一个ForeignKey属性

using System.ComponentModel.DataAnnotations.Schema;

...

[ForeignKey("Article")]
public int? ArticleId { get; set; }

[ForeignKey("User")]
public Guid UserId { get; set; }

你也可以把属性放在导航属性上

[ForeignKey("UserId")]
public ApplicationUser User { get; set; }

此外,请确保您的 dbContext 继承自 IdentityDbContext<ApplicationUser, ...>

关于c# - 属性 'x' 不是实体类型 'y' 的导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46838328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com