DeleteOnSubmit() doesn’t perform Delete on child objects.
Posted: Mon 18 Oct 2010 15:27
We are having issues with physically deleting child records in a collection. Consider following Structure.
Customer -> ContactCollection - > ContactMethod Collection.
Customer has collection of Contacts and Contact has collection of ContactMethods. Bellow is something similar I am trying to do.
dbContext.DeferredLoadingEnabled = false;
dbContext.Customer.Attach(customer, originalCustomer);
foreach (Contact contact in customer.ContactCollection)
{
Var originalContact = getOriginalContact(contact.Id);
dbContext.Contacts.Attach(contact, originalContact);
foreach(ContactMethod method in contact.ContactMethodCollection)
{
Var originalConatactMethod = getOriginalContactMethod(method.Id);
If(method.DeleteItem)
{
dbContext.ContactMethods.Attach(method);
dbContext. ContactMethods.DeleteOnSubmit(method); }
else
dbContext.ContactMethods.Attach(method, originalConatactMethod);
}
}
_ dbContext.SubmitChanges();
The code sample above doesn’t have statements for InsertOnSubmit() and it because Inser works fine for me. The code doesn’t throw any exception but even it run through the DeleteOnSubmit() Statement it doesn’t delete the record and neither does it run any Query for the Delete (checked using db monitor).
If I take out the Delete Statement and place it out side of the loop i.e. (directly delete the child without going throw each Contact and ContactMethod) it work just trigger delete statement.
Am I missing anything or is this not how DeleteOnSubmit should be used?
Customer -> ContactCollection - > ContactMethod Collection.
Customer has collection of Contacts and Contact has collection of ContactMethods. Bellow is something similar I am trying to do.
dbContext.DeferredLoadingEnabled = false;
dbContext.Customer.Attach(customer, originalCustomer);
foreach (Contact contact in customer.ContactCollection)
{
Var originalContact = getOriginalContact(contact.Id);
dbContext.Contacts.Attach(contact, originalContact);
foreach(ContactMethod method in contact.ContactMethodCollection)
{
Var originalConatactMethod = getOriginalContactMethod(method.Id);
If(method.DeleteItem)
{
dbContext.ContactMethods.Attach(method);
dbContext. ContactMethods.DeleteOnSubmit(method); }
else
dbContext.ContactMethods.Attach(method, originalConatactMethod);
}
}
_ dbContext.SubmitChanges();
The code sample above doesn’t have statements for InsertOnSubmit() and it because Inser works fine for me. The code doesn’t throw any exception but even it run through the DeleteOnSubmit() Statement it doesn’t delete the record and neither does it run any Query for the Delete (checked using db monitor).
If I take out the Delete Statement and place it out side of the loop i.e. (directly delete the child without going throw each Contact and ContactMethod) it work just trigger delete statement.
Am I missing anything or is this not how DeleteOnSubmit should be used?