none
Linq, Two Contexts, One Transaction RRS feed

  • Question

  • Hi.

    I have a huge database and would like to split the database in several data contexts.

    The problem I'm expecting is about transactions when inserting in two tables in two different context. 
    How could I perform this?

    The situation is: I insert data in table A and B that are in different data contexts. But I would like them to work like a single transaction.

    All help is appreciated. 

    Best Regards.

    Francisco Correia
    Sunday, April 26, 2009 2:14 PM

Answers

  • Wrap your operations in a System.Transactions.TransactionScope and they will be part of the same transaction:

    using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
    {
        using (SomeDataContext dc = new SomeDataContext())
        {
    	dc.InsertOnSubmit...
    	dc.SubmitChanges();
        }
    
        using SomeOtherDataContext dc = new SomeOtherDataContext())
        {
    	dc.InsertOnSubmit...
    	dc.SubmitChanges();
        }
    
        ts.Complete();
    }


    If the operations within the transaction scope occur on different connections (which they most likely will in the example above), the second operation will escalate the transaction to a MSDTC/XA transaction. If you don't want to use MSDTC or if you want to avoid the overhead of a distributed transaction you can avoid this escalation by providing a connection to both data contexts (if they're both pointing to the same database).


    Kristofer - Huagati Systems Co., Ltd. - www.huagati.com - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools
    Monday, April 27, 2009 2:38 AM
    Answerer

All replies

  • Hi, I think theres no error will result in that situation. try to insert in the old way. Try to think that your B is a table not a DB.
    Hope This One Can Help. Thanks To Be Happy Is To Be YourSelf Please remember to mark my reply as answers if it's the answer to your question.
    Monday, April 27, 2009 2:14 AM
  • Wrap your operations in a System.Transactions.TransactionScope and they will be part of the same transaction:

    using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
    {
        using (SomeDataContext dc = new SomeDataContext())
        {
    	dc.InsertOnSubmit...
    	dc.SubmitChanges();
        }
    
        using SomeOtherDataContext dc = new SomeOtherDataContext())
        {
    	dc.InsertOnSubmit...
    	dc.SubmitChanges();
        }
    
        ts.Complete();
    }


    If the operations within the transaction scope occur on different connections (which they most likely will in the example above), the second operation will escalate the transaction to a MSDTC/XA transaction. If you don't want to use MSDTC or if you want to avoid the overhead of a distributed transaction you can avoid this escalation by providing a connection to both data contexts (if they're both pointing to the same database).


    Kristofer - Huagati Systems Co., Ltd. - www.huagati.com - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools
    Monday, April 27, 2009 2:38 AM
    Answerer