我记得在 EF navigation property should be virtual :
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public string Tags { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
但我看EF Core不要将其视为虚拟:
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public ICollection<Enrollment> Enrollments { get; set; }
}
是不是不需要了?
最佳答案
virtual
在 EF 中从来都不是必需的。仅当您需要延迟加载支持时才需要它。
自 Lazy loading is not yet supported by EF Core ,目前 virtual
没有特殊含义。它会在(如果)他们添加延迟加载支持(有一个 plan 用于这样做)。
更新:从 EF Core 2.1 开始,Lazy loading现在支持。但是如果你不添加 Microsoft.EntityFrameworkCore.Proxies通过UseLazyLoadingProxies
打包并启用它,原来的答案仍然适用。
但是,如果您这样做,由于在初始实现中缺少选择加入控件,事情就完全改变了 - 它需要 所有您的导航属性虚拟
。这对我来说毫无意义,在它得到修复之前你最好不要使用它。如果您确实需要延迟加载,请使用替代方案 Lazy loading without proxies方法,在这种情况下 virtual
也不重要。
关于c# - 导航属性应该是虚拟的 - 在 ef 核心中不需要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41881169/