简体   繁体   English

从 SSDT 项目以编程方式创建 dacpac 文件

[英]Create dacpac file programmatically from SSDT project

Is it possible to create a 'fresh' .dacpac file from an existing SSDT project?是否可以从现有的 SSDT 项目创建一个“新鲜”的.dacpac文件? 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); 

I have done this using PowerShell by calling something like我通过调用类似的东西使用 PowerShell 完成了此操作

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

This will build the solution and should make the dacpac.这将构建解决方案并且应该创建 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.但是,如果您要部署 dacpac 而不是进行架构比较和发布,则您可能只需要第一步。 Does that help at all?这些帮助有用?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM