Oleg Bezhan
Oleg Bezhan

Reputation: 180

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

I'm trying to migrate my app from net 4.6.1 to netcore2.0 and some problems with Microsoft.SqlServer.Dac have been occurred. I'm deploying the database from a .dacpac file using DacServices (Microsoft.SqlServer.Dac 1.0.1), but this package supports only net 4.6.1. How can I deploy .dacpac file from netcore application? Thanks for answers!

Upvotes: 4

Views: 7226

Answers (3)

Paul Pawletta
Paul Pawletta

Reputation: 66

Microsoft.SqlServer.DacFx nuget package is now released for netstandard2.0: https://www.nuget.org/packages/Microsoft.SqlServer.DacFx/

To publish a dacpac file to SQL Server LocalDB you can use:

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());

Upvotes: 3

Alex KeySmith
Alex KeySmith

Reputation: 17101

Good news as of Nov 15, 2018:

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

Be mindful it's the Microsoft.SqlServer.DacFx nuget package not the Microsoft.SqlServer.DacFx.x86 nor Microsoft.SqlServer.DacFx.x64 packages and you need to enable pre-release mode in nuget.

Discussed in the same thread:

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

Next up, I need to find out if it works...

Upvotes: 5

Dmitry Pavlov
Dmitry Pavlov

Reputation: 28320

The .NET Core support for DacFx is planned, but not here yet and you can't do it this way in .NET Core. Now if add a NuGet package Microsoft.SqlServer.DacFx.x64 restore will print you:

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'

For a while, you can use the command line utility SqlPackage.exe

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

And you can run it programmatically:

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();

Upvotes: 8

Related Questions