Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using DropCreateDatabaseIfModelChanges in a production environment

I've just started learning .NET MVC so this may be a silly question, but I've yet to find a good answer.

I'm following the Code First approach using the Entity Framework to build my database for me. I've included the following in my Application_Start() method in order to allow me to edit my database by making changes to my Model objects.

Database.SetInitializer<ContactManagerDB>(new DropCreateDatabaseIfModelChanges<ContactManagerDB>());

I was just wondering what would happen if I pushed this application to a production environment and then made a few changes to my models and then updated the application? Would this really drop and recreate the database in the production environment?

What's the best practice for pushing changes to production env. using the Code First approach?

like image 851
August Flanagan Avatar asked May 10 '11 22:05

August Flanagan


2 Answers

DropCreateDatabaseIfModelChanges should only be use early on in development, never on a production machine. If you pushed to a production machine and made schema changes, you'd loose all your data.

like image 149
RickAndMSFT Avatar answered Oct 04 '22 14:10

RickAndMSFT


You could delete the EdmMetadata table in your production environment. In that case, EF would not know the current schema to compare to the new, so it would just assume you know what you are doing and it would not touch the database schema.

like image 37
Miguel Lima Avatar answered Oct 04 '22 15:10

Miguel Lima