Search code examples
c#sql-serversql-server-2014sql-server-data-toolssql-server-data-project

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

Solution

  • 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?