简体   繁体   English

删除具有参照完整性约束的实体

[英]Delete an entity with referential integrity constraints

Let's say you have the following tables: 假设您有以下表格:

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

Using Entity Framework 4, I am issuing an ObjectContext.ExecuteStoreCommand call to delete some friends. 使用实体框架4,我正在发出ObjectContext.ExecuteStoreCommand调用以删除一些朋友。 However, I get an error that says something like: 但是,我收到一条错误消息,内容如下:

*System.Data.SqlClient.SqlException was unhandled Message=The DELETE statement conflicted with the REFERENCE constraint "FK_Address_Friend". *未处理System.Data.SqlClient.SqlException Message = DELETE语句与REFERENCE约束“ FK_Address_Friend”冲突。 The conflict occurred in database "MyFriends", table "dbo.Address", column 'FriendId'. 数据库“ MyFriends”的表“ dbo.Address”的列“ FriendId”中发生了冲突。 The statement has been terminated.* 该语句已终止。*

How do I ensure that everything Cascade deletes? 如何确保Cascade删除的所有内容? Is there some property in the database that I have to set? 我必须在数据库中设置一些属性吗?

It sounds like you don't have the cascades set up properly on your SQL FK's. 听起来您在SQL FK上没有正确设置级联。 Possibly you have the cascade settings set to none? 可能您已将层叠设置设为无? If you're not using the framework to do the delete (and possibly even if you are) then SQL server has to be configured to cascade the deletes. 如果您不使用该框架进行删除(甚至可能使用),则必须将SQL Server配置为级联删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM