0

I'm trying to figure out what the best way to go would be for Entity Framework code generation with a "database first" approach. I want the complete master description of the model to be the actual database schema with annotations stored in database in tables or extended properties. Not interested in a model first or code first approach. Not opposed to writing custom code to implement if it will yield the highest quality solution. Here are some ideas:

(1) Generate EDMX from the database. Use custom code to add annotations to the EDMX regarding uniqueness and other constraints that are not currently built in to EF. Use T4 templates to generate code from the EDMX.

(2) Write a custom tool that generates "code first" classes from the database. No EDMX files in the solution.

(3) Something else?

It seems like #1 requires deeper knowledge of EF and more actual work than #2, which I am now opposed to if there are significant benefits.

Any advice?

Jason Kresowaty
  • 16,105
  • 9
  • 57
  • 84
  • You know you can do "code first" and not have it generate the schema, don't you? All you have to do is create the class to match the DB schema, though you do have to make any changes in both places. – tvanfosson Apr 01 '12 at 13:58
  • @Ivan, regarding "All you have to do is create the class to match the DB schema", the question is whether to generate this code directly or to put an EDMX file in the middle. – Jason Kresowaty Apr 01 '12 at 14:09
  • I like the control that code first gives me over the shape of the class and find that code first is much more easily testable than the autogenerated code from the designer. In fact, I wouldn't use EF until code first shipped; Linq2SQL was better IMO using the designer. Now, I use EF code first as my first choice with MSSQL. Unfortunately, I don't find any of those methods to be adequate to design the entire schema in the IDE (designer or code) yet and typically use DB-first with a code first model. – tvanfosson Apr 01 '12 at 14:18

2 Answers2

0

You can import the model from your database with EF. Right-click on the EF designer and choose "update model from database" option.

If you want a little more you can try http://radarc.net . It's a code generation tool which creates scaffolding and CRUDs. It uses T4 templates and code first so you can customize the way it generates code.

Rafa
  • 1
0

Well, nothing is easier than just dragging the tables from the server explorer to the edmx designer surface. That is how I do it. I don't do any customizations to the model so I can just delete parts of it an regenerate.

usr
  • 168,620
  • 35
  • 240
  • 369