Reputation: 28
I have 2 entities, for example Parent and Child.
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:
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:
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.
Upvotes: 0
Views: 87
Reputation: 394
I think you have to find all the Parents in your delete child method and remove this specific child form theirs Childs collection...this will remove the relation and then you can delete the child itself.
Upvotes: 0