3

coluld you please be so kind to tell me how do I choose DbSet depending on string variable? What I have is the following:

public class DataContext : DbContext
{
    public DataContext() : base("myDb") { }

    public DbSet<Entry> RurEntries { get; set; }
    public DbSet<Entry> UsdEntries { get; set; }
    public DbSet<Entry> EurEntries { get; set; }
}

There are 3 tables for each currency: Rur,Usd,Eur. All have same structure. There is string variable named CurrentCurrency which is changed from UI and may be one of 3 currencies. In my previous code without Entity Framework I had code that read db with pure sql, someting like:

string sqlQuery = "Select * from " + CurrentCurrency 

Now I decided to rewrite code with Entity Framework and faced that problem. Any answer will be appreciated. Thanks in advance.

3
  • 3
    Why don't you just introduce a flag to the table instead of creating three tables ? Commented Aug 15, 2016 at 10:13
  • What have you already tried and failed? Commented Aug 15, 2016 at 10:14
  • All 3 tables have unique_id field, which I receive from another software. I used unique flag on that column and it might be a problem if i put all entries to the same table Commented Aug 15, 2016 at 10:18

3 Answers 3

2

You can simply switch on your CurrentCurrency string to get set that you need:

 var db = new DataContext();
        var CurrentCurrency = "RUR";
        DbSet<Entry> set = null;
        switch (CurrentCurrency) {
            case "RUR":
                set = db.RurEntries;
            break;
            case "EUR":
                set = db.EurEntries;
            break;
            case "USD":
                set = db.UsdEntries;
            break;
            default:
                throw new Exception();
        }
        var res = set.ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Exactly what I needed. Thanks.
As @CodeCaster wrote that doesn't work as I can only use an entity class T in a DbSet<T> once per DbContext. Finally had to introduce field Currency to my class.
@Smilley I think that should work if you use inheritance and Entry would be just an abstract base class. Anyway, solution suggested by CodeCaster seems to be more straight forward.
1

You can only use an entity class T in a DbSet<T> once per DbContext. Your code won't run. See also Entity Framework 6 Creating Two table from the same entity object.

Given your comment:

All 3 tables have unique_id field, which I receive from another software. I used unique flag on that column and it might be a problem if i put all entries to the same table

You just need a composite primary key, comprised of Currency and ExternalId, as explained in Composite Key with EF 4.1 Code First:

public class Entry
{
    [Key]
    [Column(Order = 0)]
    public string Currency { get; set; }

    [Key]
    [Column(Order = 1)]
    public string ExternalId { get; set; }
}

Then you can read the "EUR" rows like this:

var eurRows = dbContext.Entries.Where(e => e.Currency == "EUR");

Comments

0

You can execute raw SQL queries in Entity Framework. Like this:

var curs = context.Database.SqlQuery<Entry>("Select * from " + CurrentCurrency").ToList();

You can also make that more neat and create a stored procedure, where you send a parameter and execute a SELECT statement based on that parameter. Then using Entity framework, you call that procedure an map the results to a List<Entry>.

However, and as I stated in the comments I do personally prefer to have only one table with a CurrenyCode column. Instead of having 3 tables with the exact same structure but with different data. Then you can query like this:

var curs = var curs = context.Currencies.Where(x=> x.CurrencyCode = CurrentCurrency)
                                        .ToList();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.