【问题标题】:How to deploy to production with entity framework code first如何先使用实体​​框架代码部署到生产环境
【发布时间】:2023-03-05 17:14:01
【问题描述】:

我从 asp.net Web Forms 转向 MVC 和实体框架代码优先方法。我有个问题。如何设置我的环境以部署到生产环境?

我正在使用 Visual Studio 2012 并部署一个 Web 部署包。在本地,我有 SQL Express,在生产环境中,我有 SQL Server 2008。

我想要的是在我的电脑上本地开发一个测试,并不时使用 web 部署包将我的解决方案部署到生产中。我不想在生产系统中运行迁移,而是想从 Visual Studio 生成脚本,然后我可以将其附加到生产 SQL。

我尝试过:

  • 在 dev 中创建初始迁移。
  • 本地更新数据库
  • 生成脚本,更新数据库 -script -sourcemigration:InitialCreate
  • 在生产中应用此脚本
  • 将应用程序部署到生产环境

这是正确的方法吗?如果我的迁移将在本地运行,它们会不会因为我的迁移代码而无法在生产中运行?

在 global.asax 中

Migrator.RunMigrations();

其中 RunMigrations 是这样的自定义 Migrator 类中的静态方法

public class Migrator
    {
        public static void RunMigrations()
        {
            var migrator = new Configuration();
            var dbMigrator = new System.Data.Entity.Migrations.DbMigrator(migrator);
            if (dbMigrator.GetPendingMigrations().Any())
            {
                dbMigrator.Update();
            }
        }
    }

【问题讨论】:

    标签: asp.net-mvc entity-framework deployment


    【解决方案1】:

    您可以使用Web Setup Project(安装需要 MSI 自定义操作):

    更新数据库的实际工作可以通过migrate.exe工具来完成。要使 MSI 包正确运行,结果证明是一个挑战。我首先在安装包中包含了migrate.exe,将它与系统的程序集一起部署到bin目录。支持将 .exe 文件作为 Web 设置项目中的自定义操作运行。不幸的是,除非工作目录设置为 bin 目录,否则我无法让 migrate.exe 工作。默认情况下,自定义操作的工作目录是 c:\windows\system32。为了解决这个问题,使用了一个小的 vb 脚本。

    http://coding.abel.nu/2012/04/update-database-msi-custom-action/

    更新:

    我找到了thisthisthis,也许会有所帮助。

    【讨论】:

      【解决方案2】:

      回滚所有迁移

      更新数据库-TargetMigration:0

      更新数据库-脚本

      https://cpratt.co/migrating-production-database-with-entity-framework-code-first/

      【讨论】:

        猜你喜欢
        • 2017-01-23
        • 1970-01-01
        • 2013-07-27
        • 1970-01-01
        • 1970-01-01
        • 2012-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多