DELETE in transaction not done in transaction

Posts   
 
    
Asimov
User
Posts: 113
Joined: 05-Dec-2003
# Posted on: 24-Jan-2005 20:38:18   

in the following code

_Evenement = new EvenementEntity(Ident);

transaction.Add(_Evenement);
transaction.Add(_Evenement.EvenementEquipes);
transaction.Add(_Evenement.PublicationEvenements);

_Evenement.EvenementEquipes.DeleteMulti();
_Evenement.PublicationEvenements.DeleteMulti();

succeed = _Evenement.Delete();

transaction.Commit();

  1. am I right to put the children collections in the transaction if the parent entity is already in the transaction?
  2. I get an error on the line ** succeed = _Evenement.Delete();** about foreign key constraint. It says I can't delete the entity referenced by _Evenement because it would violate the foreign key constraint in _Evenement.PublicationEvenements (the table that links publications Evenement and Publication in a nxn relationship. But I don't understand why the error occurs at all (because I delete the content from the linkage table before deleting the entity, and why the error occurs on the Delete(); instead of the line with Commit() (when the sql is executed) ? It's as if the _Evenement wasn't in the transaction, but on the line where the Delete is done on _Evenement, the property ParticipatesInTransaciton is true. Any idea? Thanks!
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 25-Jan-2005 10:45:53   

Asimov wrote:

in the following code

_Evenement = new EvenementEntity(Ident);
transaction.Add(_Evenement);
transaction.Add(_Evenement.EvenementEquipes);
transaction.Add(_Evenement.PublicationEvenements);
_Evenement.EvenementEquipes.DeleteMulti();
_Evenement.PublicationEvenements.DeleteMulti();
succeed = _Evenement.Delete();
transaction.Commit();

  1. am I right to put the children collections in the transaction if the parent entity is already in the transaction?

Yes, as Delete actions aren't recursive.

  1. I get an error on the line ** succeed = _Evenement.Delete();** about foreign key constraint. It says I can't delete the entity referenced by _Evenement because it would violate the foreign key constraint in _Evenement.PublicationEvenements (the table that links publications Evenement and Publication in a nxn relationship. But I don't understand why the error occurs at all (because I delete the content from the linkage table before deleting the entity, and why the error occurs on the Delete(); instead of the line with Commit() (when the sql is executed) ?

Deletes are executed when you call them. You confuse UnitOfWork behavior with this I think. The Commit() is for finalizing the transaction, so the RDBMS will commit the transaction, however the commands are already executed. So you have to first delete the FK side, then the PK side.

Frans Bouma | Lead developer LLBLGen Pro
Asimov
User
Posts: 113
Joined: 05-Dec-2003
# Posted on: 25-Jan-2005 20:53:58   

but that's what I do, no? using the DeleteMulti I delete the foreign keys, then I delete the primary key entity. Yeah I think you're right and I confused the transaction with something else wink It makes senses that everything is done and then it is just confirmed. But it should be ok if I delete the FK side using DeleteMulti and then the PK using Delete? I tried something, instead of deleting the FK side using DeleteMulti from the PK entity, I declared a collection of the FK side entities that have the foreign key of the entity I want to delete, and then I called deletemulti on the collection using the filter, and this way it worked? What's the difference between the two?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 26-Jan-2005 10:58:15   

if Evenement has other entities pointing with their FK columns to that Evenement, you have to delete these first, as it would otherwise break the FK constraints. Like removing Customer first and Order second.

Frans Bouma | Lead developer LLBLGen Pro