How to use transactions with a datacontext

前端 未结 5 1497
清歌不尽
清歌不尽 2020-12-08 21:49

Can I use transactions with a datacontext, so that I can rollback the state of the context after an error? And if so, how does that work?

5条回答
  •  臣服心动
    2020-12-08 22:42

    I use them in testing all the time :)

    try
    {
      dc.Connection.Open();
      dc.Transaction = dc.Connection.BeginTransaction();
    
      dc.SubmitChanges();
    }
    finally
    {
      dc.Transaction.Rollback();
    }
    

    UPDATE

    This will ALWAYS rollback after the fact. I use this in testing.

提交回复
热议问题