替代EF中的List.Include

时间:2016-09-19 13:56:40

标签: entity-framework

由于下面的EF代码,我正在进行循环引用。我有一个父表,分别有2个孩子和他们的孙子。有没有使用Include的替代方法,因为我读了一些关于Navigation属性的东西,它包含了每个子节点中的父节点,导致序列化具有循环引用问题。

错误讯息: 序列化“产品”类型的对象时检测到循环引用。

这部分只是为了获取检索而我不需要将父引用返回给每个孩子。我调试内部并在每个子内部看到它有父引用。

  var query = db.Products.Include(c => c.ProductTriggers.Select(b => b.ProductTriggerItems))
                    .Include(d => d.ProductsExtra.Select(e => e.ProductAllotments.Select(m => m.ProductAllotmentDetails))).AsNoTracking();

        return query.ToList();

产品类

 public partial class Product
{
    public Product()
    {
        this.ProductExtras = new HashSet<ProductExtra>();
        this.ProductTriggers = new HashSet<ProductTrigger>();
    }

    public int ProductID { get; set; }
    public string ProductCode { get; set; }
    public string ProductName { get; set; }

    public virtual ICollection<ProductProduct> ProductExtras { get; set; }
    public virtual ICollection<ProductTrigger> ProductTriggers { get; set; }
}

ProductExtra Class

 public partial class ProductExtra
{
    public ProductExtra()
    {
        this.ProductAllotments = new HashSet<ProductAllotment>();
    }

    public int ProductExtraID { get; set; }
    public int ProductID { get; set; }


    public virtual Product Product { get; set; }
    public virtual ICollection<ProductAllotment> ProductAllotments { get; set; }
}

ProductAllotment Class

   public partial class ProductAllotment
{
    public ProductAllotment()
    {
        this.ProductAllotmentDetails = new HashSet<ProductAllotmentDetail>();
    }

    public int ProductAllotmentID { get; set; }
    public int ProductExtraID { get; set; }


    public virtual ProductExtra ProductExtra { get; set; }
    public virtual ICollection<ProductAllotmentDetail> ProductAllotmentDetails { get; set; }
}

ProductTrigger Class

public partial class ProductTrigger
{
    public ProductTrigger()
    {
        this.AddOnTriggerItems = new HashSet<ProductTriggerItem>();
    }

    public int ProductTriggerID { get; set; }
    public int ProductID { get; set; }


    public virtual Product Product { get; set; }
    public virtual ICollection<ProductTriggerItem> ProductTriggerItems { get; set; }
}

ProductTriggerItem类

public partial class ProductTriggerItem
{
    public int ProductTriggerItemID { get; set; }
    public int ProductTriggerID { get; set; }


    public virtual ProductTrigger ProductTrigger { get; set; }
}

0 个答案:

没有答案