json - PUT action on WebApi Controller not saving data to database -


i have simple mvc5 webapi project following models:

public class product {     public product()     {     }      public int productid { get; set; }     public string productcode { get; set; }      public int bomid { get; set; }     public virtual bom bom { get; set; } }  public class bom {     public bom()     {         products = new list<product>();     }      public int bomid { get; set; }     public string description { get; set; }     public int counter { get; set; }      public virtual list<product> products { get; set; } }  public class appdbcontext : dbcontext {     public dbset<product> products { get; set; }     public dbset<bom> boms { get; set; } } 

here put action (the scaffolded action):

public ihttpactionresult putproduct(int id, product product) {     if (!modelstate.isvalid)     {         return badrequest(modelstate);     }      if (id != product.productid)     {         return badrequest();     }      db.entry(product).state = entitystate.modified;      try     {         db.savechanges();     }     catch (dbupdateconcurrencyexception)     {         if (!productexists(id))         {             return notfound();         }         else         {             throw;         }     }      return statuscode(httpstatuscode.nocontent); } 

when code executes db.savechanges(); code executes expected database doesn't updated.

here code in console application triggers put request:

static async task updateproduct(int id)         {             using (var client = new httpclient())             {                 client.baseaddress = new uri("http://localhost.fiddler:49195/");                 client.defaultrequestheaders.accept.clear();                 client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));                  // new code:                 httpresponsemessage response = await client.getasync("api/products/" + id.tostring());                 if (response.issuccessstatuscode)                 {                     var json = await response.content.readasstringasync();                     product product = jsonconvert.deserializeobject<product>(json);                     product.bom.counter += 1;                      json = jsonconvert.serializeobject(product);                     var httpcontent = new stringcontent(json, encoding.utf8, "application/json");                     response = await client.putasync("api/products/" + id.tostring(), httpcontent);                     console.writeline(response.issuccessstatuscode.tostring() + ": counter = " + product.bom.counter);                 }             }         } 

what method product requested (1), updates field in child (bom.counter) incrementing value on field.

it's database not update when application starts again, we're getting old value , not updated one. savechanges method seems execute database doesn't suggest has.

here , put json strings put method per fiddler2:

get json put json

note: have these lines of code in global.asax file json handles models correctly:

globalconfiguration.configuration.formatters.jsonformatter.serializersettings.referenceloophandling = newtonsoft.json.referenceloophandling.ignore; globalconfiguration.configuration.formatters.remove(globalconfiguration.configuration.formatters.xmlformatter); 

i'm hoping can see i'm going wrong. appreciated.

thanks in advance,

paul.

look @ 2 lines of code:

  product product = jsonconvert.deserializeobject<product>(json);   product.bom.counter += 1; 

your problem because have parent entity child entity, setting parent's entity state modified changed child entity, left child's entity state unchanged (entity framework apply modified state on single entity not "added" applied on whole graph), on server side if set bom entity state modified, changes database.

now suggested not permanent solution, show problem , how solve temporary.

to solve permanently , because client disconnected graph, client needs track objects , being responsible of happened graph entities (added, modified, deleted, or unchanged).

you need implement interface iobjectstate , let clients change state of disconnected graph , on server convert state entity state in before save entity.

look @ answer here detailed answer.

hope helps.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -