Materializing an ICollection structure containing subclasses

后端 未结 2 359
渐次进展
渐次进展 2021-01-28 23:37

I\'m reviewing some code that was written in the EF 4 days because it stands out during performance benchmarking.

The purpose of the code is to materialize an ICol

相关标签:
2条回答
  • 2021-01-29 00:09

    It cannot be said in advance if the performance will be better (sometimes executing a single complex query, especially with sub collection includes may have actually negative impact), but you can minimize the number of database queries to K, where K is the number of subclass types that need additional includes.

    You need to base the LoadSubclasses method on IQueryable<TBase> representing all base entities, and execute one query per each subclass type using OfType filter:

    private void LoadSubclasses(IQueryable<MyBaseClass> baseQuery)
    {
        // SubA
        baseQuery.OfType<SubA>()
            .Include(x => x.Ref1.Options)
            .Load();
       // Similar for other subclasses
    }
    

    The usage with your sample would be:

    var parent = ctx.Parents.Include(p => p.Base).Where(...).Single();
    LoadSubclasses(ctx.Entry(parent).Collection(p => p.Base).Query());
    

    or more generally:

    var parentQuery = ctx.Parents.Where(...);
    var parents = parentQuery.Include(p => p.Base).ToList();
    LoadSubclasses(parentQuery.SelectMany(p => p.Base));
    
    0 讨论(0)
  • 2021-01-29 00:30

    For those on EF Core 2.1 or later, this feature is now supported out-of-the-box.

    Request from 2010:

    When in an data model for entity framework has a navigation property it is not posseble to eager load that navigation property besides when using OfType<> or when eager loading the derived type itself by a navigation property.

    Response from 2018:

    The feature is part of EF Core 2.1, which is currently in preview. Please create issues in our issue tracker if you find any problems.

    0 讨论(0)
提交回复
热议问题