asp.net-mvc ASP.NET MVC 4,迁移 - 如何在生产服务器上运行“更新数据库”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17020393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC 4, Migrations - How to run 'update-database' on a production server
提问by niico
I can use package manager to run 'update-database -verbose' locally.
我可以使用包管理器在本地运行“update-database -verbose”。
Probably a stupid question but I can't find it online - once my website is deployed - how can I run this manually on the server?
可能是一个愚蠢的问题,但我无法在网上找到它 - 一旦我的网站被部署 - 我怎样才能在服务器上手动运行它?
Secondarily - what other strategies would you recommend for deploying database migrations to production - and how would they be preferable?
其次 - 您会推荐哪些其他策略来将数据库迁移部署到生产中 - 以及它们如何更可取?
Thanks
谢谢
采纳答案by amhed
You have a couple of options:
你有几个选择:
- You could use
update-database -scriptto generate the SQL commands to update the database on the server - You could use the migrate.exeexecutable file that resides in the package folder on
/packages/EntityFramework5.0.0/tools/migrate.exe. I've used it successfully in the past with Jet Brains' Team City Build Server to setup the migrations with my deploy scripts. - If you're using IIS Web Deploy you can tell the server to perform the migrations after publish (see pic below)
- You could setup automatic migrations, but I prefer to be in control of when things happen :)
- 您可以使用
update-database -script生成 SQL 命令来更新服务器上的数据库 - 您可以使用migrate.exe可执行文件驻留在
/packages/EntityFramework5.0.0/tools/migrate.exe. 我过去曾成功地将它与 Jet Brains 的 Team City Build Server 一起使用,以使用我的部署脚本设置迁移。 - 如果您使用 IIS Web 部署,您可以告诉服务器在发布后执行迁移(见下图)
- 您可以设置自动迁移,但我更喜欢控制事情发生的时间:)
Update:Also, check out Sayed Ibrahim's blog, he works on the MsBuild Team at Microsoft and has some great insights on deployments
更新:另外,查看Sayed Ibrahim 的博客,他在 Microsoft 的 MsBuild 团队工作,对部署有一些深刻的见解


回答by Martin Falc
I know that the question is already answered, but for future reference:
我知道这个问题已经回答了,但供将来参考:
One of the options is to put something like this in the constructor of your DB context class:
一种选择是在你的数据库上下文类的构造函数中放置这样的东西:
public MyDbContext()
{
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, Configuration>());
}
回答by Sean Kenny
For us, the DBAs are the only group to have access to the production (and pre-production) environments. We simply use the Update-Database -Scriptpackage console command to get the Sql required to update the database. This gets handed off to them where they can validate it, etc.
对我们来说,DBA 是唯一可以访问生产(和预生产)环境的群体。我们只需使用Update-Database -Scriptpackage 控制台命令来获取更新数据库所需的 Sql。这会交给他们,他们可以在那里验证它,等等。
Maybe a little too simplistic for some but it works.
对于某些人来说可能有点过于简单,但它确实有效。
HTH.
哈。
回答by Diana
A simple solution: running Update-Databasefrom your local Package Manager Console providing a connection string parameter with the production connection string. You also have to provide the connection provider name (SqlServer in this example code):
一个简单的解决方案:Update-Database从本地包管理器控制台运行,提供带有生产连接字符串的连接字符串参数。您还必须提供连接提供程序名称(本示例代码中的 SqlServer):
Update-Database -ConnectionString <your real remote server connection string here> -ConnectionProviderName System.Data.SqlClient
Instead of the connection string you can use a connection string name present in your app.config file connectionStringssection:
您可以使用 app.config 文件connectionStrings部分中存在的连接字符串名称代替连接字符串:
Update-Database -ConnectionStringName <your connection string name here>
You must have permissions to access that server from your local machine. For example, if you are able to connect to the server from a Sql Server Management Studio you can use this.
您必须有权从本地计算机访问该服务器。例如,如果您能够从 Sql Server Management Studio 连接到服务器,则可以使用它。
Note that this approach is not recommended for a real production system, you should use something like what is explained in the accepted answer. But it can help you with quick hacks in development remote servers, test environments, etc.
请注意,不建议将此方法用于真正的生产系统,您应该使用类似于已接受答案中解释的内容。但它可以帮助您快速破解开发远程服务器、测试环境等。
回答by Khalid Abuhakmeh
I personally like to setup automatic migrations that run every time the application's start method is called. That way with every deployment you make you have the migrations just run and update the application automatically.
我个人喜欢设置每次调用应用程序的启动方法时运行的自动迁移。这样,在您进行的每个部署中,您都可以运行迁移并自动更新应用程序。
Check out this post from AppHarbor. http://blog.appharbor.com/2012/04/24/automatic-migrations-with-entity-framework-4-3
查看来自 AppHarbor 的这篇文章。http://blog.appharbor.com/2012/04/24/automatic-migrations-with-entity-framework-4-3
The gist is basically you want to enable auto migrations then call the DatabaseInitializer from your code, either from the OnModelCreating method or from your Global.asax.
要点基本上是您要启用自动迁移,然后从您的代码中调用 DatabaseInitializer,无论是从 OnModelCreating 方法还是从您的 Global.asax。
回答by Francisco Goldenstein
You can get the scripts using EF commands (update-database -script) or you can write the script manually. This is not the most important thing about updating the database in production environment. For me, the most important thing is to be sure that all the scripts were run correctly and they have affected records as expected. In my opinion, you should have a preproduction environment and the database should be a copy of the production environment. This way, you can run the scripts and deploy the application in a pretty similar environment and see if there are any problems. Sometimes the scripts are executed correctly in DEV environment but they fail in production environment. To avoid a headache, you should simulate the production environment in a preproduction environment. Regarding the scripts, if the team has more than one developer I prefer to categorize the scripts in structure scripts and data scripts. Structure scripts alters the structure of the database (add a table, add a column to a table, etc.) and data scripts inserts/updates/deletes records. Also, each script should specify its dependencies so they cannot be executed in the wrong order. A data script that inserts rows in table A cannot be executed until table A has been created. This is what I do: -Define a table for registering the executed scripts. For example: ExecutedScriptsHistory. -Each script has a number and a name. -After a script is executed, a new row is inserted in table ExecutedScriptsHistory. -Before a script is executed, it checks its dependencies. In order to do that, it checks if the scripts have been executed (exists in table ExecutedScriptsHistory).
您可以使用 EF 命令 (update-database -script) 获取脚本,也可以手动编写脚本。这不是在生产环境中更新数据库最重要的事情。对我来说,最重要的是确保所有脚本都正确运行并且它们按预期影响了记录。在我看来,你应该有一个预生产环境,数据库应该是生产环境的副本。通过这种方式,您可以运行脚本并在非常相似的环境中部署应用程序,看看是否有任何问题。有时脚本在 DEV 环境中执行正确,但在生产环境中失败。为了避免头痛,您应该在预生产环境中模拟生产环境。关于剧本,如果团队有不止一个开发人员,我更喜欢将脚本分类为结构脚本和数据脚本。结构脚本更改数据库的结构(添加表、向表添加列等)和数据脚本插入/更新/删除记录。此外,每个脚本都应指定其依赖项,以免它们以错误的顺序执行。在创建表 A 之前,无法执行在表 A 中插入行的数据脚本。这就是我所做的: - 定义一个表来注册已执行的脚本。例如:ExecutedScriptsHistory。- 每个脚本都有一个编号和一个名称。- 执行脚本后,在表 ExecutedScriptsHistory 中插入新行。- 在执行脚本之前,它会检查其依赖项。为此,它会检查脚本是否已执行(存在于表 ExecutedScriptsHistory 中)。
After you have run the scripts, you can check if all the scripts have been executed checking ExecutedScriptsHistory. This strategy is similar to the one chosen by Microsoft in EF Migration but you have full control of it.
运行脚本后,您可以检查是否所有脚本都已执行检查 ExecutedScriptsHistory。此策略类似于 Microsoft 在 EF Migration 中选择的策略,但您可以完全控制它。
回答by Worthy7
just to give everyone the simple answer.
只是为了给大家一个简单的答案。
This is the "Update-Database" In your Migrations folder, Configuration.cs:
这是您的 Migrations 文件夹 Configuration.cs 中的“Update-Database”:
internal sealed class Configuration : DbMigrationsConfiguration<projectname.Models.dbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true; // Update-Data -Force (deletes columns etc)
}
And to "Enable migrations" in the first place on a remote server, add this to your Global.asax.cs file:
并首先在远程服务器上“启用迁移”,将其添加到您的 Global.asax.cs 文件中:
protected void Application_Start()
{
....
System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<dbContext, Migrations.Configuration>());

