如何使用Azure Devops管道将代码优先的Entity Framework数据库部署到SQL Server [英] How to deploy a code-first Entity Framework database using Azure Devops pipelines to SQL server
问题描述
我们正在使用带有Azure DevOps的Entity Framework核心以及使用MSSQL Server 2016定位IIS的ASP.net应用程序.
我们的测试服务器是包含IIS和SQL Server实例的本地Windows 2016计算机.
我已经成功编写了一个部署工作流,可以在代码的测试服务器上进行连续部署,但是我找不到任何部署数据库的方法.第一个版本的目标是asp.net core 2.0,因此我可以使用命令行在IIS外部启动应用程序并触发数据库更新,但是当我们切换到2.2并在进程中进行IIS部署时,我们显然失去了该功能./p>
我想将数据库的部署集成到部署管道中的服务器上,包括数据库的创建,但是我找不到使用Azure Devops的任何方法:我可以将Azure SQL实例作为目标,但是,除非我缺少某些东西,而不是本地的东西.
所以:
- 如何在未安装SDK的计算机上使用ASP.NET core 2.2进程内应用程序手动创建和填充数据库?
- 我需要添加什么到Azure DevOps管道中才能将数据库部署到本地MSSQL服务器数据库
为了在本地部署,我遵循以下两个步骤:
1.创建数据库脚本
您可以创建普通的创建数据库脚本,该脚本在本地数据库实例中创建数据库.
2.应用迁移
创建简单的控制台应用程序,该应用程序首先运行创建数据库脚本,然后应用迁移.
myDbContext.Database.Migrate();
这就是我的工作方式.
有关将数据库发布到Azure的先前内容:
您需要"Azure SQL发布"任务. 前提条件是您应该已经创建了Azure SQL数据库.
步骤:
Step1:命令在构建管道中生成迁移脚本
创建命令行任务以生成迁移脚本:
dotnet ef migrations script -i -o %BUILD_ARTIFACTSTAGINGDIRECTORY%\migrate.sql --project EfMigrationApp.Database\EfMigrationApp.Database.csproj --startup-project EfMigrationApp\EfMigrationApp.csproj -i -o %BUILD_ARTIFACTSTAGINGDIRECTORY%\migrate.sql
步骤2:发布管道中的Azure SQL发布
操作:发布 类型:SQL脚本文件 sql脚本应如下所示:
$(System.ArtifactsDirectory)/_$(Build.DefinitionName)/drop/migrate.sql
请参考本文用于设置CI管道. 请参阅 解决方案
EDIT: For deploying on local, I followed below two steps:
1. Create Database Script
You can create normal Create Database script which creates the database in the local database instance.
2. Apply Migrations
Create simple console application which runs the create database script first and then applies the migrations.
myDbContext.Database.Migrate();
That's how I got it working.
Previous Contents about Publishing DB to Azure:
You need "Azure SQL Publish" task. Pre requisite is you should have Azure SQL Database already created.
Steps:
Step1 : Command To generate migration script in build pipeline
Create a command line task to generate the migration script:
dotnet ef migrations script -i -o %BUILD_ARTIFACTSTAGINGDIRECTORY%\migrate.sql --project EfMigrationApp.Database\EfMigrationApp.Database.csproj --startup-project EfMigrationApp\EfMigrationApp.csproj -i -o %BUILD_ARTIFACTSTAGINGDIRECTORY%\migrate.sql
Step 2: Azure SQL Publish in release pipeline
Action: Publish Type: SQL script file Sql script should be as below:
$(System.ArtifactsDirectory)/_$(Build.DefinitionName)/drop/migrate.sql
Refer this article for setting up CI pipeline. Refer this article for setting up CD pipeline.
这篇关于如何使用Azure Devops管道将代码优先的Entity Framework数据库部署到SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!