Last active
June 19, 2026 09:22
-
-
Save kipusoep/1d6af1e40c6b689e61d018348e86816a to your computer and use it in GitHub Desktop.
Import BACPAC without external table resources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Add-Type -AssemblyName System.Windows.Forms | |
| # Selecteer BACPAC | |
| $dialog = New-Object System.Windows.Forms.OpenFileDialog | |
| $dialog.Filter = "BACPAC files (*.bacpac)|*.bacpac" | |
| $dialog.Title = "Selecteer een BACPAC bestand" | |
| if ($dialog.ShowDialog() -ne "OK") { | |
| Write-Host "Geen bestand geselecteerd." | |
| exit | |
| } | |
| $bacpac = $dialog.FileName | |
| # Temp folder | |
| $workDir = Join-Path $env:TEMP ("bacpac_" + [guid]::NewGuid()) | |
| New-Item -ItemType Directory -Path $workDir | Out-Null | |
| Write-Host "Extracting model.xml..." | |
| Expand-Archive -Path $bacpac -DestinationPath $workDir | |
| $modelFile = Join-Path $workDir "model.xml" | |
| [xml]$xml = Get-Content $modelFile | |
| # Namespace fix | |
| $ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable) | |
| $ns.AddNamespace("x", $xml.DocumentElement.NamespaceURI) | |
| # Remove external objects | |
| $nodes = $xml.SelectNodes( | |
| "//x:Element[ | |
| starts-with(@Type, 'SqlExternal') | |
| or @Type='SqlExternalDataSource' | |
| or @Type='SqlExternalTable' | |
| or @Type='SqlExternalFileFormat' | |
| or @Type='SqlDatabaseScopedCredential' | |
| ]", | |
| $ns | |
| ) | |
| Write-Host "Removing $($nodes.Count) external objects..." | |
| foreach ($node in $nodes) { | |
| $node.ParentNode.RemoveChild($node) | Out-Null | |
| } | |
| # Cleanup relationships | |
| $rels = $xml.SelectNodes("//x:Relationship[contains(@Name,'External')]", $ns) | |
| foreach ($rel in $rels) { | |
| $rel.ParentNode.RemoveChild($rel) | Out-Null | |
| } | |
| $xml.Save($modelFile) | |
| # Target DB info | |
| $server = Read-Host "SQL Server (e.g.: localhost)" | |
| $dbName = Read-Host "Database name" | |
| Write-Host "Starting import..." | |
| Write-Host "Server: $server" | |
| Write-Host "Database: $dbName" | |
| SqlPackage ` | |
| /Action:Import ` | |
| /SourceFile:$bacpac ` | |
| /TargetServerName:$server ` | |
| /TargetDatabaseName:$dbName ` | |
| /TargetTrustServerCertificate:True ` | |
| /ModelFilePath:"$modelFile" | |
| Write-Host "" | |
| Write-Host "✅ Import done: $dbName on $server" | |
| # Cleanup | |
| Remove-Item $workDir -Recurse -Force |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ref: https://stackoverflow.com/questions/42028861/error-importing-azure-bacpac-file-to-local-db-error-incorrect-syntax-near-extern