简体   繁体   English

如何在Entity Framework中使用动态DbSet?

[英]How to use dynamic DbSet in Entity Framework?

coluld you please be so kind to tell me how do I choose DbSet depending on string variable? coluld你请你好好告诉我如何根据字符串变量选择DbSet? 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. 每种货币有3个表格: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. 有一个名为CurrentCurrency的字符串变量,它从UI更改,可能是3种货币之一。 In my previous code without Entity Framework I had code that read db with pure sql, someting like: 在我以前没有实体框架的代码中,我有使用纯sql读取db的代码,有些像:

string sqlQuery = "Select * from " + CurrentCurrency 

Now I decided to rewrite code with Entity Framework and faced that problem. 现在我决定用Entity Framework重写代码并面对这个问题。 Any answer will be appreciated. 任何答案将不胜感激。 Thanks in advance. 提前致谢。

You can simply switch on your CurrentCurrency string to get set that you need: 您只需打开CurrentCurrency字符串即可获得所需的设置:

 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();

You can only use an entity class T in a DbSet<T> once per DbContext. 每个DbContext只能在DbSet<T>使用一个实体类T Your code won't run. 您的代码将无法运行。 See also Entity Framework 6 Creating Two table from the same entity object . 另请参见实体框架6从同一实体对象创建两个表

Given your comment: 鉴于你的评论:

All 3 tables have unique_id field, which I receive from another software. 所有3个表都有unique_id字段,我从另一个软件收到。 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 : 您只需要一个复合主键,由Currency和ExternalId组成,如使用EF 4.1 Code First的Composite Key中所述

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: 然后你可以读取这样的“EUR”行:

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

You can execute raw SQL queries in Entity Framework. 您可以在Entity Framework中执行原始SQL查询。 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. 您还可以使它更整洁并创建存储过程,您可以在其中发送参数并基于该参数执行SELECT语句。 Then using Entity framework, you call that procedure an map the results to a List<Entry> . 然后使用Entity框架,将调用该过程的结果映射到List<Entry>

However, and as I stated in the comments I do personally prefer to have only one table with a CurrenyCode column. 但是,正如我在评论中所说,我个人更喜欢只有一个带有CurrenyCode列的表。 Instead of having 3 tables with the exact same structure but with different data. 而不是具有完全相同的结构但具有不同数据的3个表。 Then you can query like this: 然后你可以像这样查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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