DeleteOnSubmit() doesn’t perform Delete on child objects.

DeleteOnSubmit() doesn’t perform Delete on child objects.

Postby AKhan » 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?
AKhan
 
Posts: 4
Joined: Wed 22 Sep 2010 17:17

Postby StanislavK » Tue 19 Oct 2010 16:48

The DeleteOnSubmit() method marks that the entity should be deleted at the nearest SubmitChanges call:
http://www.devart.com/linqconnect/docs/Devart.Data.Linq~Devart.Data.Linq.Table%601~DeleteOnSubmit.html
When DeleteOnSubmit is executed, the entity should neither be removed from the corresponding collection nor deleted from the table.

Please tell us if you are observing a behaviour different from this.
StanislavK
Devart Team
 
Posts: 1710
Joined: Thu 03 Dec 2009 10:48

Postby AKhan » Fri 29 Oct 2010 14:35

I looked at different examples and codes for the delete, and I think i am doing every thing as suggested.
When DeleteOnSubmit is executed the entity is still attached and exist in both collection and table.

My issues is that i am calling DeleteOnSubmit on an inner Object (grandchild) of the main object (Customer), and it is part of the collection. The delete work on the first or 2nd level of objects but not at the deeper level.
AKhan
 
Posts: 4
Joined: Wed 22 Sep 2010 17:17

Postby StanislavK » Mon 01 Nov 2010 14:55

I will send you a test project, please check that it was not blocked by your mail filter. The following entities are used in the sample: Company -> Order -> Order Detail (each Company has a collection of Orders, each Order has a collection of Order Details). For a single Company entity, all of the corresponding Order Details are deleted.

Please specify if this scenario is similar to the one you are trying to perform; if not, please describe what should be changed in the sample, or send us your test project.
StanislavK
Devart Team
 
Posts: 1710
Joined: Thu 03 Dec 2009 10:48


Return to dotConnect for Oracle