使用实体框架,我只想包含第一个子对象,而不要包括child(sub的sub)的子对象 [英] Use Entity framework I want to include only first children objects and not child of child(sub of sub)

本文介绍了使用实体框架,我只想包含第一个子对象,而不要包括child(sub的sub)的子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用实体框架,我只想包括子对象的第一级对象,而不要包括子对象的子对象

Useing Entity framework I want to include an only the first level of children objects and not the children of child

我有这两节课:

public class BusinessesTBL
{
    public string ID { get; set; }
    public string FirstName { get; set; }
    public string lastName { get; set; }

    public ICollection<OffersTBL> OffersTBLs { get; set; }
}

public class OffersTBL 
{
    public int ID { get; set; }
    public string Name { get; set; }

    public int CatId { get; set; }

    public string BusinessesTBLID { get; set; }
    public virtual BusinessesTBL BusinessesTBLs { get; set; }
}

当我尝试根据CatId字段提供所有报价时,我还需要返回BusinessesTBL,但是该方法还会针对每个BusinessesTBL obj再次返回报价,我的代码是:

when I try to bring all offers according to CatId field, I need to return the BusinessesTBLs also, but the method also return offers again per each BusinessesTBL obj , My code is :

public IQueryable<OffersTBL> GetOffersTBLsCat(int id)
{
    db.OffersTBLs.Include(s => s.BusinessesTBLs);
}

您可能会在上看到错误的结果: http://mycustom.azurewebsites.net/api/OffersApi/GetOffersTBLsCat/4

You can see the wrong result on : http://mycustom.azurewebsites.net/api/OffersApi/GetOffersTBLsCat/4

如您所见,它返回每个业务对象下的所有报价,而每个报价下的业务对象,并且我只想返回带有业务对象的报价,而没有业务对象下的报价.

As you can see it return all offers under each Business object while business object under each offer, And I want only to return offers with its Business object without offer under Business obj.

有人可以帮忙吗?

推荐答案

之所以会发生这种情况,是因为Entity Framework执行关系修正,这是当所属对象存在时自动填充导航属性的过程.存在于上下文中.因此,即使禁用了延迟加载,使用循环引用也可以无休止地挖掘导航属性. Json序列化程序确实做到了这一点(但是显然,它被指示处理循环引用,因此它不会陷入无限循环中).

This happens because Entity Framework performs relationship fixup, which is the process that auto-populates navigation properties when the objects that belong there are present in the context. So with a circular references you could drill down navigation properties endlessly even when lazy loading is disabled. The Json serializer does exactly that (but apparently it's instructed to deal with circular references, so it isn't trapped in an endless loop).

诀窍是防止关系修正受到限制.关系修正依赖于上下文的ChangeTracker,该上下文缓存对象以跟踪其更改和关联.但是,如果没有什么可追踪的,那就没有什么要修复的了.您可以通过调用AsNoTracking():

The trick is to prevent relationship fixup from ever happing. Relationship fixup relies on the context's ChangeTracker, which caches objects to track their changes and associations. But if there's nothing to be tracked, there's nothing to fixup. You can stop tracking by calling AsNoTracking():

db.OffersTBLs.Include(s => s.BusinessesTBLs)
             .AsNoTracking()

如果除此之外,您还通过设置contextConfiguration.LazyLoadingEnabled = false禁用了上下文的延迟加载,您将看到Json字符串中仅填充了OffersTBL.BusinessesTBLs,而BusinessesTBL.OffersTBLs是空数组.

If besides that you also disable lazy loading on the context (by setting contextConfiguration.LazyLoadingEnabled = false) you will see that only OffersTBL.BusinessesTBLs are populated in the Json string and that BusinessesTBL.OffersTBLs are empty arrays.

一个额外的好处是AsNoTracking()可以提高性能,因为更改跟踪器不会忙于跟踪EF实现的所有对象.实际上,您应该始终在断开连接的设置中使用它.

A bonus is that AsNoTracking() increases performance, because the change tracker isn't busy tracking all objects EF materializes. In fact, you should always use it in a disconnected setting.

这篇关于使用实体框架,我只想包含第一个子对象,而不要包括child(sub的sub)的子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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