Linq to Sql - Order of operations

Linq to Sql - Order of operations

Old forum URL:

ajj3085 posted on Wednesday, March 24, 2010


I have a Csla BusinessListBase, which is a child of another BO.

The problem I have is this; users can add items to the list, which ties a produt to the price list (the price list is the parent BO, if that matters).

The problem is that a user can add a product which is already on the list.  Not a huge deal, I have business rules that prevent saving.

But if the user chooses to delete the BO which already exists in the database and keep the "new" BO which links the same product to the same price list, Linq To Sql attempts to do the insert of the new object first, which of course causes a database constraint.

Any ideas?



JoeFallon1 replied on Wednesday, March 24, 2010

I am curious as to why the Insert happens first.

As I recall the code for a BBL loops over the items in the Deleted list first so the object should be removed form the database prior to the insert. Therefore the insert should work.

1. Would a transaction affect the above logic?

2. Do you have any custom code which changes the above logic?




ajj3085 replied on Wednesday, March 24, 2010

I'm curious as well.  The BLB does fire the DeleteSelf methods first, then the Insert Updates. Its linq that is executing the insert before the delete when SubmitChanges is called.

The statements are already executing within a transaction, and there's nothing in the BLB class that alters Csla's behavior.


ajj3085 replied on Wednesday, March 24, 2010

I came up with this as a workaround:

   protected override void Child_Update( params object[] parameters ) {
   var listChanged = RaiseListChangedEvents;
   RaiseListChangedEvents = false;

   try {
    foreach ( var child in DeletedList ) {
     DataPortal.UpdateChild( child, parameters );


    using ( var mgr = ContextManager<MyDataContext>.GetManager() ) {
   finally {
    RaiseListChangedEvents = listChanged;

   base.Child_Update( parameters );

Can't say I'm thrilled with it though.

tmg4340 replied on Wednesday, March 24, 2010

I may be mis-reading this, but digging around in the DataContext code using Reflector seems to indicate that when developing the list of objects to process, the list is sorted so that inserts are processed, then updates, then deletes.


- Scott

ajj3085 replied on Wednesday, March 24, 2010

Digging around online seems to back this up, as appearently others have had a similar issue.  Seems kind of brain dead.

Copyright (c) Marimer LLC