[英]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. 我首先使用MVC 5,EF 5.0代码进行开发。 I publish the project using file sharing folder.
我使用文件共享文件夹发布该项目。
I can use database migration to update the developing localDb when the model is changed. 更改模型后,我可以使用数据库迁移来更新正在开发的localDb。 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.
但是,在将项目发布到远程服务器后,它显示了在使用完整SQL数据库服务器的生产Web服务器上更改模型和需要迁移的错误。
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. 您可以生成可应用于生产数据库的SQL脚本。 Run the Update-Database in the Package Manager Console with the -Script flag like so:
使用-Script标志在程序包管理器控制台中运行Update-Database,如下所示:
Update-Database -Script -SourceMigration: InitialMigration -TargetMigration: LatestMigration 更新数据库-脚本-SourceMigration:InitialMigration -TargetMigration:LatestMigration
Or use a tool like Red Gate SQL Compare to generate the script. 或使用Red Gate SQL Compare之类的工具来生成脚本。
On Application startup you can automatically upgrade the database by registering the MigrateDatabaseToLatestVersion database initializer: 在应用程序启动时,您可以通过注册MigrateDatabaseToLatestVersion数据库初始化程序来自动升级数据库:
Database.SetInitializer(new MigrateDatabaseToLatestVersion()); Database.SetInitializer(new MigrateDatabaseToLatestVersion());
You can read more about both methods in the article MSDN - Code First Migrations . 您可以在文章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,
EF代码首先不是为了这个目的,而只是为了开发,
There are a couple of options, if you use Visual Studio Ultimate, there is a schema compare tool under SQL Menu. 有两个选项,如果您使用Visual Studio Ultimate,则SQL菜单下有一个架构比较工具。 Alternatively, you can use Red Gate SQL Compare tools.
或者,您可以使用Red Gate SQL比较工具。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.