How can you dynamically select a table with entity

2019-02-10 09:59发布

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条回答
戒情不戒烟
2楼-- · 2019-02-10 10:30
虎瘦雄心在
3楼-- · 2019-02-10 10:34

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

查看更多
登录 后发表回答