2010-10-20 261 views
1

實體比方說,你有如下表:刪除與參照完整性約束

Friend 
------ 
Id int not null (primary key) 
Name nvarchar(50) not null 

Address 
------- 
Id int not null (primary key) 
FriendId int not null (links to Friend.Id) 
City nvarchar(50) null 
Country nvarchar(50) not null 

使用實體框架4,我發出ObjectContext.ExecuteStoreCommand調用刪除一些朋友。但是,我收到的錯誤如下所示:

* System.Data.SqlClient.SqlException未處理 消息= DELETE語句與參考約束「FK_Address_Friend」衝突。衝突發生在數據庫「MyFriends」,表「dbo.Address」,列'FriendId'中。 該聲明已被終止。*

如何確保Cascade刪除所有內容?數據庫中是否有一些屬性需要設置?

回答

3

這聽起來像你沒有在你的SQL FK上正確設置的級聯。可能您已將級聯設置設置爲無?如果您沒有使用框架來執行刪除操作(甚至有可能),則必須配置SQL Server來級聯刪除操作。