none
DbContextTransaction EF 6 RRS feed

  • Question

  • Hello.
    I am using DbContextTransaction class like as:
    var transaction = context.Database.BeginTransaction();

    DbContextTransaction class have methods: Commit, Rollback, Dispose.
    If I will use Commit method, transaction will disposed automatically after it? Or i must call Dispose by myself? And if i will not call Dispose - transaction "will be" on database?
    Thank you.
    • Edited by ArtsiomRusak Wednesday, December 18, 2013 2:40 PM
    Wednesday, December 18, 2013 2:35 PM

Answers

  • Hello Artyom,

    >>If I will use Commit method, transaction will disposed automatically after it? Or i must call Dispose by myself?

    Please see this blog and search for “Combining several operations into one transaction within the same context”, then we can see a description as below:

    The DbContextTransaction is meant to be disposed once it has been committed or rolled back. One easy way to accomplish this is the using(…) {…} syntax which will automatically call Dispose() when the using block completes.

    So if we use the using syntax, we do not need, if we not use the using syntax, we have to call the Dispose() method by ourselves.

    >>And if I will not call Dispose - transaction "will be" on database?

    After we call the Commit(), the transaction has be over and is not on database any more. The purpose we call the Dispose() is to dispose the transaction object.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by ArtsiomRusak Thursday, December 19, 2013 7:14 PM
    Thursday, December 19, 2013 3:03 AM
    Moderator