属性“x”不是实体类型“y”的导航属性

我正在使用EF Core和ASP Core 2.0。 使用最新的Identity框架。 我在页面All上得到了这个例外。

InvalidOperationException:属性“User”不是实体类型“Gallery”的导航属性。 ‘Include(string)’方法只能与”一起使用。 分隔的导航属性名称列表。

ApplicationUser看起来像:

public class ApplicationUser : IdentityUser { public ICollection 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 PhotosList { get { return Photos?.Split('|').ToList(); } set { Photos = string.Join("|", value); } } } 

Controller for View看起来像:

 public async Task All() { var databaseContext = db.Galleries.Include(x => x.Article).Include(x => x.User); return View(await databaseContext.ToListAsync()); } 

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

数据库是最新的。

添加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; } 

另外,请确保您的dbContextinheritance自IdentityDbContext