简体   繁体   中英

EF 5 - Model first database migration: change model after published to remote production server?

I'm developing in MVC 5, EF 5.0 code first. I publish the project using file sharing folder.

I can use database migration to update the developing localDb when the model is changed. However, after I published the project to a remote server, it shows the error of models being changed and migration required on the production web server which use a full SQL database server.

I know I can delete the database on the production server and let it recreates the database. However, there are already user entered data in the tables.

Is there a way to apply the model/database changes without recreate the database?

Yes there is a way. Actually there are several.

  • You can generate a SQL script that can be applied to the production database. Run the Update-Database in the Package Manager Console with the -Script flag like so:

    Update-Database -Script -SourceMigration: InitialMigration -TargetMigration: LatestMigration

    Or use a tool like Red Gate SQL Compare to generate the script.

  • On Application startup you can automatically upgrade the database by registering the MigrateDatabaseToLatestVersion database initializer:

    Database.SetInitializer(new MigrateDatabaseToLatestVersion());

You can read more about both methods in the article MSDN - Code First Migrations .

I never had the option to delete the database on the production. EF code first is not for that purpose, it's for development only,

There are a couple of options, if you use Visual Studio Ultimate, there is a schema compare tool under SQL Menu. Alternatively, you can use Red Gate SQL Compare tools.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM