【问题标题】:Dynamic table name in Entity Framework实体框架中的动态表名
【发布时间】:2017-01-13 17:24:28
【问题描述】:

我正在使用具有数据库优先方法的实体框架。我想根据条件动态更改表名或视图名。

在这里,我使用V_OVT_VLD_340B_DNA_CLDV_OVT_B_tableV_OVT_c_table 来获取记录。

根据来源,我需要调用不同的表名并获取记录。整个代码 sn -p 都是一样的,只是表名不同。

请参考以下代码

private dOVT_OutlierViewEntities db = new dOVT_OutlierViewEntities();

if(source == "a")
{
    var result = this.db.V_OVT_VLD_340B_DNA_CLD.Where(x => x.DNA_PGM_PRTN_ID == partitionId && x.CLIENT_ID == clientId).ToList().Select(y => new ValidationModel
                {
                    claim_validation_test_id = new List<byte?> { y.CLAIM_VLD_TEST_ID },
                    claim_id = y.CLAIM_ID,
                    Provider_ID = y.Provider_ID,

                }).Take(id).ToList();
}

if(source == "b")
{
    var result = this.db.v_OVT_B_table.Where(x => x.DNA_PGM_PRTN_ID == partitionId && x.CLIENT_ID == clientId).ToList().Select(y => new ValidationModel
                    {
                        claim_validation_test_id = new List<byte?> { y.CLAIM_VLD_TEST_ID },
                        claim_id = y.CLAIM_ID,
                        Provider_ID = y.Provider_ID,

                    }).Take(id).ToList();
}

if(source == "c")
{
    var result = this.db.v_OVT_C_table.Where(x => x.DNA_PGM_PRTN_ID == partitionId && x.CLIENT_ID == clientId).ToList().Select(y => new ValidationModel
                    {
                        claim_validation_test_id = new List<byte?> { y.CLAIM_VLD_TEST_ID },
                        claim_id = y.CLAIM_ID,
                        Provider_ID = y.Provider_ID,

                    }).Take(id).ToList();

}

我想通过根据条件动态地将表名附加到数据库上下文来修改上述实现。

string tableName = string.empty

if(source == "a") 
    tableName = "aTable";

if(source == "b")  
    tableName="bTable";

this.db.tableName.where().....

这可能吗?

【问题讨论】:

    标签: c# entity-framework linq c#-4.0


    【解决方案1】:

    您可以使用切换条件来设置表格类型并将其与上下文一起使用

    switch (tableName)
            {
                case "a":
                    tableType = typeof(V_OVT_VLD_340B_DNA_CLD);
                    break;
                case "b":
                    tableType = typeof(v_OVT_B_table);
                    break;
                default:
                    tableType = typeof(v_OVT_C_table);
                    break;
            }
    
    var query = context.Set(tableType);
    var result = query.Find(); //filter with this query condition
    

    【讨论】:

      【解决方案2】:

      你可以做这样的事情..

         string tableName = string.empty
      
          if(source == "a") 
              tableName =db.GetTable("aTable");
      
          if(source == "b")  
              tableName=db.GetTable("bTable");
      
      
          and then query like..
      
           tableName.where()
      

      【讨论】:

      • GetTable() 方法的含义。我在上下文中找不到任何名为“GetTable”的方法
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多