简体   繁体   English

EF 5-模型首次数据库迁移:发布到远程生产服务器后要更改模型?

[英]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.

相关问题 更改asp.net mvc模型的EF Code First模型后,如何在Azure中正确更新SQL数据库? - How to properly update SQL database in Azure after EF Code First model of asp.net mvc model change? EF4 Database First Model 基于正确规范化表的设计 - EF4 Database First Model Design based on properly normalized table 更改数据库第一个实体模型的连接字符串 - Change the connection string of the database first entity model 修改模型结构而不更改数据库优先方法中现有的EF 4.x生成的模型 - Modify Model structure without changing existing EF 4.x Generated Model in Database First Approach 首先在远程安全数据库上使用EF和数据库 - Using EF with database first on a remote and secured database 在MVC中更改模型后如何更改数据库? - How to change database after altering model in MVC? 将数据库从嵌入式更改为SQL Server Express后,Entity Framework 4.1代码第一个模型兼容性问题 - Entity Framework 4.1 code first model compatibility issue after changing database from embedded to SQL Server Express 在生产服务器上运行EF迁移的最佳方法是什么? - What is the best way to run EF migration on production server? 使用 EF4 代码优先:如何在不丢失数据的情况下更改 Model - Using EF4 Code First: How can I change the Model without losing data 模型和表格更改错误EF 4.1 - model and table change error EF 4.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM