microsoft / DACExtensions

DACExtensions contains samples that extend Data-Tier Applications using DacFx. These samples include deployment contributors and static code analysis rules that can be used with Visual Studio as well as examples of how to use the DacFx public mode
MIT License
125 stars 41 forks source link

Microsoft.SqlServer.Dac.DacServicesException for PostDeployment only project #31

Closed davisnw closed 5 years ago

davisnw commented 5 years ago

I'm using the nuget package Microsoft.SqlServer.DACFx 150.4441.1-preview

I have a database project used for sample data that only contains post deployment scripts.

The publish profile works fine in Visual Studio (2019), however, the same publish profile fails with the exception: Microsoft.SqlServer.Dac.DacServicesException : Cannot deploy to existing database when upgrading has been disabled.

The publish profile is pretty basic:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <IncludeCompositeObjects>True</IncludeCompositeObjects>
    <TargetDatabaseName>MyTargetDb</TargetDatabaseName>
    <DeployScriptFileName>MyTargetDb_SampleData.sql</DeployScriptFileName>
    <TargetConnectionString>Data Source=(localdb)\mssqllocaldb;Integrated Security=True;Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True</TargetConnectionString>
    <ProfileVersionNumber>1</ProfileVersionNumber>
    <BlockOnPossibleDataLoss>True</BlockOnPossibleDataLoss>
    <DropConstraintsNotInSource>False</DropConstraintsNotInSource>
    <DropDmlTriggersNotInSource>False</DropDmlTriggersNotInSource>
    <DropExtendedPropertiesNotInSource>False</DropExtendedPropertiesNotInSource>
    <DropIndexesNotInSource>False</DropIndexesNotInSource>
    <DropStatisticsNotInSource>False</DropStatisticsNotInSource>
    <IncludeTransactionalScripts>False</IncludeTransactionalScripts>
    <IgnoreAnsiNulls>False</IgnoreAnsiNulls>
    <VerifyCollationCompatibility>False</VerifyCollationCompatibility>
    <VerifyDeployment>False</VerifyDeployment>
  </PropertyGroup>
</Project>

The publishing code is:

var dac = new DacServices(publishConnString);

using (var dbPackage = DacPackage.Load(SampleData, DacSchemaModelStorageType.Memory))
{
    var publishProfile = DacProfile.Load(PublishProfileSampleData);

    var options = publishProfile.DeployOptions;
    //Tried setting the below options - no change in error
    //options.BlockOnPossibleDataLoss = false;
    //options.BlockWhenDriftDetected = false;
    //options.AllowIncompatiblePlatform = true;

    //PostDeployment script is present - this looks like the correct sql
    string sql = new StreamReader(dbPackage.PostDeploymentScript).ReadToEnd();

    //fails with  Cannot deploy to existing database when upgrading has been disabled.`
    dac.Deploy(dbPackage, dbName, options: options);
}
davisnw commented 5 years ago

This is not a bug. I just needed to set upgradeExisting: true e.g.

dac.Deploy(dbPackage, dbName, options: options, upgradeExisting: true);

Closing issue.