HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Can't restore Azure bacpac into localdb with SqlPackage

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
canwithintolocaldbazuresqlpackagebacpacrestore

Problem

I have made a backup of my SQL Azure in a bacpac file and when I try to import the bacpac in localdb it always fail.

Here is the command I use:

PS C:\Program Files (x86)\Microsoft SQL Server\110\DAC\bin> .\SqlPackage.exe /Action:Import 
/SourceFile:"C:\Users\S\Desktop\MyDb.bacpac" 
/TargetConnectionString:"Data Source=(localdb)\v11.0;Initial Catalog=MyDb; Integrated Security=true;"


And here is the error message:

*** Error importing database:Data cannot be imported into target because it 
contains one or more user objects. Import should be performed against a new, empty 
database.
Error SQL0: Data cannot be imported into target because it contains one or more
user objects. Import should be performed against a new, empty database.


My machine is newly installed, and I have never used localdb at all.

Solution

I encountered this problem and, after some frustration, solved it.

What had happened is that you have created, most likely by mistake, a user object in the model Database. On Management Studio look under Databases\System Databases\model. This is the template that is used when you create a new database, and is used when importing the bacpac. If there are objects (such as a table) in the model, they are automatically created at the same time as the new database. The bacpac import then encounters them in what it expects to be a blank database, and so immediately fails.

You'll also see these same objects created if you manually create a new database.

Deleting the object(s) from the model database solves the problem.

Context

StackExchange Database Administrators Q#52327, answer score: 5

Revisions (0)

No revisions yet.