【发布时间】:2018-03-31 23:44:41
【问题描述】:
我将 EF Core 与 ASP Core 2.0 一起使用。使用最新的身份框架。我在全部页面上收到此异常。
InvalidOperationException:属性“用户”不是实体类型“画廊”的导航属性。 'Include(string)' 方法只能与 '.' 一起使用。导航属性名称的分隔列表。
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); }
}
}
视图控制器如下所示:
public async Task<IActionResult> All()
{
var databaseContext = db.Galleries.Include(x => x.Article).Include(x => x.User);
return View(await databaseContext.ToListAsync());
}
我不知道为什么它不会在文章上崩溃..
数据库是最新的。
【问题讨论】:
-
this 有帮助吗?
-
kabanus:不,外键没有帮助
-
Cristic:好吧,我没有选项 x.ApplicationUser 因为我定义了名为 User 而不是 ApplicationUser 的导航。ApplicationUser 是类而不是变量名
-
值得注意。我在我的场景中犯了一个错误。我正在使用 Database.Order.Include(o => o.ShopId) 而实际上它应该是 Database.Order.Include(o => o.Shop) - 在我的情况下实际上是一个错字 - 只要检查你是不是不小心像在 SQL 查询中一样链接到 Id。
标签: c# asp.net-core entity-framework-core