我们有多个客户端在其服务器上运行不同的数据库版本。一些客户端使用SQL Server 2008,一些SQL Server 2012,一些客户端使用SQL Server 2014。
我的Visual Studio 2015解决方案中有一个SSDT项目。
对于生产部署,首先我们要求客户端提供SQL Server版本&根据该版本,我们将合适的.dacpac
文件提供给客户端。
每次构建SSDT项目时,它只会在所选"目标平台"的输出文件夹中创建一个.dacpac
文件。版本(SSDT项目>属性>项目设置>目标平台)
如果选择SQL Server 2008作为目标平台 - 为SQL Server 2008创建单个.dacpac
文件
如果选择SQL Server 2012作为目标平台 - 为SQL Server 2012创建单个.dacpac
文件
有没有办法为单个版本中的所有版本创建多个.dacpac
文件,而无需更改"目标平台"手动设置?
使用pre / post构建事件还是使用某些Exe文件?
感谢。
答案 0 :(得分:1)
我有一个类似的事情,我需要一个项目来处理多个版本,我管理它的方式是将它设置为你支持的最低版本(我只需要2007r2和2012所以它也不是硬)。然后当我部署时,我设置" AllowIncompatiblePlatform"为真。
另一种方法是将dacpac打开为zip文件或使用System.Packaging .net api并更改model.xml中的版本。
我还在博客上写了一些关于你不能控制你正在部署的数据库时可能已经知道的一些困难:
如果您决定更改dacpac中的版本,请告诉我,我会告诉您如何但它确实意味着您跳过构建阶段并非理想。
版
答案 1 :(得分:1)
所以,我试图自己解决这个问题,一开始我走上了编辑项目 msbuild xml 的道路。不幸的是,无论我如何处理构建 xml,我都无法改变 DSP(SQL Server 版本)。最后我决定放弃并编写一个 PowerShell 来创建所有构建。这是我想出来的,以防它对其他人有帮助。将此 PowerShell 添加到您的解决方案文件夹,并修改其中的项目路径,它应该为每个 SQL 服务器版本为您创建一个单独的文件夹,每个 dacpac 都针对该版本。当然,所有构建参数都可以更改以适合您所需的输出。我希望构建不仅可以控制 SQL 版本,还可以控制 .net 框架版本。您还可以添加其他内容,例如条件编译符号。由你决定。
Clear-Host
# TIM C: I extrapolated how to do this from the shortcut that installs with VS 2019 called: "Developer PowerShell for VS 2019".
# I added the take on VSWHERE so it will work for any version 2017+
#. "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -?
$instance = (. "$(${env:ProgramFiles(x86)})\Microsoft Visual Studio\Installer\vswhere.exe" -latest -format json | ConvertFrom-Json)
$common7Path = ([System.IO.FileInfo]$instance.productPath).Directory.Parent.FullName
$instanceId = $instance.instanceId
$vsVersion = ([System.Version]$instance.installationVersion).Major
Import-Module "$common7Path\Tools\Microsoft.VisualStudio.DevShell.dll";
Enter-VsDevShell $instanceId
$scriptRoot = [System.IO.Directory]::GetParent($MyInvocation.MyCommand.Definition).FullName
Set-Location $scriptRoot
<# *********************************************************************
EDIT HERE: The sub path to the SSDT project to build
********************************************************************* #>
$projectPath = [System.IO.Path]::Combine($scriptRoot, "project\project.sqlproj")
<# *********************************************************************
********************************************************************* #>
$projectFileName = [System.IO.Path]::GetFileNameWithoutExtension($projectPath)
$builds = @(
[pscustomobject]@{sql=2008;version=100;framework="v3.5";constants=""},
[pscustomobject]@{sql=2012;version=110;framework="v4.5";constants="DOTNET45"},
[pscustomobject]@{sql=2014;version=120;framework="v4.5";constants="DOTNET45"},
[pscustomobject]@{sql=2016;version=130;framework="v4.5";constants="DOTNET45"}
)
$configs = @( "Debug", "Release" )
foreach ($build in $builds) {
foreach ($config in $configs) {
Write-Host "Building for $($build.sql)-$config" -ForegroundColor Yellow
& msbuild $projectPath `
/target:ReBuild /interactive:false /nologo /nodeReuse:false /p:platform="any cpu" /consoleLoggerParameters:ErrorsOnly `
/p:configuration="$config" /p:VisualStudioVersion="$vsVersion.0" /p:OutputPath="bin\$config\$($build.sql)\" `
/p:DSP="Microsoft.Data.Tools.Schema.Sql.Sql$($build.version)DatabaseSchemaProvider" `
/p:TargetFrameworkVersion=$($build.framework) /p:SqlTargetName="$($projectFileName)_$($build.sql)" `
/p:DefineConstants="$($build.constants)" | Out-Host
if (!$?) { Write-Host "Build -failed for $($build.sql)-$config" -ForegroundColor Red }
}
}