如何用实体框架4.x动态选择一个表? [英] How can you dynamically select a table with entity framework 4.x?

查看:233
本文介绍了如何用实体框架4.x动态选择一个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 MyDatabase 的数据库,它有两个表 - MyTable1 MyTable2 。使用Entity Framework 4.x和.NET 4中的Code First方法,我生成了一个名为 MyDatabaseContext 的上下文,其中包含 MyTable1s MyTable2s



通常,访问表格如 MyDatabaseContext.Table1s。< ;指挥GT; 。在我的数据库中,许多表共享了几个常见的字段(例如CreatedOn,ModifiedOn)。我想要能够动态地替换一个表名,所以我可以对任何表执行相同的查询。



我真正想要什么(哪些似乎不可用)是像 MyDatabaseContext [ContextTableName]。< Command>。



有没有人有任何想法这种功能存在于在查询时动态选择表。

解决方案

查看动态linq。以下是Scott Guthrie的一篇文章: http://weblogs.asp.net/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: http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

这篇关于如何用实体框架4.x动态选择一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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