Specify a cascading delete for parent / child relationships?

房东的猫 提交于 2020-01-14 19:23:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!