CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

Update many child objects in one transaction

Last post 05-17-2008, 1:28 AM by tchimev. 3 replies.
Sort Posts: Previous Next
  •  05-16-2008, 6:58 AM 23619

    Update many child objects in one transaction

    I'm using CSLA .net and Linq To SQL.
    I have a ProductList : BusinessListBase<ProductList, Product>
    I changed the values of two Products form the list.

    using (System.Transactions.TransactionScope ts = new  System.Transactions.TransactionScope())
    {
              DataPortal.Update<ProductList>(pList);
              ts.Complete();
    }

    I use the code above to save the changes into DB.

    Here is the DataPortal method from the Product class:
     private void Child_Update()
    {
           using (var ctx = ContextManager<LinqDataContext>.GetManager("DBname"))
          {
                    ctx.DataContext.Products.Attach(this.MapToLinq(), this.IsDirty);
                    ctx.DataContext.SubmitChanges();
          }
    }

    I expected to save it in one transaction but I got an error it requires a Distributed transaction.
    Is there a way to save changes in one local transaction?
    Without transaction everything works fine.


  •  05-16-2008, 7:15 AM 23620 in reply to 23619

    Re: Update many child objects in one transaction

    The only time you're creating a connection is in the child update.  Since no connection exists, one is created.  At the end of the using block, since only one object was using the connection, it's closed.  Loop around again, and the same thing happens.

    Leave the code in your child alone, but change the parents data portal code to this:

    using (var ctx = ContextManager<LinqDataContext>.GetManager("DBname"))
          {
        DataPortal.Update<ProductList>(pList);
    }

    This will create the connection.  The ContextManager in the CHILD object will pick up that a connection already exists, and use that.  It also won't close the connection because the parent still has a reference to it.

    HTH
    Andy
  •  05-16-2008, 7:18 AM 23621 in reply to 23619

    Re: Update many child objects in one transaction

    Also, there's no need to manually use the TransactionScope.  You can use the Transactional attribute with an TransactionScope value to do the same thing, without requiring you to code it.
  •  05-17-2008, 1:28 AM 23634 in reply to 23621

    Re: Update many child objects in one transaction

    Thank you for your answer.
View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems