具有代理创建和延迟加载的实体框架禁用仍在加载子对象 [英] Entity Framework with Proxy Creation and Lazy Loading disabled is still loading child objects
问题描述
我有一个类客户
和另一个 CustomerType
所以客户
具有一个属性类型
(类型为 CustomerType
指示该类型)和 CustomerType
具有属性客户
是客户$ c的集合$ c> s(所有
客户
s具有该类型)所以这些基本上是关联的两端的Navigation属性,导致POCO代码类似于:
public partial class Customer
{
public int Id {get;组; }
public string Name {get;组; }
public int TypeId {get;组; }
public CustomerType Type {get;组;
public partial class CustomerType
{
public CustomerType()
{
this.Customers = new HashSet< CustomerType> );
}
public int Id {get;组; }
public string TypeName {get;组; }
public virtual ICollection< Customer>客户{get;组;我已经关闭了代理创建和LazyLoading(即两个<$ c $($)
< c> DbContext.Configuration.ProxyCreationEnabled = false 和 DbContext.Configuration.LazyLoadingEnabled = false
),因为它们使序列化变得痛苦。
如果我从客户
集合中获取实例,那么 Type $它们的c $ c>属性默认为null。
但是如果我从客户
中设置了一个 .Include(Type )
不仅是加载类型
属性,而且还加载了孩子 - 即客户
s。
这是预期的吗?
Include扩展名影响运行的SQL。根据CustomerType.ParentId列,加载的CustomerTypes(凭借包含在客户查询中)将被内置到对象树中。
所以如果有些侥幸,父母和孩子都被加载到同一个查询中,孩子将被填入父母。
I'm having some issues with the Entity Framework using POCOs and I hope someone can tell me at a high level if the behaviour I'm seeing is expected or I need to dig deeper into why it's happening.
I have a class Customer
and another CustomerType
, so Customer
has a property Type
(of type CustomerType
indicating the type) and CustomerType
has property Customers
which is a collection of Customer
s (All Customer
s that have that type) So these are basically the Navigation properties on both ends of an association, resulting in POCO code something like:
public partial class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
public CustomerType Type { get; set; }
}
public partial class CustomerType
{
public CustomerType()
{
this.Customers = new HashSet<CustomerType>();
}
public int Id { get; set; }
public string TypeName { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
}
I have turned off Proxy creation and LazyLoading (i.e. both DbContext.Configuration.ProxyCreationEnabled=false
and DbContext.Configuration.LazyLoadingEnabled=false
) because they make Serialization a pain.
As expected when I get instances from the Customer
set, the Type
property on them is null by default.
But if I get instances from the Customer
set with a .Include("Type")
not only is it loading the Type
properties, but it's also loading the children - i.e. the collection of Customer
s on each of these.
Is this expected?
It is semi expected. The Include extension affects the SQL that is run. Those CustomerTypes that ARE loaded (by virtue of being included in the Customer query) will be built into the object tree according to the CustomerType.ParentId column.
So if by some fluke both a parent and a child is loaded in the same query, the child will be stuffed into the parent.
这篇关于具有代理创建和延迟加载的实体框架禁用仍在加载子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!