延迟加载是在第一次调用导航属性时从数据库加载得,延迟加载是通过代理类和虚属性实现的.
public class Princess
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Unicorn> Unicorns { get; set; }
}
将虚属性改为非虚属性就可以禁用延迟加载.
public class Princess
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Unicorn> Unicorns { get; set; }
}