使用 .Net Core 2.0 应用程序中的 .dacpac 文件部署数据库

Deploy database using .dacpac file from .Net Core 2.0 application

我正在尝试 migrate 我的应用程序从 net 4.6.1 升级到 netcore2.0Microsoft.SqlServer.Dac 出现了一些问题。我正在使用 DacServices (Microsoft.SqlServer.Dac 1.0.1).dacpac 文件部署数据库,但这 package supportsnet 4.6.1. 我如何从 netcore 部署 .dacpac 文件应用? 谢谢解答!

.NET Core support for DacFx is planned, but not here yet and you can't do it in .NET Core. Now if add a NuGet package Microsoft.SqlServer.DacFx.x64 还原将打印您:

Package 'Microsoft.SqlServer.DacFx.x64 140.3881.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.0'

有一段时间,您可以使用命令行实用程序SqlPackage.exe

SqlPackage.exe 
  /Action:Publish 
  /SourceFile:C:/file.dacpac 
  /TargetConnectionString:[Connection string]

您可以 运行 以编程方式:

var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo
{
    WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
    FileName = @"C:\Program Files (x86)\Microsoft SQL Server\<Version>\DAC\bin\SqlPackage.exe",
    Arguments = "/Action:Publish /SourceFile:C:/file.dacpac /TargetConnectionString:[Connection string]"
};
process.StartInfo = startInfo;
process.Start();

截至 2018 年 11 月 15 日的好消息:

... the preview package which supports netcoreapp2.1 and net46: https://www.nuget.org/packages/Microsoft.SqlServer.DACFx/150.4240.1-preview

请注意它是 Microsoft.SqlServer.DacFx nuget 包而不是 Microsoft.SqlServer.DacFx.x86 或 Microsoft.SqlServer.DacFx.x64 包,您需要在 nuget 中启用预发布模式。

在同一主题中讨论:

https://github.com/Microsoft/DACExtensions/issues/20#issuecomment-439222493

接下来,我需要看看它是否有效...

Microsoft.SqlServer.DacFx netstandard2.0 现已发布 nuget 包: https://www.nuget.org/packages/Microsoft.SqlServer.DacFx/

要将 dacpac 文件发布到 SQL 服务器 LocalDB,您可以使用:

using Microsoft.SqlServer.Dac;

...

var dacpac  = DacPackage.Load(@"path\to.dacpac");
var dacpacService = new DacServices("Server=(localdb)\mssqllocaldb;Database=TargetDatabase;Trusted_Connection=True;MultipleActiveResultSets=true");
dacpacService.Publish(dacpac, "TargetDatabase", new PublishOptions());