Microsoft® SQL Server® Data-Tier Application Framework (17.1 DacFx)
Sunday, March 31, 2019

Microsoft® SQL Server® Data-Tier Application Framework (17.1 DacFx)


A Coffee in Sunny Winter Afternoon
Sitecore Best Practices and Tricks/Tips

Skip to content
  • Home
  • About

Sitecore 9 Installation – Distributed

Sitecore 9 Installation Error – SQL Server Data-Tier Application Framework

Posted on October 27, 2018 by Wen Chen

During Sitecore 9 installation, no matter standalone or distributed topology, if you see the error like this below, it is mainly related to SQL Server Data-Tier Application Framework (DACFx).

data-orig-size=”1247,226″ data-comments-opened=”1″ data-image-meta=”"aperture":"0","credit":"","camera":"","caption":"","created_timestamp":"0","copyright":"","focal_length":"0","iso":"0","shutter_speed":"0","title":"","orientation":"0"” data-image-title=”2018-09-28_1117″ data-image-description=”” data-medium-file=”https://born4joy.files.wordpress.com/2018/10/2018-09-28_1117.png?w=640?w=300″ data-large-file=”https://born4joy.files.wordpress.com/2018/10/2018-09-28_1117.png?w=640?w=640″ src=”https://born4joy.files.wordpress.com/2018/10/2018-09-28_1117.png?w=640″ alt=”2018-09-28_1117″ srcset=”https://born4joy.files.wordpress.com/2018/10/2018-09-28_1117.png?w=640 640w, https://born4joy.files.wordpress.com/2018/10/2018-09-28_1117.png?w=150 150w, https://born4joy.files.wordpress.com/2018/10/2018-09-28_1117.png?w=300 300w, https://born4joy.files.wordpress.com/2018/10/2018-09-28_1117.png?w=768 768w, https://born4joy.files.wordpress.com/2018/10/2018-09-28_1117.png?w=1024 1024w, https://born4joy.files.wordpress.com/2018/10/2018-09-28_1117.png 1247w” sizes=”(max-width: 640px) 100vw, 640px” />

First, make sure you have installed “MS SQL Server Data-Tier Application Framework (DACFx) v17.1”. This is the prerequisite of Sitecore 9 installation. It will deploy Sitecore 9 SQL Server database objects and data with DACPAC file.

Second, check database server version, especially error message like “…..Microsoft.Data.Tools.Schema.Sql.Sql120DatabaseSchemaProvider is not valid…..”. Sitecore 9 requires MS SQL Server 2016 or above. The compatibility level should be 130. Here is the SQL script to check compatibility level.

select name, compatibility_level from sys.databases

Third, if all above are OK, then what’s the issue? This is the hardest part which I was facing. At the end I found there were several different versions of DACFx installed in the server. They might come with SQL Server Management Studio, Visual Studio 2015, 2017. So after registering DACFx path to specify which one to use, then ran the script and it was successful.

DACReg

 

Advertisements

Share this:

  • Twitter
  • Facebook
  • Google
This entry was posted in Information Technology , Sitecore and tagged DACFx , Data-Tier Application Framework , Sitecore , Sitecore 9 Installation , SQL Server . Bookmark the permalink .

Sitecore 9 Installation – Distributed

2 Responses to Sitecore 9 Installation Error – SQL Server Data-Tier Application Framework

  1. Aura Petric says:
    December 5, 2018 at 8:47 am

    Hi, have you installed the DACFx on the SQL Server machine or on the CMS instance? Thanks

    Reply

    • Wen Chen says:
      December 5, 2018 at 2:29 pm

      You should have it on CMS machine in order to install db package by script.

      Reply

Leave a Reply Cancel reply

  • Wen Chen (Toronto, Canada)

    Sitecore Architect passionate about user contextual experience driven by big data, artificial intelligence and machine learning

  • Recent Posts

    • Sitecore 9 Installation Error – SQL Server Data-Tier Application Framework
    • Sitecore 9 Installation – Distributed
    • Sitecore 9 Installation – Standalone
    • Sitecore CMS-Only Mode and Submit Queue
    • Sitecore SPEAK: ProgressBar Event Trigger
  • Recent Comments

    Wen Chen on Sitecore 9 Installation Error…
    Aura Petric on Sitecore 9 Installation Error…
    Sitecore 9 Installat… on Sitecore 9 Installation…
    Wen Chen on Sitecore CMS-Only Mode and Sub…
    Sunil Aher on Sitecore CMS-Only Mode and Sub…
  • Archives

    • October 2018
    • August 2018
    • July 2018
    • January 2018
    • November 2017
    • September 2017
    • May 2017
    • February 2017
    • January 2017
    • December 2016
    • November 2016
    • April 2016
    • March 2016
    • January 2016
    • November 2015
    • September 2015
    • August 2015
    • July 2015
    • June 2015
    • May 2015
    • April 2015
    • January 2015
    • October 2014
    • January 2014
    • December 2013
  • Categories

    • Context Intelligence
    • Entity Framework
    • Information Technology
    • Life Learning
    • MVC
    • ReactJS
    • Sitecore
    • Solr
    • Uncategorized
    • Website Performance
  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.com
  • Advertisements

A Coffee in Sunny Winter Afternoon

Blog at WordPress.com.

%d bloggers like this:



    This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use.
    Learn more

    TaskModuleSqlUtility

    0.1.1

    SqlPackageOnTargetMachines.ps1

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    function RunCommand

        param(
            [string]$command,
            [bool] $failOnErr = $true
        )

        $ErrorActionPreference = ‘Continue’

        if( $psversiontable.PSVersion.Major -le 4)
        
            $result = cmd.exe /c "`"$command`""  2>&1
        
        else
        

            Write-Verbose -Verbose $command
            $result = cmd.exe /c "$command"  2>&1
        

        $ErrorActionPreference = ‘Stop’

        if($failOnErr -and $LASTEXITCODE -ne 0)
        
            throw $result
        

        return $result

    function Get-SqlPackageOnTargetMachine

        try
        
            $sqlDacPath, $sqlVersion = LocateHighestVersionSqlPackageWithSql
            $sqlVersionNumber = [decimal] $sqlVersion
        
        catch [System.Exception]
        
            Write-Verbose ("Failed to get Dac Framework (installed with SQL Server) location with exception: " + $_.Exception.Message)
            $sqlVersionNumber = 0
        

        try
        
            $sqlMsiDacPath, $sqlMsiVersion = LocateHighestVersionSqlPackageWithDacMsi
            $sqlMsiVersionNumber = [decimal] $sqlMsiVersion
        
        catch [System.Exception]
        
            Write-Verbose ("Failed to get Dac Framework (installed with DAC Framework) location with exception: " + $_.Exception.Message)
            $sqlMsiVersionNumber = 0
        

        try
        
            $vsDacPath, $vsVersion = LocateHighestVersionSqlPackageInVS
            $vsVersionNumber = [decimal] $vsVersion
        
        catch [System.Exception]
        
            Write-Verbose ("Failed to get Dac Framework (installed with Visual Studio) location with exception: " + $_.Exception.Message)
            $vsVersionNumber = 0
        

        if (($vsVersionNumber -ge $sqlVersionNumber) -and ($vsVersionNumber -ge $sqlMsiVersionNumber))
        
            $dacPath = $vsDacPath
        
        elseif ($sqlVersionNumber -ge $sqlMsiVersionNumber)
        
            $dacPath = $sqlDacPath
        
        else
        
            $dacPath = $sqlMsiDacPath
        

        if ($dacPath -eq $null)
        
            throw  "Unable to find the location of Dac Framework (SqlPackage.exe) from registry on machine $env:COMPUTERNAME"
        
        else
        
            return $dacPath
        

    function Get-RegistryValueIgnoreError

        param
        (
            [parameter(Mandatory = $true)]
            [Microsoft.Win32.RegistryHive]
            $RegistryHive,

            [parameter(Mandatory = $true)]
            [System.String]
            $Key,

            [parameter(Mandatory = $true)]
            [System.String]
            $Value,

            [parameter(Mandatory = $true)]
            [Microsoft.Win32.RegistryView]
            $RegistryView
        )

        try
        
            $baseKey = [Microsoft.Win32.RegistryKey]::OpenBaseKey($RegistryHive, $RegistryView)
            $subKey =  $baseKey.OpenSubKey($Key)
            if($subKey -ne $null)
            
                return $subKey.GetValue($Value)
            
        
        catch
        
        
        return $null

    function Get-SubKeysInFloatFormat($keys)

        $targetKeys = @()
            foreach ($key in $keys)
            
                try 
                    $targetKeys += [decimal] $key
                
                catch 
            

        $targetKeys

    function Get-SqlPackageForSqlVersion([int] $majorVersion, [bool] $wow6432Node)

        $sqlInstallRootRegKey = "SOFTWARE", "Microsoft", "Microsoft SQL Server", "$majorVersion" -join [System.IO.Path]::DirectorySeparatorChar

        if ($wow6432Node -eq $true)
        
            $sqlInstallRootPath = Get-RegistryValueIgnoreError LocalMachine "$sqlInstallRootRegKey" "VerSpecificRootDir" Registry64
        
        else
        
            $sqlInstallRootPath = Get-RegistryValueIgnoreError LocalMachine "$sqlInstallRootRegKey" "VerSpecificRootDir" Registry32
        

        if ($sqlInstallRootPath -eq $null)
        
            return $null
        

        Write-Verbose "Sql Version Specific Root Dir for version $majorVersion as read from registry: $sqlInstallRootPath"

        $DacInstallPath = [System.IO.Path]::Combine($sqlInstallRootPath, "Dac", "bin", "SqlPackage.exe")

        if (Test-Path $DacInstallPath)
        
            Write-Verbose "Dac Framework installed with SQL Version $majorVersion found at $DacInstallPath on machine $env:COMPUTERNAME"
            return $DacInstallPath
        
        else
        
            return $null
        

    function LocateHighestVersionSqlPackageWithSql()
     %$_.GetSubKeyNames() 
        $versions = Get-SubKeysInFloatFormat $keys 

    function LocateHighestVersionSqlPackageWithDacMsi()

        $sqlDataTierFrameworkRegKeyWow = "HKLM:", "SOFTWARE", "Wow6432Node", "Microsoft", "Microsoft SQL Server", "Data-Tier Application Framework" -join [System.IO.Path]::DirectorySeparatorChar
        $sqlDataTierFrameworkRegKey = "HKLM:", "SOFTWARE", "Microsoft", "Microsoft SQL Server", "Data-Tier Application Framework" -join [System.IO.Path]::DirectorySeparatorChar

        if (-not (Test-Path $sqlDataTierFrameworkRegKey))
        
            $sqlDataTierFrameworkRegKey = $sqlDataTierFrameworkRegKeyWow
        

        if ((Test-Path $sqlDataTierFrameworkRegKey))
         %$_.GetSubKeyNames() 
            $versions = Get-SubKeysInFloatFormat $keys 

        $sqlRegKeyWow = "HKLM:", "SOFTWARE", "Wow6432Node", "Microsoft", "Microsoft SQL Server", "DACFramework", "CurrentVersion" -join [System.IO.Path]::DirectorySeparatorChar
        $sqlRegKey = "HKLM:", "SOFTWARE", "Microsoft", "Microsoft SQL Server", "DACFramework", "CurrentVersion" -join [System.IO.Path]::DirectorySeparatorChar

        $sqlKey = "SOFTWARE", "Microsoft", "Microsoft SQL Server", "DACFramework", "CurrentVersion" -join [System.IO.Path]::DirectorySeparatorChar

        if (Test-Path $sqlRegKey)
        
            $dacVersion = Get-RegistryValueIgnoreError LocalMachine "$sqlKey" "Version" Registry64
            $majorVersion = $dacVersion.Substring(0, $dacVersion.IndexOf(".")) + "0"
        

        if (Test-Path $sqlRegKeyWow)
        
            $dacVersionX86 = Get-RegistryValueIgnoreError LocalMachine "$sqlKey" "Version" Registry32
            $majorVersionX86 = $dacVersionX86.Substring(0, $dacVersionX86.IndexOf(".")) + "0"
        

        if ((-not($dacVersion)) -and (-not($dacVersionX86)))
        
            Write-Verbose "Dac Framework (installed with DAC Framework) not found on machine $env:COMPUTERNAME"
            return $null, 0
        

        if ($majorVersionX86 -gt $majorVersion)
        
            $majorVersion = $majorVersionX86
        

        $dacRelativePath = "Microsoft SQL Server", "$majorVersion", "DAC", "bin", "SqlPackage.exe" -join [System.IO.Path]::DirectorySeparatorChar
        $programFiles = $env:ProgramFiles
        $programFilesX86 = "$env:ProgramFiles(x86)"

        if (-not ($programFilesX86 -eq $null))
        
            $dacPath = $programFilesX86, $dacRelativePath -join [System.IO.Path]::DirectorySeparatorChar

            if (Test-Path("$dacPath"))
            
                Write-Verbose "Dac Framework (installed with DAC Framework Msi) found on machine $env:COMPUTERNAME at $dacPath"
                return $dacPath, $majorVersion
            
        

        if (-not ($programFiles -eq $null))
        
            $dacPath = $programFiles, $dacRelativePath -join [System.IO.Path]::DirectorySeparatorChar

            if (Test-Path($dacPath))
            
                Write-Verbose "Dac Framework (installed with DAC Framework Msi) found on machine $env:COMPUTERNAME at $dacPath"
                return $dacPath, $majorVersion
            
        

        return $null, 0

    function LocateSqlPackageInVS([string] $version)

        $vsRegKeyForVersion = "SOFTWARE", "Microsoft", "VisualStudio", $version -join [System.IO.Path]::DirectorySeparatorChar

        $vsInstallDir = Get-RegistryValueIgnoreError LocalMachine "$vsRegKeyForVersion" "InstallDir" Registry64

        if ($vsInstallDir -eq $null)
        
            $vsInstallDir = Get-RegistryValueIgnoreError LocalMachine "$vsRegKeyForVersion"  "InstallDir" Registry32
        

        if ($vsInstallDir)
        
            Write-Verbose "Visual Studio install location: $vsInstallDir" 

            $dacExtensionPath = [System.IO.Path]::Combine("Extensions", "Microsoft", "SQLDB", "DAC")
            $dacParentDir = [System.IO.Path]::Combine($vsInstallDir, $dacExtensionPath)

            if (Test-Path $dacParentDir)
             Sort-Object @e=$_.Name -as [int] -Descending

                foreach ($dacVersionDir in $dacVersionDirs)
                
                    $dacVersion = $dacVersionDir.Name
                    $dacFullPath = [System.IO.Path]::Combine($dacVersionDir.FullName, "SqlPackage.exe")

                    if(Test-Path $dacFullPath -pathtype leaf)
                    
                        Write-Verbose "Dac Framework installed with Visual Studio found at $dacFullPath on machine $env:COMPUTERNAME"
                        return $dacFullPath, $dacVersion
                    
                    else
                    
                        Write-Verbose "Unable to find Dac framework installed with Visual Studio at $($dacVersionDir.FullName) on machine $env:COMPUTERNAME"
                    
                
            
        

        return $null, 0

    function LocateHighestVersionSqlPackageInVS()
     %$_.GetSubKeyNames()
        $versions = Get-SubKeysInFloatFormat $keys 

    function Get-SqlPackageCmdArgs

        param (
        [string]$dacpacFile,
        [string]$targetMethod,
        [string]$serverName,
        [string]$databaseName,
        [string]$authscheme,
        [string]$sqlUsername,
        [string]$sqlPassword,
        [string]$connectionString,
        [string]$publishProfile,
        [string]$additionalArguments
        )

        Write-Verbose -Verbose "File is $dacpacFile"

        # validate dacpac file
        if ([System.IO.Path]::GetExtension($dacpacFile) -ne ".dacpac")
        
            throw "Invalid Dacpac file [ $dacpacFile ] provided"
        

        $sqlPkgCmdArgs = [string]::Format(‘ /SourceFile:"0" /Action:Publish’, $dacpacFile)

        if($targetMethod -eq "server")
        
            $sqlPkgCmdArgs = [string]::Format(‘0 /TargetServerName:"1"’, $sqlPkgCmdArgs, $serverName)
            if ($databaseName)
            
                $sqlPkgCmdArgs = [string]::Format(‘0 /TargetDatabaseName:"1"’, $sqlPkgCmdArgs, $databaseName)
            

            if($authscheme -eq "sqlServerAuthentication")
            
                $sqlPkgCmdArgs = [string]::Format(‘0 /TargetUser:"1" /TargetPassword:"2"’, $sqlPkgCmdArgs, $sqlUsername, $sqlPassword)
            
        
        elseif($targetMethod -eq "connectionString")
        
            $sqlPkgCmdArgs = [string]::Format(‘0 /TargetConnectionString:"1"’, $sqlPkgCmdArgs, $connectionString)
        

        if( ![string]::IsNullOrWhiteSpace($publishProfile) )
        
            # validate publish profile
            if ([System.IO.Path]::GetExtension($publishProfile) -ne ".xml")
            
                throw "Invalid Publish Profile [ $publishProfile ] provided"
            
            $sqlPkgCmdArgs = [string]::Format(‘0 /Profile:"1"’, $sqlPkgCmdArgs, $publishProfile)
        

        $sqlPkgCmdArgs = [string]::Format(‘0 1’, $sqlPkgCmdArgs, $additionalArguments)
        Write-Verbose "Sqlpackage.exe arguments : $sqlPkgCmdArgs"
        return $sqlPkgCmdArgs

    function Escape-SpecialChars

        param(
            [string]$str
        )

        return $str.Replace(‘`’, ‘“’).Replace(‘"’, ‘`"’).Replace(‘$’, ‘`$’)

    function Execute-DacpacDeployment

        param (
         [string]$dacpacFile,
         [string]$targetMethod,
         [string]$serverName,
         [string]$databaseName,
         [string]$authscheme,
         [string]$sqlUsername,
         [string]$sqlPassword,
         [string]$connectionString,
         [string]$publishProfile,
         [string]$additionalArguments
        )

        Write-Verbose "Entering script SqlPackageOnTargetMachines.ps1"
        $sqlPackage = Get-SqlPackageOnTargetMachine
        $sqlPackageArguments = Get-SqlPackageCmdArgs -dacpacFile $dacpacFile -targetMethod $targetMethod -serverName $serverName -databaseName $databaseName -authscheme $authscheme -sqlUsername $sqlUsername -sqlPassword $sqlPassword -connectionString $connectionString -publishProfile $publishProfile -additionalArguments $additionalArguments
        Write-Verbose -Verbose $sqlPackageArguments
        
        $command = "`"$sqlPackage`" $sqlPackageArguments"
        Write-Verbose "Executing command: $command"
        RunCommand -command $command -failOnErr $true