【发布时间】:2013-09-05 21:22:19
【问题描述】:
DELETE 语句与 SAME TABLE REFERENCE 约束冲突 “FK_AuthCategories_Parent”。数据库“MyDB”中发生冲突, 表“dbo.AuthCategories”, “父 ID”列。
如果我尝试删除具有 ParentID 的自引用 FK 的表中的所有内容,我会收到上面的错误,即我需要首先删除子项(即,它尝试删除具有子项的父项,这会破坏FK)。
var dc = from c in db.AuthCategories
select c;
db.AuthCategories.DeleteAllOnSubmit(dc);
db.SubmitChanges();
是否有一个简单的 LINQ to SQL 查询可以在处理级联删除时删除表中的所有内容?
- 不想使用 SQL 服务器端解决方案,例如触发器或 ON DELETE CASCADE
- 需要使用 LINQ to SQL,而不是 EF
- 希望它尽可能简单,如果可能的话,单行
这是表结构:
[Table(Name = "AuthCategories")]
public class AuthCategory
{
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int ID { get; set; }
[Column]
public string Name { get; set; }
[Column]
private int? ParentID { get; set; }
private EntityRef<AuthCategory> parent;
[Association(IsForeignKey = true, ThisKey = "ParentID")]
public AuthCategory Parent
{
get { return parent.Entity; }
set { parent.Entity = value; }
}
}
【问题讨论】:
标签: c# sql sql-server linq