简体   繁体   中英

Entity Framework Core 5 dynamic table query

With previous versions of Entity Framework, you could write a query with just a table name like this:

Type t = Type.GetType(Assembly.GetExecutingAssembly().GetName().Name + "." + "TableName");
DbSet dbset = dbcontext.Set(t);

Now with EF Core 5 and dbContext.Set<TEntity>() this is not working. How can I do the same in EF Core 5?

Not sure why you code like that as you could use other shortcut way.

dbContext.Set<TEntity>() is still working in EFCore 5.

Use like

public void YourFunction<T>() where T : class
{
    DbSet dbset = dbcontext.Set<T>();
}

or

public class YourClass<T> where T : class
{
    public void YourFunction()
    {
        DbSet dbset = dbcontext.Set<T>();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM