c# - Ignore TransactionScope for specific query -
i'm looking way execute query while transactionscope alive, , ignore transactionscope - basically, want execute particular query no matter what.
i'm using ef code-first, , way application designed, new data context opened many times throughout single call, each own changes, , of contained within single transactionscope, has complete()
called @ end assuming no failures. inside of context we've overridden savechanges
if exception happens on base.savechanges()
, can catch , log database before rolling transaction.
since savechanges
happens inside transaction, logging doesn't happen, because belongs same transaction original call. i'm trying ignore transactionscope altogether logging code.
here's stripped-down code:
// context public override int savechanges() { try { return base.savechanges(); } catch (exception ex) { // writes log table - want run no matter logrepo.log(/*stuff log context*/); throw; } } // inside business logic public void dosomething() { try { using (var scope = new transactionscope()) { using (var context = new foocontext()) { // context.savechanges(); } using (var context = new foocontext()) { // else context.savechanges(); } scope.complete(); } } catch (exception ex) { // scope.complete never called, transaction rolled } }
i tried using regular ado.net instead of ef logging, still same results - gets rolled too.
i need error handling happen inside of savechanges
, because i'm logging state of entities being saved - can't move logging somewhere else. build message while inside savechanges catch
, , throw , let dosomething catch
log it, there dozens of dosomething
methods, , i'd rather deal in 1 place.
if wrap log call inside of transaction scope suppress option enabled, transaction scope not used.
public override int savechanges() { try { return base.savechanges(); } catch (exception ex) { string message = /*stuff log context*/; using (var scope = new transactionscope(transactionscopeoption.suppress)) { logrepo.log(msg); } throw; }
}
Comments
Post a Comment