EF 中的延迟加载不起作用
Lazy loading in EF does not work
我又得到了以下两个 类,它们是由我的测试模型生成的:
public partial class House
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public House()
{
this.Persons = new HashSet<Person>();
}
public int id { get; set; }
public string street { get; set; }
public string city { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Person> Persons { get; set; }
}
public partial class Person
{
public int id { get; set; }
public string namen { get; set; }
public int house { get; set; }
public virtual House House1 { get; set; }
}
在我的 DBContext 的构造函数中,我明确启用了延迟加载,这甚至不是必需的:
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = true;
稍后我调用一个房子列表:
YardEntities cx = new YardEntities();
IList<House> hl = cx.Houses.ToList<House>();
House h = hl[0];
///**************BREAKPOINT*******************///
Person g = h.Persons.First<Person>();
output = g.namen;
在我预期的标记断点之后,由于延迟加载,此时所有与房屋相关的人都未加载。我没有访问一个人,为什么要加载他们?但是它们被加载了,因为我的调试器向我显示了所有名称属性。
有人可以解释这种行为吗?为什么延迟加载不起作用?
I did not access a single person so why should they be loaded?
你是对的。延迟加载不会加载任何关系,除非你想访问它们。
However they are loaded, because my debugger shows me all the name attributes.
实际上你的调试器想要访问它们,所以延迟加载将完成工作并从数据库中获取它们。
如果我不使用 tolist():
DbSet<House> hl = cx.Houses;
House h = hl.FirstOrDefault<House>();
h 包含所有人员,即使启用了延迟加载。我想阿文斯的回答一定是真的。当我想看到它们时,调试器会加载所有值。
但是有办法证明他的猜测吗?
我又得到了以下两个 类,它们是由我的测试模型生成的:
public partial class House
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public House()
{
this.Persons = new HashSet<Person>();
}
public int id { get; set; }
public string street { get; set; }
public string city { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Person> Persons { get; set; }
}
public partial class Person
{
public int id { get; set; }
public string namen { get; set; }
public int house { get; set; }
public virtual House House1 { get; set; }
}
在我的 DBContext 的构造函数中,我明确启用了延迟加载,这甚至不是必需的:
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = true;
稍后我调用一个房子列表:
YardEntities cx = new YardEntities();
IList<House> hl = cx.Houses.ToList<House>();
House h = hl[0];
///**************BREAKPOINT*******************///
Person g = h.Persons.First<Person>();
output = g.namen;
在我预期的标记断点之后,由于延迟加载,此时所有与房屋相关的人都未加载。我没有访问一个人,为什么要加载他们?但是它们被加载了,因为我的调试器向我显示了所有名称属性。
有人可以解释这种行为吗?为什么延迟加载不起作用?
I did not access a single person so why should they be loaded?
你是对的。延迟加载不会加载任何关系,除非你想访问它们。
However they are loaded, because my debugger shows me all the name attributes.
实际上你的调试器想要访问它们,所以延迟加载将完成工作并从数据库中获取它们。
如果我不使用 tolist():
DbSet<House> hl = cx.Houses;
House h = hl.FirstOrDefault<House>();
h 包含所有人员,即使启用了延迟加载。我想阿文斯的回答一定是真的。当我想看到它们时,调试器会加载所有值。 但是有办法证明他的猜测吗?