c# - DBTransaction closing when used for another method -


i trying use single transaction multiple different insert/update statements in entity framework repository, whenever pass transaction different method returned closed, see below -

objectcontext objectcontext = ((iobjectcontextadapter)context).objectcontext; objectcontext.connection.open(); dbtransaction transaction = objectcontext.connection.begintransaction();  using (transaction) {       ipersonrepository personrepository = new personrepository();       context.entry(person).state = system.data.entitystate.modified;       personrepository.update(person, objectretrieveddatetime, transaction, objectcontext);        if (existingstatus != null)       {            objectcontext.createobjectset<tblpersonstatus>().attach(existingstatus);            existingstatus.enddate = datetime.now;            context.entry(existingstatus).state = system.data.entitystate.modified;             ipersonstatusesrepository repository = new personstatusesrepository();            repository.update(existingstatus, objectretrieveddatetime, transaction, objectcontext);       } } 

by time 1st update method finished (personrepository.update), transaction has error of "base {system.systemexception} = {"this sqltransaction has completed; no longer usable."}"

is there way around this?

edit - update method called looks -

public virtual void update(t entity, datetime? objectretrieveddatetime, dbtransaction transaction, objectcontext objectcontext)     {         if (entity == null)         {             throw new argumentexception("cannot update null entity.");         }          using (transaction)         {             objectstateentry entry = objectcontext.objectstatemanager.getobjectstateentry(entity);              string entityname = entity.gettype().name;              if (!objectretrieveddatetime.hasvalue || !this.auditsafterretieval(objectretrieveddatetime, entityname, entity))             {                 dictionary<string, object> oldvalues = new dictionary<string, object>();                 dictionary<string, object> newvalues = new dictionary<string, object>();                  bool changed = this.entityhaschanged(entity, entry, out oldvalues, out newvalues);                  // check changes before saving                 if (changed)                 {                     this.context.savechanges();                     this.audit(entity, entityname, "update", oldvalues, newvalues, false, null);                 }             }             else             {                 throw new exception("object cannot saved has been amended in thread");             }         }     } 

just remove using statement inside update method

you disposing transaction inside using

this quick stub how should use transaction

using(dbconnection connection = ...)     {         connection.open();         using(dbtransaction transaction = connection.begintransaction(...))         {       try{         ... update ...         ... update ...          transaction.commit();        }catch(exception){        // transaction rolled here if exception thrown before call commit()         transaction.rollback()       }        }      } // connection closed here 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -