我们正在使用DacServices
. 然而,有时会添加新的约束导致约束冲突,这需要我们编写迁移脚本以确保列中的数据有效。
但是,DacServices.Deploy
如果ScriptNewConstraintValidation
选项为 true 并且新的约束错误,我无法弄清楚如何回滚 DacServices 所做的更改。
我们正在使用 AlwaysOn 可用性,因此执行备份/恢复将非常困难。以下是我尝试过的一种方法,但没有成功。
var dacServices = new DacServices(dbConnection.ConnectionString);
var deploymentOptions = new DacDeployOptions
{
CreateNewDatabase = createNewDatabase,
ScriptDatabaseCompatibility = false,
ScriptDatabaseCollation = false,
IgnoreUserSettingsObjects = true,
DropPermissionsNotInSource = false,
DropRoleMembersNotInSource = false,
IgnorePermissions = true,
IgnoreRoleMembership = true,
GenerateSmartDefaults = true,
IncludeTransactionalScripts = true,
ScriptNewConstraintValidation = true
};
Microsoft.SqlServer.Dac.DacPackage dacPackage = DacPackage.Load(dacPath);
Console.WriteLine("Deploying DacPac");
CancellationTokenSource tokenSource = new CancellationTokenSource();
try
{
dacServices.Deploy(dacPackage, databaseName, true, deploymentOptions, tokenSource.Token);
}
catch(Exception)
{
tokenSource.Cancel();
}