none
Saving changes inside a non-completed TransactionScope RRS feed

  • Question

  • Hi,

    I have a code that does something like this

     

    using (TransactionScope scope = new TransactionScope())
    {
     using (MyEntities entities = new MyEntities())
     {
     IMyManager myMgr = ManagersFactory.GetMyManager();
     if (myMgr.DoSomethingImportant())
     {
      ...
      myMgr.DoAnotherThingThatCouldFail(param1, param2, param3);
      ...
      scope.Complete();
     }
     }
    }
    


    but... it's really important that myMgr.DoSomethingImportant() logs into the database EVEN if the transaction is not completed...

    I tried doing this at DoSomethingImportant()

    EntityConnection conn = new EntityConnection("name=ConnectionName");
    using (MPCEntities entities = new MPCEntities(conn))
    {
     ImportantLogEntry log = new ImportantLogEntry();
     log.Data = "something happened";
     entities.AddToLogEntries(log);
     entities.SaveChanges();
    }
    
    

     


    without luck... I don't know what else to try... I don't want to have to take into account if a given operation logs or not before including it into a transaction scope... I may even want to have operations that log some things and have other things that aren't commited unless the transaction is completed. I'm pretty sure that there's some way of doing this by creating a context not attached to any transactions cope, but can't find it anywhere.

    Thanks in advance!!!

     



    Tuesday, July 12, 2011 9:15 PM

Answers

  • Hi,

    Since you are in a TransactionScope every connection you create within it will use that TransactionScope automatically. However, to avoid this you should create a new TransactionScope around the specific code you want to work in a seperate TransactionScope. The important thing is to call this with TransactionScopeOption.Supress which tells the connections that enters this scope that anything that happens within this scope should be ignored.

    Hope this helps! 


    --Rune
    Wednesday, July 13, 2011 7:26 AM

All replies

  • Hi,

    Since you are in a TransactionScope every connection you create within it will use that TransactionScope automatically. However, to avoid this you should create a new TransactionScope around the specific code you want to work in a seperate TransactionScope. The important thing is to call this with TransactionScopeOption.Supress which tells the connections that enters this scope that anything that happens within this scope should be ignored.

    Hope this helps! 


    --Rune
    Wednesday, July 13, 2011 7:26 AM
  • Thanks!!! works like a charm
    Wednesday, July 13, 2011 1:02 PM