deleting an entity and froreign key error

Posts   
 
    
Aidan
User
Posts: 8
Joined: 20-Mar-2006
# Posted on: 30-Mar-2006 15:19:43   

I am having a problem deleting an entity in my application. (Self Service)

I have two entities CustomerEntity and ContactEntity

The CustomerEntity can have 0 or 1 ContactEntity. When I try to delete the ContactEntity from the CustomerEntity I get a sql foreign key exception

THe code I am using is

   CustomerEntity oCustomer = new CustomerEntity ();
   oCustomer.FetchUsingPK(337);
   oCustomer.Contact.Delete();
   oCustomer.Save(true);

From looking at a sql trace the only sql that gets run is to delete the row from the Contact table ... and this fails because there is a foreign tey to the row in the Customers table

Help !!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 30-Mar-2006 15:44:10   
oCustomer.Contact.Delete();

I think when the above line is excuted, it tries to delete the contact row from the database, when nothing has been done to remove the row(s) that reference it from the Customer table.

So I think you should first try to save the customer entity with a null reference to the contact row, then try to delete that contact.

You may perform the previous tasks in a transaction, or by using a Unit Of Work.

Aidan
User
Posts: 8
Joined: 20-Mar-2006
# Posted on: 30-Mar-2006 16:20:30   

Walaa wrote:

oCustomer.Contact.Delete();

I think when the above line is excuted, it tries to delete the contact row from the database, when nothing has been done to remove the row(s) that reference it from the Customer table.

So I think you should first try to save the customer entity with a null reference to the contact row, then try to delete that contact.

You may perform the previous tasks in a transaction, or by using a Unit Of Work.

This seems like a limitation in the software , is there a reason that the delete functionality was implemented in this way or is this something that might be implemented in future releases of the software ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39930
Joined: 17-Aug-2003
# Posted on: 30-Mar-2006 16:39:57   

Aidan wrote:

Walaa wrote:

oCustomer.Contact.Delete();

I think when the above line is excuted, it tries to delete the contact row from the database, when nothing has been done to remove the row(s) that reference it from the Customer table.

So I think you should first try to save the customer entity with a null reference to the contact row, then try to delete that contact.

You may perform the previous tasks in a transaction, or by using a Unit Of Work.

This seems like a limitation in the software , is there a reason that the delete functionality was implemented in this way or is this something that might be implemented in future releases of the software ?

You mean, why isn't there a recursive save or FK traversal save action? Well, because that would be too time consuming to execute: all related entities first have to be fetched into memory and then be deleted. it's not possible to determine which entities to delete by just looking at a single entity in memory. It's not the same as a recursive save, because even if you have a couple of entities in memory, it's still possible that an entity in the db will create an FK violation.

Say you have 10 customers you want to delete, which have each 100 orders with each 10 order lines. To delete all these entities, I would have to start with the order lines. In theory, I could write a query which has a couple of subqueries which filters on those 10 customers, their orders and then delete the order details with this filter. However, this fails if I have multiple paths from entity A to entity E (A<-B<-C-E and A<-B<-D<-E), in which case SqlServer sometimes gives up as well when you specify cascading deletes with the remark that it isn't possible.

A second reason is that cascading deletes are very intrusive and delete entites implicitly, and llblgen pro is designed to not do things implicitly when it comes to deletes, as you can't roll things back when the transaction commits as in: when a graph has been removed and you didn't like that after all, you have to restore a backup, hence the requirement to do explicit deletes.

Frans Bouma | Lead developer LLBLGen Pro