Entity Framework Eager Download downloads everything

We use the Entity Framework + Repository template in the web application to retrieve the database. Because of our complex business, our models sometimes become complex, and this causes strange behavior in the Entity Framework bootable system.

Imagine our real model. We have tables, boxes on the table, pencil cases that can be on the table or in the box, and pencils that can be on the table or in the box or in the pencil case. We applied this in our application as follows.

public class Table
{
    public int TableID{ get; set; }
    public virtual ICollection<Box> Boxes{ get; set; }
    public virtual ICollection<PencilCases> PencilCases{ get; set; }
    public virtual ICollection<Pencils> Pencils{ get; set; }
}

public class Box
{
    public int BoxID{ get; set; }
    public int TableID{ get; set; }
    [ForeignKey("TableID")]
    public virtual Table Table{ get; set; }
    public virtual ICollection<PencilCases> PencilCases{ get; set; }
    public virtual ICollection<Pencils> Pencils{ get; set; }
}

public class PencilCases
{
    public int PencilCaseID{ get; set; }
    public int? BoxID{ get; set; }
    public int TableID{ get; set; }
    [ForeignKey("TableID")]
    public virtual Table Table{ get; set; }
    [ForeignKey("BoxID")]
    public virtual Box Box{ get; set; }
    public virtual ICollection<Pencils> Pencils{ get; set; }
}

public class Pencils
{
    public int PencilID{ get; set; }
    public int? PencilCaseID{ get; set; }
    public int? BoxID{ get; set; }
    public int TableID{ get; set; }
    [ForeignKey("TableID")]
    public virtual Table Table{ get; set; }
    [ForeignKey("BoxID")]
    public virtual Box Box{ get; set; }
    [ForeignKey("PencilCaseID")]
    public virtual PencilCase PancelCase{ get; set; }
}

, http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application get .

var tables = unitOfWork.TableRepository.Get(includeProperties: "Boxes, PencilCases, Boxes.Pencils");

, , , , Boxes, PencilCases Boxes.Pencils, , , Pencils, PencilCases.Pencils Boxes.PencilCases.Pencils. OutOfMemoryException, .

, Entity Framework , Boxes.Pencils. , , .

+4
1

- EF, , , , 100% . , , , , .

, EF , , , , .

, Pencil Boxes.Pencils, ICollection Table.Pencils, , .

MVC-.

, , -, Data Entity, , Data Entity - , JSON/XML.

:

mapper/encoder, / :

, , / . , .

mapper/encoder,

- , Pencil, . , , stackoverflow, .

/

- , , stackoverflow. , , , , , .

-

, , - - . , - .

:

// Removed Pencils
public class BusinessTable
{
    public int TableID{ get; set; }
    public IEnumerable<Box> Boxes{ get; set; }
    public IEnumerable<PencilCases> PencilCases{ get; set; }
}

// Removed Table & PencilCases
public class BusinessBox
{
    public int BoxID{ get; set; }
    public int TableID{ get; set; }
    public IEnumerable<Pencils> Pencils{ get; set; }
}

// Removed Table & Box & Pencils
public class BusinessPencilCases
{
    public int PencilCaseID{ get; set; }
    public int? BoxID{ get; set; }
    public int TableID{ get; set; }
}

// Removed Table, Box, PencilCase
public class BusinessPencils
{
    public int PencilID{ get; set; }
    public int? PencilCaseID{ get; set; }
    public int? BoxID{ get; set; }
    public int TableID{ get; set; }
}

, -, .

theres 2: / factory Factory, ValueInjecter AutoMapper - NuGet.

AutoMapper: AutoMapper, , :

Mapper.CreateMap<Table, BusinessTable>();
Mapper.CreateMap<Box, BusinessBox>();
Mapper.CreateMap<PencilCases, BusinessPencilCases>();
Mapper.CreateMap<Pencils, BusinessPencils>();

:

var tables = unitOfWork.TableRepository.Get(includeProperties: "Boxes, PencilCases, Boxes.Pencils");
var result = Mapper.Map<IEnumerable<Table>, IEnumerable<BusinessTable>>(tables);

var tables = unitOfWork.TableRepository.Get(includeProperties: "Boxes, PencilCases, Boxes.Pencils").Project().To<IEnumerable<BusinessTable>;

, AutoMapper (, ): https://github.com/AutoMapper/AutoMapper/wiki/Getting-started

ValueInjecter:

var tables = unitOfWork.TableRepository.Get(includeProperties: "Boxes, PencilCases, Boxes.Pencils");
var result = new List<BusinessTable>().InjectFrom(tables);

:

var tables = unitOfWork.TableRepository.Get(includeProperties: "Boxes, PencilCases, Boxes.Pencils");
var result = tables.Select(x => new BusinessTable.InjectFrom(x).Cast<BusinessTable>());

ValueInjection, SmartConventionInjection, , ORM ValueInjecter.

, , Github

, MaxDepthCloneInjector ( , ) , , .

:

  • , . SO: Include
  • , , : STICK WITH EAGER LOADING, Lazy Loading N + 1. :
    • Lazy Loading, , , , .
    • Eager Loading, , , Web Api , .

, Felix

+1

Source: https://habr.com/ru/post/1540480/


All Articles