c# - How to correctly update entity? -


hello have next code update entity in data base:

 public async void updateproductfrommodel(producteditmodel model)         {             var res = await getproduct(model.goodid);                        mapper.map<producteditmodel, goods>(model, res);             updateproduct(res);         }          public void updateproduct(goods product)         {             try             {                 using (var dbctx = new smartbags_storeentities())                 {                     dbctx.entry(product).state = entitystate.modified;                     dbctx.savechanges();                 }             }             catch (exception ex)             {                  throw ex;             }          }   public async task<goods> getproduct(int id)         {             try             {                 var dbctx = new smartbags_storeentities();                  return await dbctx.goods.firstordefaultasync(d => d.goodid == id);              }             catch (exception ex)             {                  throw ex;             }         } 

but getting error an entity object cannot referenced multiple instances of ientitychangetracker. should updated object ? new commer in entityframework 6 thanks.

update @o.o create , update entity. code working. can see create , update different context.

public bool savenewproduct(producteditmodel model)         {             var prices = model.prices;             var g = new goods                         {                             article = model.article,                             categoryid = model.categoryid,                             code = model.code,                             description = model.description,                             name = model.name,                             isexclusive = model.isexclusive,                             isnew = model.isnew,                             isvisible = model.isvisible,                          };               addnewproducttodb(g);              //update prices             if (prices.any(d => d.isactive) && g.prices1.any() && prices.count() > 1)             {                 var pr = prices.firstordefault(d => d.isactive);                 g.priceid = g.prices1.first().priceid;             }              updateproduct(g);             return true;         } public int? addnewproducttodb(goods product)         {             try             {                 using (var dbctx = new smartbags_storeentities())                 {                     //add standard entity standards entityset                     dbctx.goods.add(product);                     //save whole entity graph database                     dbctx.savechanges();                     return product.goodid;                 }             }             catch (exception ex)             {                  throw;             }             return null;         } 

you need use same context getproduct , updateproduct calls. when called getproduct, object got belonged 1 context , when called updateproduct created different context , tried use product still assigned getproduct.

one option pass in context:

var dbctx = new smartbags_storeentities() var res = await getproduct(model.goodid, dbctx);            mapper.map<producteditmodel, goods>(model, res); updateproduct(res, dbctx); 

for edit:

your g variable never has context, why not error.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -