Visual Studio SSDT 数据库项目 (.sqlproj) 构建 - 使用 CLI 生成 .dacpac,无需 MSBUILD (msbuild.exe)

2023-12-23

看起来好像dotnet CLI根据以下内容,不支持数据库项目(.sqlproj):https://github.com/dotnet/sdk/issues/8546 https://github.com/dotnet/sdk/issues/8546

就我而言dotnet build失败并出现以下错误:

C:...*.Database.sqlproj(59,3): 错误 MSB4019:导入的项目“C:\Program 文件\ dotnet \ sdk \ 3.1.301 \ Microsoft \ VisualStudio \ v11.0 \ SSDT \ Microsoft.Data.Tools.Schema.SqlTask​​s.targets“ 没找到。确认进口申报中的表达式 “C:\程序 文件\ dotnet \ sdk \ 3.1.301 \ Microsoft \ VisualStudio \ v11.0 \ SSDT \ Microsoft.Data.Tools.Schema.SqlTask​​s.targets“ 是正确的,并且该文件存在于磁盘上。

不用说,该解决方案通过 Visual Studio 中的 Build 编译并生成 .dacpac。我需要这个才能从命令行工作。除了 msbuild.exe 之外还有其他解决方案吗?也许有一些可爱的 nuget 包可以提供帮助?


我已设法使其按本文所述工作:https://erikej.github.io/efcore/2020/05/11/ssdt-dacpac-netcore.html https://erikej.github.io/efcore/2020/05/11/ssdt-dacpac-netcore.html

它需要特殊类型的 .NET Standard Visual Studio 项目 -MSBuild.Sdk.SqlProj(nuget 下载),编译时从数据库项目复制脚本。

这是我的 build_database.cmd 代码:

SET db_project=Database.Build
dotnet tool install -g dotnet-script  
cd %db_project%
dotnet build
dotnet-script Program.csx 

以及一个 C# 脚本 (Program.csx),它使用以下命令创建并部署 .dacpacMicrosoft.SqlServer.DACFx努吉特包:

#r "nuget: Microsoft.SqlServer.DACFx, 150.4769.1"
using Microsoft.SqlServer.Dac;

var dbName = "SampleDatabase";
var connectionString = $"Data Source=.;Initial Catalog={dbName};Integrated Security=True;";
var dacpacLocation = Directory.GetFiles(@".\bin", "Database.Build.dacpac", 
                                                           SearchOption.AllDirectories)[0];
var dbPackage = DacPackage.Load(dacpacLocation);
var services = new DacServices(connectionString);

services.Deploy(dbPackage, dbName, true); 

这种方法是完全自动化的,可以在 CI 或本地开发环境中使用,而且显然也是跨平台的(尽管我只在 Windows 上进行了测试)。 希望这可以帮助 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Visual Studio SSDT 数据库项目 (.sqlproj) 构建 - 使用 CLI 生成 .dacpac,无需 MSBUILD (msbuild.exe) 的相关文章

随机推荐