4
我有一个实体:实体框架6代码优先级联删除自引用实体
public class Section : SortableEntity
{
private ICollection<Section> _sections;
public ICollection<Section> Sections
{
get
{
return _sections ?? (_sections = new HashSet<Section>());
}
set
{
_sections = value;
}
}
public string Title { get; set; }
public string Description { get; set; }
public Section ParentSection { get; set; }
public int? ParentSectionId { get; set; }
}
而且在模型创建我有一个配置:
modelBuilder.Entity<Section>().HasOptional(x => x.ParentSection).WithMany(p => p.Sections).HasForeignKey(d => d.ParentSectionId);
我试图做一个级联删除,并且出现以下错误: “DELETE语句与SAME TABLE REFERENCE约束冲突”FK_dbo.Section_dbo.Section_ParentSectionId“。
如何才能我在自引用实体上配置级联删除?
我不认为你可以在Entity Framework中做到这一点,因为你不能在SQL SERVER的自引用表上使用CASCADE DELETE ......但我可能是错的。 –