sql.net.net-coredacpacsqlpackage

Migrating .sqlproj to Sdk style breaks certain database options


I'm trying to migrate my existing .sqlproj targeting .NET Framework 4.7.2 to the new Sdk style project so that I can generate the .dacpac using the new .NET SDK. My new .csproj looks like this:

<Project Sdk="MSBuild.Sdk.SqlProj/1.17.3">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <SqlServerVersion>SqlAzure</SqlServerVersion>
    <BundledNETCoreAppTargetFrameworkVersion>5.0</BundledNETCoreAppTargetFrameworkVersion>
  </PropertyGroup>

  <PropertyGroup>
    <SchemaVersion>2.0</SchemaVersion>

    <CompatibilityMode>130</CompatibilityMode>
    <AnsiNullDefault>False</AnsiNullDefault>
    <AnsiPadding>False</AnsiPadding>
    <AnsiWarnings>False</AnsiWarnings>
    <ArithAbort>False</ArithAbort>
    <ConcatNullYieldsNull>False</ConcatNullYieldsNull>
    <NumericRoundAbort>False</NumericRoundAbort>
    <RecursiveTriggersEnabled>False</RecursiveTriggersEnabled>
.......................
  </PropertyGroup>

The second PropertyGroup is a 99% copy of the same element from the old .sqlproj - I want to be sure the new .dacpac is identical to the old one. The problem is a few of the configuration params are not respected by the new .csproj. If I generate a script from the new .dacpac with SqlPackage utility against the same database it contains the following which is not present in the old script:

IF EXISTS (SELECT 1
           FROM   [sys].[databases]
           WHERE  [name] = N'$(DatabaseName)')
    BEGIN
        ALTER DATABASE [$(DatabaseName)]
            SET ANSI_PADDING ON,
                ANSI_WARNINGS ON,
                ARITHABORT ON,
                QUOTED_IDENTIFIER ON,
                ANSI_NULL_DEFAULT ON 
            WITH ROLLBACK IMMEDIATE;
    END

In other words, the params like AnsiPadding and AnsiWarnings and a few others are ignored. Moreover, I had to comment a few other params that caused the build to fail like DbScopedConfigLegacyCardinalityEstimation:

System.ArgumentException: Unable to parse value for property with name DbScopedConfigLegacyCardinalityEstimation: Off (Parameter 'value')

Why is this happening and how can I fix it? I need to be sure switching to Sdk style project doesn't break my database which would be a disaster in production.


Solution

  • Apparently they renamed a lot of things. So if you want to migrate your .sqlproj to the new Sdk style .csproj you need to be careful. AnsiPadding is now AnsiPaddingOn, AnsiWarnings is AnsiWarningsOn, etc.

    <AnsiPaddingOn>False</AnsiPaddingOn>
    <AnsiWarningsOn>False</AnsiWarningsOn>
    <ArithAbortOn>False</ArithAbortOn>
    

    Some non-boolean params are also changed. For example:

    <TargetRecoveryTimeUnit>SECONDS</TargetRecoveryTimeUnit>`
    

    Now needs to be (notice the casing, see Time Unit enum)

    <TargetRecoveryTimeUnit>Seconds</TargetRecoveryTimeUnit>
    

    Check the official Microsoft.SqlServer.Dac.Model documentation and always verify the generated script. I've successfully migrated my project and now use .dacpac built from the new .csproj to apply my migrations.