属性"x"不是实体类型"y"的导航属性 [英] The property 'x' is not a navigation property of entity type 'y'
问题描述
我将EF Core与ASP Core 2.0一起使用.使用最新的身份框架.我在全部"页面上看到此异常.
I'm using EF Core with ASP Core 2.0. Using latest Identity framework. I get this exception on page All.
InvalidOperationException:属性"User"不是实体类型"Gallery"的导航属性. "include(string)"方法只能与."一起使用.导航属性名称的分隔列表.
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看起来像:
ApplicationUser looks like:
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 for View looks like:
public async Task<IActionResult> All()
{
var databaseContext = db.Galleries.Include(x => x.Article).Include(x => x.User);
return View(await databaseContext.ToListAsync());
}
我不知道为什么它不会在Article上崩溃.
I have no idea why it dont crash on Article..
数据库是最新的.
推荐答案
添加ForeignKey
属性
using System.ComponentModel.DataAnnotations.Schema;
...
[ForeignKey("Article")]
public int? ArticleId { get; set; }
[ForeignKey("User")]
public Guid UserId { get; set; }
您也可以将属性放在导航属性上
You can also put the attribute on the navigation property
[ForeignKey("UserId")]
public ApplicationUser User { get; set; }
此外,请确保您的dbContext继承自IdentityDbContext<ApplicationUser, ...>
这篇关于属性"x"不是实体类型"y"的导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!