How to use transactions with a datacontext

前端 未结 5 1502
清歌不尽
清歌不尽 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:40

    Something like this, probably:

    try
    {
        using (TransactionScope scope = new TransactionScope())
        {
            //Do some stuff
    
            //Submit changes, use ConflictMode to specify what to do
            context.SubmitChanges(ConflictMode.ContinueOnConflict);
    
            scope.Complete();
        }
    }
    catch (ChangeConflictException cce)
    {
            //Exception, as the scope was not completed it will rollback
    }
    

提交回复
热议问题