【问题标题】:Delete an entity with referential integrity constraints删除具有参照完整性约束的实体
【发布时间】:2010-10-21 02:59:38
【问题描述】:

假设您有以下表格:

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

使用 Entity Framework 4,我发出一个 ObjectContext.ExecuteStoreCommand 调用来删除一些朋友。但是,我收到一条错误消息,内容如下:

*System.Data.SqlClient.SqlException 未处理 Message=DELETE 语句与 REFERENCE 约束“FK_Address_Friend”冲突。冲突发生在数据库“MyFriends”、表“dbo.Address”、列“FriendId”中。 该语句已终止。*

如何确保 Cascade 删除的所有内容?数据库中是否有一些我必须设置的属性?

【问题讨论】:

    标签: c# entity-framework entity-framework-4


    【解决方案1】:

    听起来您没有在 SQL FK 上正确设置级联。可能您将级联设置设置为无?如果您没有使用框架来执行删除(甚至可能是),则必须将 SQL Server 配置为级联删除。

    【讨论】:

      猜你喜欢
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多