Loading all the children entities with entity framework

前端 未结 3 2055
傲寒
傲寒 2020-12-03 05:02

I have a data model like this

\"Data

I would like to load all the related entities from a Recon

相关标签:
3条回答
  • 2020-12-03 05:33

    There are two ways to perform Eager Loading in Entity Framework:

    • ObjectQuery.Include Method
    • Explicit loading using either DbEntityEntry.Collection Method or DbEntityEntry.Reference Method along with their respective Load methods

    There are also manners to write Raw SQL Queries against database:

    • DbSet.SqlQuery Method deals with entities
    • Database.SqlQuery Method deals with arbitrary types
    • Database.ExecuteSqlCommand Method for arbitrary DDL/DML

    For the case, when you're attempting to load nearly entire database, it would be good idea to execute dedicated store procedure against it.

    0 讨论(0)
  • 2020-12-03 05:38

    Try with just .Include(r => r.ReconciliationDetails) initially. Then add the .Select() statements one-by-one. At what point does the exception reappear? The .SelectMany() call looks a bit suspicious to me!

    A second question that might help identify the problem... After you run the code that contains all the ToList() calls, is your recon entity complete? i.e. are all its navigation properties populated? This should be the case because of the automatic 'fixup' behavior of Entity Framework.

    With EF, sometimes it is more efficient to load a complex object graph with several calls rather than chained Include() calls. Check the generated SQL and see what is most efficient in your case.

    0 讨论(0)
  • 2020-12-03 05:46

    Not sure if it's to late but could you benefit from structuring your code such as

    var acctName = "someName";
    
    var detailList =  _repository.Include(e => e.JrnlEntryDetail).Filter(c => c.JrnlEntryDetail.Any(e => e.AcctName == acctName)).Get().ToList();
    
    0 讨论(0)
提交回复
热议问题