动态表名与实体框架 [英] Dynamic table name with entity framework

查看:107
本文介绍了动态表名与实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多具有相同模型结构的表,但其他表名与其他数据(在这种情况下将是〜100表)。我想使用Entity Framework在运行时动态切换表名(例如从路由获取名称表)。数据库中的表由其他脚本动态添加和删除。有一种方法可以做到这一点,这样做很好?

  db.Table(TableName)< SpecificModel> ;.其中(x => x.ID == ID)


解决方案

p>你想这样做吗?

  foreach(string tableName in new [] {Table1,Table2 })
{
var result = dbContext.Database.SqlQuery< SpecificModel>(string.Format(SELECT * FROM {0} WHERE ID = @ p0,tableName),1).FirstOrDefault() ;
}


I have many tables with the same model structure but with other table names with other data (in this case will be ~100 tables). I want to dynamically switch table name in runtime using Entity Framework (for example get name table from routing). Tables in database are dynamically adding and deleting by other script. There is a way to do this with good performance like this?

db.Table("TableName")<SpecificModel>.Where(x => x.ID == ID)

解决方案

Do you want to do like this?

foreach (string tableName in new[] { "Table1", "Table2" })
{
   var result = dbContext.Database.SqlQuery<SpecificModel>(string.Format("SELECT * FROM {0} WHERE ID=@p0", tableName), 1).FirstOrDefault();
}

这篇关于动态表名与实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆