【问题标题】:Running Entity Framework migrations during Octopus Deploy CI to Azure在 Octopus 期间运行实体框架迁移 将 CI 部署到 Azure
【发布时间】:2015-06-07 00:05:28
【问题描述】:

我需要设置一个持续集成流程,使用 Octopus Deploy 将我们的应用程序部署为 Azure 云服务。此过程包括针对我们的 Azure SQL 数据库执行 Entity Framework 6.1 迁移的步骤(通过从本地 Octopus 触手运行 migrate.exe)。但是,需要在 Octopus 机器上打开端口 1433 才能使其正常工作,而我们的管理员不会这样做。

您可以建议在自动部署过程中执行实体框架迁移的其他方法吗?

【问题讨论】:

  • 为什么打不开端口?你决定做什么?

标签: entity-framework azure octopus-deploy


【解决方案1】:

我们最终打开了那个端口,因为我们找不到任何其他解决方案。作为参考,这是我们正在运行的脚本(我们的 Deploy.ps1 脚本,由 NuGet 在每次部署时执行)。

# SOURCE: http://danpiessens.com/blog/2014/06/10/deploying-databases-with-octopus-deploy-part-2/

# Get the exe name based on the directory
$contentPath = (Join-Path $OctopusOriginalPackageDirectoryPath "content")
$fullPath = (Join-Path $OctopusOriginalPackageDirectoryPath "content\migrate.exe")

Write-Host "Content Path:" $contentPath
Write-Host "Migrate Path:" $fullPath

cd $contentPath
write-host "Working Dir: "$(get-location)

# Run the migration utility

& "$fullPath" MyApp.Data.dll /startUpConfigurationFile=MyApp.Web.dll.config /connectionString=$ApplicationConnectionString /connectionProviderName="System.Data.SqlClient" /verbose | Write-Host

【讨论】:

    【解决方案2】:

    我使用此代码在应用程序启动时运行迁移:

        class ApplicationDataContext : DbContext
        {
            internal static void UpdateDatabase()
            {
                Database.SetInitializer<ApplicationDataContext>(null);
    
                var settings = new Migrations.Configuration();
                var migrator = new DbMigrator(settings);
                migrator.Update();
    
            }
    }
    

    【讨论】:

    • 嗨@Serban,您是否在持续集成中使用它?如果是这样,您的部署过程是否会发现数据库迁移问题?
    • 嘿@Ozzy,是的,我将其作为持续集成/部署的一部分。
    • 好的@Serban。但是,如果迁移失败,您真的会在部署期间发现异常,还是仅在构建和部署过程后有人第一次实际运行您的应用程序时才发现异常?
    • 不,我不知道。只是当有人第一次运行应用程序时(通常是由进行部署的人完成,以检查事情是否顺利运行)。但是你想要完成的是什么?在我看来,如果您想在部署和回滚时运行迁移,那么您可以使用 powershell 脚本作为部署步骤之一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多