Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying database changes with EF 4.1

Does anyone have any best practices around deploying database changes in an EF 4.1 code-first solution? I know MS does not currently support database migrations for EF 4.1, but obviously people are going to need to do this from time to time.

Thanks

like image 244
RepDetec Avatar asked May 02 '11 20:05

RepDetec


People also ask

How do I run EF migrations on deploy?

Right click your web project, click publish, use web deploy, go to your databases, target your new database, ensure Execute Code First Migrations is checked (this will run all the migrations you've done for your localdb on your new database).

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.

Which option in EDMX can be used to get the latest changes from the database?

Use the update model wizard (to update the storage model), open the . edmx file using the XML editor, find the desired property in the CSDL (conceptual model) section and change the desired attributes. This is basically the same as option 1, but you're editing the XML directly (a find and replace might be useful here).


1 Answers

Once you deployed database to production you must do incremental changes. It means that before you deploy next version you must prepare two databases in your dev box:

  • Database with DB schema currently deployed in production - you should be able to get this from source control so always correctly label / tag your production releases
  • Database with new DB schema

Once you have two databases you can use some tool to make difference SQL script for you. I have experience with both:

  • Visual Studio 2010 Premium / Ultimate Database tools
  • Red Gate SQL Compare

These tools are for SQL server.

Once you have difference script you can test it on your dev box. Be aware that some more complicated changes cannot be created by difference script and require you to create custom migration script for example with storing data existing data in temporary tables while refactoring real table. Also if you use some new seed data in your new version you must add them manually into script or use Data Compare tools (also offered by both products).

After that you can plan outage of your production application, database backup and running upgrade script.

like image 62
Ladislav Mrnka Avatar answered Sep 26 '22 18:09

Ladislav Mrnka