views:

94

answers:

1

What are the best practices for database refactoring with codefirst EF4?

I am interested to hear how people change the classes and the database when the RecreateDatabaseIfModelChanges option is not feasible. Migration of data will need to occur.

Currently Microsoft has a solution for doing this with model first:

http://blogs.msdn.com/b/adonet/archive/2010/02/08/entity-designer-database-generation-power-pack.aspx?PageIndex=2#comments

Does anyone have a good strategy for code first?

A: 

You can't do that. DB is continuously created during development (it doesn't make much sense to use code only against existing database). After deployment to production you have to keep that version and continue development on the next version. Before deployment of the next version you have to create migration script for old database schema to new one. There are tools which can help you with this - for example VS 2008 Team System for Database developers and VS 2010 Premium or Red Gate SQL Compare.

Edit:

Ok I have to start differ between Code First and Code Only. Obviously these two terms don't mean everytime the same. So you want to have separate database project and use "code first" to map your entities to that database? In that case each time you refactor your code you have to update mapping or change database schema. I don't think there is any other way to keep those two in sync.

Ladislav Mrnka
Code first against a existing database: http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx
Adam
So you are saying put RecreateDatabaseIfModelChanges on in dev environment and off in prod and then use SQL Compare to generate a migration script that you keep in a VS database project?
Adam