假设我有一个名为 MyDatabase
的数据库,其中有两个表 - MyTable1
和 MyTable2
。使用 Entity Framework 4.x 和 .NET 4 的 Code First 方法,我生成了一个名为 MyDatabaseContext
的上下文,其中包含 MyTable1s
和 MyTable2s
。
通常,表的访问方式类似于 MyDatabaseContext.Table1s.
。在我的数据库中,许多表共享几个公共字段(例如 CreatedOn、ModifiedOn)。我希望能够动态替换表名,以便我可以对任何表执行相同的查询。
我真正想要的(似乎不可用)是类似 MyDatabaseContext[ContextTableName]..
有谁知道是否存在这样的功能来在查询时动态选择表时间。
Suppose I have a database named MyDatabase
which has two tables - MyTable1
and MyTable2
. Using the Code First approach with Entity Framework 4.x and .NET 4, I have generated a context named MyDatabaseContext
which contains MyTable1s
and MyTable2s
.
Normally, tables are accessed like MyDatabaseContext.Table1s.<Command>
. In my database many tables share a couple of common fields (e.g. CreatedOn, ModifiedOn). I would like to to be able to dynamically substitute a table name so I can execute the same query against any table.
What I really want (and which does not seem to be available) is something like MyDatabaseContext[ContextTableName].<Command>.
Does anyone have any idea if such functionality exists to dynamically select a table at query time.
发布评论
评论(2)
查看动态 linq。这是 Scott Guthrie 的一篇关于它的文章: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Check out dynamic linq. Here is an article about it from Scott Guthrie: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
我对每天构建的表有同样的问题,例如:TableName_2014_06_06
最后我只是回到基础知识并使用 SqlConnection ..
我建议你也这样做
i had same issue with tables that were build for each day like: TableName_2014_06_06
In the end i just went back to basics and used SqlConnection ..
I suggest that you do the same