简体   繁体   English

Linq To Sql,对实体的扩展删除将不再允许它删除

[英]Linq To Sql, Extended Delete of an Entity wont let it delete anymore

I have 2 entities, for example Parent and Child. 我有2个实体,例如父级和子级。

what I'm trying to achieve here is to have Delete method of entity Child extended so i can do some stuff when it is Deleting, here is what i have done so far: 我要在这里实现的目的是扩展实体Child的Delete方法,以便在删除时可以做一些事情,这是我到目前为止所做的:

partial class MyDataClassDataContext
{
    partial void DeleteChild(Child instance)
    {
        //My Custom Code Here
    }
}

there is an event in the code that would delete a Parent and all of its Children, something like this: 代码中有一个事件会删除一个Parent及其所有子级,如下所示:

 void DeleteParent(Parent itemParent)
 {
    var ListChilds = db.Parents.Where(p=>p == itemParent).Select(p=>p.Childs);
    if(ListCilds.Any())
    {
       db.Chields.DeleteAllOnSubmit(ListCilds);
    }
    db.Parents.DeleteOnSubmit(itemParent);
    db.SubmitChanges();
 } 

This code always worked, but when i try to extend the Delete method of Child, it seems to wait for me to decide whether or not to delete them, and i get an exception that Parent cannot be deleted where it have Child. 这段代码始终有效,但是当我尝试扩展Child的Delete方法时,似乎在等待我决定是否删除它们,并且我得到一个例外,即在有Child的地方无法删除Parent。

我认为您必须在您的delete child方法中找到所有Parents并从其Childs集合中删除此特定孩子...这将删除关系,然后您可以删除孩子本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM