问题
Model:
public class MenuItem
{
public Guid Id {get;set;}
public virtual Guid? ParentMenuItemId {get;set;}
public virtual MenuItem ParentMenuItem {get;set;}
public virtual ICollection<MenuItem> ChildMenuItems {get;set;}
}
current mapping:
HasOptional(m => m.ParentMenuItem).WithMany(p => p.ChildMenuItems).HasForeignKey(m => m.ParentMenuItemId);
I tried adding the WillCascadeOnDelete(true)
, but I got an error. How should I update my mapping to allow for cascading deletes? So, If I delete a Parent, all the children will be deleted. Do I have to do this manually?
回答1:
In your model, just look at the association's properties. There is an OnDelete that you can set to Cascade. The XML should look like this:
<OnDelete Action="Cascade" />
来源:https://stackoverflow.com/questions/9432994/specify-a-cascading-delete-for-parent-child-relationships