如何用实体框架4.x动态选择一个表?

编程入门 行业动态 更新时间:2024-10-27 14:18:18
本文介绍了如何用实体框架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的一篇文章: 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动态选择一个表?

本文发布于:2023-11-17 17:05:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何用   实体   框架   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!