Entity Framework DbSet <?> - column names of an unknown database at compile time

So, I came across a situation where I need to use the Entity Framework (DbContext) with databases whose columns are not known at compile time. Basically, I don’t have the luxury of hardcoding the customTable class to host in DbSet.

The only thing I can think of to solve this problem is to use Reflection.Emit to create a customTable class at runtime based on the information I collect by polling the database.

I was not able to find any information to solve this problem, but I am very new to working with the Entity Framework, so maybe the solution will be more obvious to someone more experienced.

I feel that there should be a cleaner way than using Reflection.Emit to dynamically create a class to feed a DbSet.

If I am leaving the database for the intended use of the Entity Framework, this information will also be useful.

+5
source share
1 answer

Hi, I am also studying this problem and I have found what seems like a solution.

http://romiller.com/2012/03/26/dynamically-building-a-model-with-code-first/

tells you how to make a DbContext into which you add a new DbSet, using the type as a parameter. This type can be created using the Dynamic Linq library:

http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

CreateClass, POCO , DbSet , .

- DbSet DbContext,

db.Set(type)

'type' - , . linq linq.

, CMS, CMS, DI , .

0

All Articles