动态表名与实体框架 [英] Dynamic table name with 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屋!
查看全文