asp.net - Linq-To-Sql SubmitChanges Not Updating Database -


i've read multiple questions similar 1 none situation.

using linq-to-sql insert new record , submit changes. then, in same web request, pull same record, , update it, submit changes. changes not saved. databasecontext same across both these operations.

insert:

var transaction = _factory.createtransaction(siteid, userid, questionid, type, amount, transactionid, processor);  using (iunitofwork unitofwork = unitofwork.begin()) {     transaction.amount = amount;      _transactionrepository.add(transaction);      unitofwork.commit(); } 

select , update:

itransaction transaction = _transactionrepository.findbyid(transactionid);  if (transaction == null) throw new exception(constants.errorcannotfindtransactionwithid.formatwith(transactionid));  using (iunitofwork unitofwork = unitofwork.begin()) {     transaction.crmid = crmid;      transaction.updatedat = systemtime.now();      unitofwork.commit(); } 

here's unit of work code:

public virtual void commit() {     if (_isdisposed)     {         throw new objectdisposedexception(gettype().name);     }      _database.submitchanges(); } 

i went designer.cs file , put breakpoint on field being set not updated. stepped through , entered , execute set code, entity should getting "notified" of change field:

public string crmid {         {         return this._crmid;     }     set     {         if ((this._crmid != value))         {             this.oncrmidchanging(value);             this.sendpropertychanging();             this._crmid = value;             this.sendpropertychanged("crmid");             this.oncrmidchanged();         }     } } 

other useful information:

  • objecttracking enabled
  • no errors or exceptions when second submitchanges called (just silently fails update)
  • sql profiler shows insert , select not subsequent update statement. linq-to-sql not generating update statement.
  • there 1 database, 1 database string, update not going database
  • the table has primary key.

i don't know cause linq-to-sql not issue update command , not raise kind of error. perhaps problem stems using same datacontext instance? i've refreshed object database using datacontact.refresh method before pulled update, didn't help.

i have found root cause. using unity. initial insert being performed in service class perwebrequest lifetime. select , update happening in class singleton lifetime. assumption datacontext instances same incorrect.

so, in class singleton lifetime, fresh instance of database repository , perform update , no problem.

now still don't know why original code didn't work , approach still considered more workaround solution, did solve problem , useful others.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -