efcore icon indicating copy to clipboard operation
efcore copied to clipboard

DbContext.Set<T> throws exception with base class as generic type definition

Open razzemans opened this issue 10 years ago • 5 comments

We have a model definition that, simplified, looks like this:

public abstract class Item

public class ConcreteA : Item

public class ConcreteB : Item

We map these classes to their own table:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  // ...
  foreach (Type type in itemTypes) // contains ConcreteA and ConcreteB
    modelBuilder.Entity(type).ToTable(type.Name);
}

Which in the database gives us the tables ConcreteA and ConcreteB.

Consider the following method:

public Task<T> SaveAsync<T>(T t) where T : Item
{
  if(db.Set<T>().Any())
    // ..
}

Calling this method the following way:

Item item = new ConcreteA();
await SaveAsync(item);

results in db.Set<T> throwing an InvalidOperationException

Cannot create a DbSet for 'Item' because this type is not included in the model for the context.

I understand why this is happening. Nonetheless I think this is a common use case that should be supported. A possible solution might be a non-generic overload Set(Type type) so we could do:

db.Set(typeof(t)).Any()

which would be able to map to the correct table. There are non-generic method overloads for Add Attach Entry etcetera so I can't see why this would not be supported for Set.

Further technical details

EF Core version: 1.0.0 Operating system: Windows 10 Visual Studio version: 2015 Update 3

razzemans avatar Jul 07 '16 09:07 razzemans

Possibly related: #5813 #6001

@rowanmiller, I think all of them are related to inheritance restrictions on the model context. It's nice that the DbContext enforces some restrictions, but been able to overcome these restrictions should be possible when you know (or think so) what you are doing.

soycabanillas avatar Jul 07 '16 14:07 soycabanillas

Note for triage: This is an interesting case where having non-generic Set might be useful. We should consider this and #5998 together.

ajcvickers avatar Jul 08 '16 16:07 ajcvickers

Never mind. It doesn't really help because once you get the non-generic IQueryable you can't call Any (or anything else useful) on it without treating it as a generic and doing manual reflection calls or expression building.

var setMethod = typeof(DbContext).GetMethod(nameof(DbContext.Set)).MakeGenericMethod(t.getType());
var query = (IQueryable)setMethod.Invoke(db, null);
var results = query... // What to do here? No ToList, no Any, etc.

ajcvickers avatar Jul 08 '16 17:07 ajcvickers

Agreed, because of how LINQ works the only option is to use MakeGenericMethod to create a DbSet<T> based on the type being used.

rowanmiller avatar Jul 08 '16 18:07 rowanmiller

I had this exact same issue. It happened when I moved my abstract parent class from a different namespace to the same namespace as my model classes.

like this, it worked fine: `namespace Helpers; public abstract class Base { }

namespace Models; public class Model1 : Base { }

public class Model2: Base { }`

But when I moved the base class to the Model namespace: `namespace Models; public abstract class Base { }

public class Model1 : Base { }

public class Model2: Base { }` None of my context queries would work, because it didn't think the classes were referenced in the context. I'm not sure why it works when they are in separate namespaces, but it is another wrinkle to consider.

harko12 avatar Aug 14 '26 00:08 harko12