EF 5 - 第一型数据库迁移:变化模型后,发布到远程生产服务器? [英] EF 5 - Model first database migration: change model after published to remote production server?

查看:122
本文介绍了EF 5 - 第一型数据库迁移:变化模型后,发布到远程生产服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC 5,EF 5.0 code首先发展。我使用文件共享文件夹中发布项目。

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

我可以使用数据库迁移当模型改变为更新发展的LocalDB。然而,当我发表这个项目到远程服务器,它显示模型的误差被更改以及使用完整的SQL数据库服务器生产Web服务器上需要迁移。

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.


  • 可以产生可以应用到生产数据库的SQL脚本。像这样-Script标志运行的软件包管理器控制台更新,数据库:

  • 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:

更新,数据库-Script -SourceMigration:InitialMigration -TargetMigration:LatestMigration

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

或者使用像红门SQL的工具比较来生成脚本。

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

在应用程序启动就可以自动通过注册MigrateDatabaseToLatestVersion数据库初始化升级数据库:

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

Database.SetInitializer(新MigrateDatabaseToLatestVersion());

Database.SetInitializer(new MigrateDatabaseToLatestVersion());

您可以阅读更多有关的文章 MSDN两种方法 - code首先迁移

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

这篇关于EF 5 - 第一型数据库迁移:变化模型后,发布到远程生产服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆