5

I want to get a DbSet with the class name that I have stored in a variable.

I have tried with this code:

string name = "ParosLineas";
var dbset = (System.Data.Entity.DbSet)efContext.GetType().GetProperty(name).GetValue(efContext);

dbset.AddRange(dates.[name]);
efContext.SaveChanges();

but I get this error:

System.InvalidCastException: 'Unable to cast object of type 'System.Data.Entity.DbSet1[Report.Models.ParosLinea]' to type 'System.Data.Entity.DbSet'

this is my ParosLine declaration: enter image description here

3

1 Answer 1

3

The problem is that the generic DbSet<TEntity> class does not inherit (hence cannot be cast to) the non generic DbSet.

I see two options.

If you know the namespace name of the entity class, you can use Type.GetType to get the corresponding Type which in turn can be used to call the non generic DbContext.Set method which returns a non generic DbSet object, e.g.

string nameSpace = "MyEntities";
string name = "ParosLineas";
var type = Type.GetType($"{namespace}.{name}");
var dbSet = efContext.Set(type);

Another way is to use reflection to get the DbSet<T> property as you are doung, but cast the result to IQueryable. Then you can use the IQueryable.ElementType to call the non generic Set method as above:

string name = "ParosLineas";
var type = ((IQueryable)efContext.GetType().GetProperty(name).GetValue(efContext))
    .ElementType;
var dbSet = db.Set(type);

The first method is preferable. First, because it uses less calls, and second, because does not require DbSet<T> property in the context and does not assume that DbSet<T> property name is the same as the entity class name.

4
  • 1
    I use the second option and it worked! Thanks a lot! I'm really really grateful! You are the best Aug 18, 2019 at 16:44
  • Hi @Ivan Stoev I hope you response my comment. I'm making a CRUD, that is the reason why I wanted to make a insert using reflection. Now i want to make a select with the same way. I'm trying to make something like: SELECT * FROM any Table WHRE foreign Key = any number I want to make this using linq and reflection. having the name of table that i want to manipulate in a string variable is it possible? Aug 25, 2019 at 0:17
  • Everything is possible (to some sort of extent). The good thing for querying is that all you need is IQueryable. The bad thing is that almost all out of the box LINQ methods are for the generic IQueryable<T> and not for IQueryable. So you have to use a lot of reflection and manual expression building. 3rd party packages like Microsoft.EntityFrameworkCore.DynamicLinq might help.
    – Ivan Stoev
    Aug 25, 2019 at 0:42
  • DbContext in Entity Framework Core 6.0 does not have a .Set() method that takes a type as parameter. You have to use dbContext.Find(type, id).
    – Creative
    May 12, 2022 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.