-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DbContext.Set<T> throws exception with base class as generic type definition #6013
Comments
@rowanmiller, I think all of them are related to inheritance restrictions on the model context. |
Note for triage: This is an interesting case where having non-generic Set might be useful. We should consider this and #5998 together. |
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. |
Agreed, because of how LINQ works the only option is to use |
We have a model definition that, simplified, looks like this:
We map these classes to their own table:
Which in the database gives us the tables
ConcreteA
andConcreteB
.Consider the following method:
Calling this method the following way:
results in
db.Set<T>
throwing anInvalidOperationException
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 forSet
.Further technical details
EF Core version: 1.0.0
Operating system: Windows 10
Visual Studio version: 2015 Update 3
The text was updated successfully, but these errors were encountered: