cs0815
cs0815

Reputation: 17388

Create dacpac file programmatically from SSDT project

Is it possible to create a 'fresh' .dacpac file from an existing SSDT project? I would like to be able to recreate the database (teardown + setup) before each automatic end-to-end test:

const string dacPacFileName = @"D:\Bla.dacpac";
var connectionString = ConfigurationManager.ConnectionStrings["BlaConnectionString"].ConnectionString;

var dacPackage = DacPackage.Load(dacPacFileName);
var dacServices = new DacServices(connectionString);      
var dacOptions = new DacDeployOptions();
dacOptions.CreateNewDatabase = true;
dacServices.Deploy(dacPackage, "Bla", true, dacOptions); 

Upvotes: 4

Views: 4170

Answers (2)

Bennet roy
Bennet roy

Reputation: 121

var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo
{
    WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
    FileName = @"C:\Program Files\Microsoft SQL Server\150\DAC\bin\SqlPackage.exe",
    Arguments = "/TargetFile:D:/output_target.dacpac /Action:Extract 
                /SourceServerName:ServerName /SourceDatabaseName:DatabaseName 
                /SourceUser:UserId /SourcePassword:Password"
};
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();

Upvotes: 2

onupdatecascade
onupdatecascade

Reputation: 3366

I have done this using PowerShell by calling something like

> msbuild.exe <mysolution>.sln /p:Configuration=<myconfig>

This will build the solution and should make the dacpac.

After that I call

> sqlpackage.exe /Action:<someaction> /SourceFile:<thatdacpac> /Profile:<publishprofile>

but you might just need the first step if you are deploying the dacpac as opposed to doing schema compare and publish. Does that help at all?

Upvotes: 3

Related Questions