Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 926
Oleg Bezhan Avatar asked Dec 17 '22 23:12

Oleg Bezhan


2 Answers

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();
like image 58
Dmitry Pavlov Avatar answered May 09 '23 14:05

Dmitry Pavlov


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...

like image 45
Alex KeySmith Avatar answered May 09 '23 13:05

Alex KeySmith