Unable to restore bacpac due to foreign key confli

2019-04-07 10:53发布

I'm attempting to restore a backup (.bacpac) of a SQL Azure database to another SQL Azure database but am unable to do so because of the following error:

Error encountered during the service operation. Could not import package. Error SQL72014: .Net SqlClient Data Provider: Msg 547, Level 16, State 0, Line 3 The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_x_xx". The conflict occurred in database "x", table "dbo.x". Error SQL72045: Script execution error. The executed script: PRINT N'Checking constraint: FK_x_xx[dbo].[x]'; ALTER TABLE [dbo].[x] WITH CHECK CHECK CONSTRAINT [FK_x_xx];

I have also attempted to restore locally without success with a mscorlib exception (I'll be attempting it again and will update with the exact error message shortly.

I've checked the live database and I can't see why this key is being violated.

I've also attempted to modify the model.xml contained in the .bacpac to remove the constraint but this fails because it now (rightly so) fails checksum validation.

1条回答
Viruses.
2楼-- · 2019-04-07 11:23

The bacpac file is not transactional, so new rows written to your target database while the bacpac is being generated will end up corrupting the index. The database either must have no other users connected making writes, or you can copy the database and make a bacpac from the copy.

1) Copy the target database, which will return straight away, but the database will take some time to copy. This operation will create a full transactional copy:

CREATE DATABASE <name> AS COPY OF <original_name>

2) Find the status of your copy operation:

SELECT * FROM sys.dm_database_copies

3) Generate a bacpac file on the copied database, which isn't being used by anyone.

4) Delete the copied database, and you'll have a working bacpac file.

查看更多
登录 后发表回答