Materializing an ICollection structure containing subclasses

entity-framework entity-framework-6


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 ICollection<MyBaseClass> using Entity Framework (we're now on EF 6.1).

The code exists because references present in specific subclasses aren't materialized when retrieving

public Parent
    public virtual ICollection<MyBaseClass>() Base { get; set; }

from the database, when the actual types stored are subclasses of MyBaseClass.

Example subclass:

public SubA : MyBaseClass
    public virtual ICollection<Options> Ref1 { get; set; }

Currently, the code does something like this:

var parent = ctx.Parents.Include(p => p.Base).Where(...).Single();
private void LoadSubclasses(IEnumerable<MyBaseClass> myBase)
    foreach (var my in myBase)
        if (my is SubA)
        else... // Similar for other subclasses

Note that ICollection<MyBaseClass>() Base contains a mix of several concrete subclasses. There are generally a few hundred objects in the ICollection.

Is there a more efficient way to materialize Base?

8/21/2017 12:37:41 AM

Accepted Answer

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
        .Include(x => x.Ref1.Options)
   // 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));
8/21/2017 10:41:08 AM

Popular Answer

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.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow