2

The method - Entity Framework Code-First - looks good. But its very difficult to create all the classes for a large database.

Is there any easy way to generate the Entity Framework Code-First classes?

Christopher
  • 10,409
  • 13
  • 73
  • 97
Mukesh
  • 807
  • 3
  • 10
  • 18

6 Answers6

3

You can use the recently released Entity Framework Power Tools CTP1. The tool gives you the ability to reverse engineer code first, meaning the Database will be mapped to Code.

Note that all tables in your large database will be mapped. There currently is no way to choose which tables will be mapped to code. Reading through the comments, this feature will most likely be implemented in a future release.

jotosmurf
  • 31
  • 1
2

The point of EF Code-First is that you define your domain model in code, then your user-interface and database can be easily generated from that domain model. This has a number of advantages including reducing the amount of tedious code which needs to be written, and helping to ensure your database, your domain model, and your UI match each other.

However, at some point you are going to have to write your domain model - there's no way that can be "generated" (by which I assume you mean computer-generated) as it is personal to your application.

If I've misunderstood your question, please leave a comment and I'll update my answer.

RB.
  • 36,301
  • 12
  • 91
  • 131
  • Yes I mean the same. But I 'm little confused on this line - "then your user-interface and database can be easily generated from that domain model". – Mukesh Mar 28 '11 at 08:47
  • Well, that's really the whole point of EF Code First. You write your code first (i.e. your domain model). Then you can use that to generate your database, and to generate your UI (e.g. View pages in MVC). I would look at a tutorial such as http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx, which shows exactly what I mean. – RB. Mar 28 '11 at 08:53
1

If you want to use the code-first model, but already have an existing database, you can use the Entity Framework Power Tools to generate classes.

If you're reading this after May/2012, the above tool may be out of beta!

Christopher
  • 10,409
  • 13
  • 73
  • 97
0

Generate the code from the database first using database first generation and then modify the resulting code to start your code first version

0

No there is no way to generate classes for you if you are using code-first. Code first means that there is no model and no database so you can't generate classes unless you have some upfront design in any case system (UML) which will autogenerate code for you. Simply generating classes without any input about how they should look like sounds like AI from Sci-fi, doesn't it?

If you already have databse you are not using code first but database first. In such case you can have your classes generated.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Yes I already have my database. I just planned to do like this just because of the easiness of using DataAnnotations. – Mukesh Mar 28 '11 at 09:15
  • The easiness is based on fact that with code-first you are defining the database but in your case you must set them exactly to reflect your database. Check that article I posted in my answer. – Ladislav Mrnka Mar 28 '11 at 09:30
  • Can you please suggest me some samples using edmx and partial classes for DataAnnotations. – Mukesh Mar 28 '11 at 09:33
  • Here is the example how to add data annotations: http://stackoverflow.com/questions/5128303/how-to-add-validation-to-my-pocotemplate-classes – Ladislav Mrnka Mar 28 '11 at 09:42
  • Ofen times you might find yourself starting in one direction and then going in a different direction. I started using the Database First approach. This was based on the recomendation of a co-worker. This approach created heavy classes with a tight binding between the model and classes that I was not happy with. Then I discovered Code First which is similar to nHibernate which we use heavily. – Roland Schaer May 13 '11 at 14:27
0

Check out the link below. It's a program that will generate POCO classes from your databases. I think that's what you're looking for.

http://msormcodegen.codeplex.com/

Roland Schaer
  • 1,656
  • 1
  • 22
  • 30