简体   繁体   English

ASP.NET MVC 4,迁移 - 如何在生产服务器上运行“update-database”

[英]ASP.NET MVC 4, Migrations - How to run 'update-database' on a production server

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 谢谢

You have a couple of options: 你有几个选择:

  • You could use update-database -script to generate the SQL commands to update the database on the server 您可以使用update-database -script生成SQL命令以更新服务器上的数据库
  • You could use the migrate.exe executable file that resides in the package folder on /packages/EntityFramework5.0.0/tools/migrate.exe . 您可以使用migrate.exe驻留在包装上的文件夹中的可执行文件/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. 我过去曾使用Jet Brains的Team City Build Server成功地使用它来设置我的部署脚本的迁移。
  • If you're using IIS Web Deploy you can tell the server to perform the migrations after publish (see pic below) 如果您使用的是IIS Web Deploy,则可以告诉服务器在发布后执行迁移(参见下面的图片)
  • You could setup automatic migrations, but I prefer to be in control of when things happen :) 您可以设置自动迁移,但我更愿意控制发生的事情:)

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的博客 ,他在微软的MsBuild团队工作,并对部署有一些很好的见解

在此输入图像描述

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: 其中一个选项是将这样的东西放在DB上下文类的构造函数中:

public MyDbContext()
    {
        System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, Configuration>());            
    }

For us, the DBAs are the only group to have access to the production (and pre-production) environments. 对我们而言,DBA是唯一可以访问生产(和预生产)环境的组。 We simply use the Update-Database -Script package console command to get the Sql required to update the database. 我们只需使用Update-Database -Script包控制台命令来获取更新数据库所需的Sql。 This gets handed off to them where they can validate it, etc. 这可以传递给他们,在那里他们可以验证它,等等。

Maybe a little too simplistic for some but it works. 对某些人来说可能有点过于简单,但它有效。

HTH. HTH。

A simple solution: running Update-Database from your local Package Manager Console providing a connection string parameter with the production connection string. 一个简单的解决方案:从本地程序包管理器控制台运行Update-Database ,为生产连接字符串提供连接字符串参数。 You also have to provide the connection provider name (SqlServer in this example code): 您还必须提供连接提供程序名称(在此示例代码中为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 connectionStrings section: 您可以使用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. 但它可以帮助您快速破解开发远程服务器,测试环境等。

I personally like to setup automatic migrations that run every time the application's start method is called. 我个人喜欢设置每次调用应用程序的start方法时运行的自动迁移。 That way with every deployment you make you have the migrations just run and update the application automatically. 这样,您进行的每个部署都可以运行迁移并自动更新应用程序。

Check out this post from AppHarbor. 看看AppHarbor上的这篇文章。 http://blog.appharbor.com/2012/04/24/automatic-migrations-with-entity-framework-4-3 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。

You can get the scripts using EF commands (update-database -script) or you can write the script manually. 您可以使用EF命令(update-database -script)获取脚本,也可以手动编写脚本。 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. 有时脚本在DEV环境中正确执行,但它们在生产环境中失败。 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. 在创建表A之前,无法执行在表A中插入行的数据脚本。 This is what I do: -Define a table for registering the executed scripts. 这就是我所做的: - 定义一个用于注册已执行脚本的表。 For example: ExecutedScriptsHistory. 例如:ExecutedScriptsHistory。 -Each script has a number and a name. - 每个脚本都有一个数字和一个名称。 -After a script is executed, a new row is inserted in table ExecutedScriptsHistory. - 执行脚本后,在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). 为此,它会检查脚本是否已执行(存在于ExecutedScriptsHistory表中)。

After you have run the scripts, you can check if all the scripts have been executed checking ExecutedScriptsHistory. 运行脚本后,可以检查是否已执行所有脚本,检查ExecutedScriptsHistory。 This strategy is similar to the one chosen by Microsoft in EF Migration but you have full control of it. 此策略类似于Microsoft在EF Migration中选择的策略,但您可以完全控制它。

just to give everyone the simple answer. 只是为每个人提供简单的答案。

This is the "Update-Database" In your Migrations folder, Configuration.cs: 这是“更新 - 数据库”在迁移文件夹中,Configuration.cs:

    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>());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生产数据库迁移ASP.NET MVC 4 - Production Database Migrations ASP.NET MVC 4 asp.net MVC 应用程序中的更新数据库问题 - Update-database problem in asp.net MVC application 实体框架5,迁移,MVC 4,更新数据库-force-在服务器上运行的最佳方法 - Entity Framework 5, Migrations, MVC 4, update-database -force - best way to run on server 在更新数据库期间,ASP.NET MVC初始化程序不会为数据库设置种子 - ASP.NET MVC Initializer doesn't seed Database during Update-Database ASP.NET MVC“更新数据库”问题。 DB 在 SSMS 中不可见 - ASP.NET MVC "update-database" problem. DB not visible in SSMS 在ASP.Net MVC应用程序中执行更新数据库时,Visual Studio冻结 - Visual Studio freezes when executing update-database in ASP.Net MVC app 更新数据库代码首先迁移 ASP.NET MVC 时,ORA-00955 名称已被现有对象错误 - ORA-00955 name is already used by an existing object Error when Update-Database Code First Migration ASP.NET MVC 将ASP.NET MVC数据库部署到生产环境 - Deploying ASP.NET MVC Database to Production 实体框架代码优先 - 如何为生产数据库运行 Update-Database - Entity framework code first - how to run Update-Database for production database 使用已经准备好的迁移ASP.NET MVC创建数据库 - Creating database using already prepared migrations ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM