具有代理创建和延迟加载的实体框架禁用仍在加载子对象 [英] Entity Framework with Proxy Creation and Lazy Loading disabled is still loading child objects

查看:71
本文介绍了具有代理创建和延迟加载的实体框架禁用仍在加载子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用POCO在实体框架中遇到一些问题,希望有人可以在高层次上告诉我,如果我看到的行为是预期的,或者我需要深入了解它为什么发生。



我有一个类客户和另一个 CustomerType 所以客户具有一个属性类型(类型为 CustomerType 指示该类型)和 CustomerType 具有属性客户客户 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 属性默认为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 Customers (All Customers 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 Customers 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆