How can you dynamically select a table with entity framework 4.x?
Asked Answered
C

2

5

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.

Cobnut answered 17/10, 2011 at 23:20 Comment(1)
look at this question and their answer: social.msdn.microsoft.com/Forums/eu/linqprojectgeneral/thread/…Seducer
O
2

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

Oldenburg answered 17/10, 2011 at 23:30 Comment(1)
Thank you! I have used Dynamic Linq and it definitely has its purpose. However, I am hoping to find something which is more direct since the only thing which is changing is the table from which they query is being executed. If no other solutions appear, Dynamic Linq will definitely be the fallback.Cobnut
G
0

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

Goldeye answered 9/7, 2015 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.