通常,访问表格如 MyDatabaseContext.Table1s。< ;指挥GT; 。在我的数据库中,许多表共享了几个常见的字段(例如CreatedOn,ModifiedOn)。我想要能够动态地替换一个表名,所以我可以对任何表执行相同的查询。
我真正想要什么(哪些似乎不可用)是像 MyDatabaseContext [ContextTableName]。< Command>。
有没有人有任何想法这种功能存在于在查询时动态选择表。
解决方案查看动态linq。以下是Scott Guthrie的一篇文章: weblogs.asp/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query -library.aspx
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.
解决方案Check out dynamic linq. Here is an article about it from Scott Guthrie: weblogs.asp/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
更多推荐
如何用实体框架4.x动态选择一个表?
发布评论