3

I want to know if there is a way to generate the class dynamically at runtime. I want to use it in Entity framework code first.

Consider if I have 100 tables(or connect to unknown database) I will have to create model/POCO for each table in EF Code First, instead of this I want to generate the POCO class and all its properties at runtime based on the database connected.

2 Answers2

0

Probably not.

Consider this... If the classes aren't defined before compilation, then how is any other code going to use them? If no other code is going to use them, why do you need them?

You can generate objects based on the table schema at design time. Doesn't Entity Framework in fact do this already?

I realize that I've linked to something that is "database first" instead of "code first" but, well, that's what you're asking:

I want to use it in Entity framework code first.

[...]

I will have to create model/POCO for each table in EF

You have a database, and you want to generate models based on the schema of that database. That's database-first.

David
  • 208,112
  • 36
  • 198
  • 279
-1

You can use the EF Power Tools (beta 3) for Visual Studio 2010 or 2012 to reverse engineer a database to POCOs. After installation, right click on a project and select Reverse Engineer Code First under the new Entity Framework menu group. The Power tools: http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

Rowan Miller has a blog post about this, and some advanced uses: http://romiller.com/2012/05/09/customizing-reverse-engineer-code-first-in-the-ef-power-tools/

Fredrik Ljung
  • 1,445
  • 13
  • 28