Answer the question
In order to leave comments, you need to log in
How to use dynamic DbSet in Entity Framework?
Good afternoon. Can you please tell me how to select a DbSet depending on a string type variable? What is in short:
public class DataContext : DbContext
{
public DataContext() : base("myDb") { }
public DbSet<User> Users { get; set; }
public DbSet<Entry> RurEntries { get; set; }
public DbSet<Entry> UsdEntries { get; set; }
public DbSet<Entry> EurEntries { get; set; }
}
Answer the question
In order to leave comments, you need to log in
You need to define this in the OnModelCreating method of the class
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
string table = "USD" //Здесь сделать выбор строки-имени таблицы по нужному параметру
modelBuilder.Entity<Entry>().ToTable(table);
base.OnModelCreating(modelBuilder);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question