S
S
Smilley2016-08-15 12:13:13
PHP
Smilley, 2016-08-15 12:13:13

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; }
    }

There is a table for each of the currencies: RUR, USD, EUR, all tables are identical in the fields.
There is a variable of type string CurrentCurrency, which takes the value of one of the currencies.
It would be desirable that depending on CurrentCurrency one of DbSet'ov was used.
Thanks in advance for your advice.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2016-05-05
@m77x

photoshop - use filters along the path

M
Maxim Kryuchek, 2016-10-18
@KMaxI

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 question

Ask a Question

731 491 924 answers to any question