locked
How to manage transactions with EF 4.1 (and DbContext)? RRS feed

  • Question

  • Hi,

    We are upgrading to EF 4.1 and would like to use DbContext instead of ObjectContext.  But, we are wondering how to implement explicit transaction using DbContext.

    This page (http://msdn.microsoft.com/en-us/library/bb738523.aspx) explains how to do it with ObjectContext, but since DbContext doesn't have an AcceptAllChanges method, we are wondering if it is possible to implement transaction using only DbContext or we must have a reference to ObjectContext as well.


    We'd like to have some guidelines about this or an example.

    Thank you very much
    Phil

    Tuesday, May 10, 2011 4:11 PM

Answers

  • Thanks.

     

    Yes, we would like to support explicit transaction in our Entity Framework usage.

    As a workaround, we now cast the DbContext into an IObjectContextAdapter to get access to the underlying ObjectContext.

    That way, we are able to replicate the example page (http://msdn.microsoft.com/en-us/library/bb738523.aspx) for our usage.

    Still, we'd like to know the recommeded way to manage explicit transaction in EF 4.1

     

    Thanks again

    Phil

    • Proposed as answer by Jackie-Sun Tuesday, May 17, 2011 5:34 AM
    • Marked as answer by Jackie-Sun Tuesday, May 31, 2011 2:47 AM
    Wednesday, May 11, 2011 5:54 PM

All replies

  • Part of the answer will be that DbContext.SaveChanges() is automatically transactional, so in many cases, you will not need to roll your own transactions. 

    If you decide you need them, wrap SaveChanges in a transaction:

     

    using (TransactionScope transaction = new TransactionScope())    

    {

    context.SaveChanges();    

    transaction.Complete();

    }

    if you not call transaction.Complete() the changes will be rolled back

    • Proposed as answer by Pcnova (Old) Thursday, December 13, 2012 4:55 PM
    Wednesday, May 11, 2011 5:45 PM
  • Thanks.

     

    Yes, we would like to support explicit transaction in our Entity Framework usage.

    As a workaround, we now cast the DbContext into an IObjectContextAdapter to get access to the underlying ObjectContext.

    That way, we are able to replicate the example page (http://msdn.microsoft.com/en-us/library/bb738523.aspx) for our usage.

    Still, we'd like to know the recommeded way to manage explicit transaction in EF 4.1

     

    Thanks again

    Phil

    • Proposed as answer by Jackie-Sun Tuesday, May 17, 2011 5:34 AM
    • Marked as answer by Jackie-Sun Tuesday, May 31, 2011 2:47 AM
    Wednesday, May 11, 2011 5:54 PM
  • Hi jesuissur,

    Happy to see that you have resolved your issue, and thanks for sharing your solution to us.

    For managing explicit transaction in EF 4.1, I think that's no difference from it's in EF 4. If you have any questions, please feel free to let me know.

     

    have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 31, 2011 2:47 AM
  • Hi all,

    may I revive this thread? I am currently looking for the "official/recommended" way of using transactions in EF 4.1, too. Although I had some feeling, this place is the first to tell me that

    Part of the answer will be that DbContext.SaveChanges() is automatically transactional, so in many cases, you will not need to roll your own transactions.

    However, I have some questions concerning this:

    • What does "is automatically transactional" mean more concretely?
    • If that is the case, why would we need to be able to do the transactions explicitely using TransactionScope?
    • If "SaveChanges()" is transactional, and say, it fails due to a validation error or a database error. Will the state of the changed entities in the DbContext be automatically reverted? If not, how would we roll back the transaction using DbContext without "explicit transaction management" in case SaveChanges fails?

    Thank you in advance,

    darkroastjava


    Wednesday, August 17, 2011 7:40 AM