Dynamic table name in entity framework linq

≡放荡痞女 提交于 2019-12-12 00:50:50

问题


I am currently using entity framework (.net 4) to read from a 3rd party database using LINQ statements. Unfortunately, at compile time i do not know from which table i will be reading - in fact, new tables can be added to this database after my application is compiled. The table name to read from will be passed as a string parameter to my method.

How should one approach this situation when the table name is not know at compile time? i cannot even add these tables to my data model since they might not yet exist. whilst i like the convenience of linq, i am after a simple approach.

thanks!


回答1:


For the queries that can only be constructed at run-time and that will return types of different shapes, you're pretty much forced to craft and execute the SQL you want to run yourself. DataContext.ExecuteQuery(string query, params object[] parameters) is going to be your friend.

You probably would already do this, but I would recommend keeping this portion of code isolated to one section of the code, where you execute the query, and then put the results into an strongly typed object before exposing it to other area's of your application. Make sure you clean the table name too.



来源:https://stackoverflow.com/questions/11472059/dynamic-table-name-in-entity-framework-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!