asp.net mvc - Update operation Issue in NopCommerce? -
i using nopcommerce. have created new table named "productviewdetails". have tried insert/update record in table. when tried insert new record in table,new record inserted successfully. when tried update operation ,method "updatedata" executed record did not update.
code :
public void incremementviews(int productid) { productviewdetails productdetails = new productviewdetails(); productdetails.id = 5; productdetails.productid = 1; productdetails.numberofviews = 10; updatedata(productdetails); } public void updatedata(productviewdetails productdetails) { _productviewdetails.update(productdetails); }
update method code :
public partial class efrepository<t> : irepository<t> t : baseentity { private readonly idbcontext _context; private idbset<t> _entities; public void update(t entity) { try { if (entity == null) throw new argumentnullexception("entity"); this._context.savechanges(); } catch (dbentityvalidationexception dbex) { var msg = string.empty; foreach (var validationerrors in dbex.entityvalidationerrors) foreach (var validationerror in validationerrors.validationerrors) msg += environment.newline + string.format("property: {0} error: {1}", validationerror.propertyname, validationerror.errormessage); var fail = new exception(msg, dbex); //debug.writeline(fail.message, fail); throw fail; } } }
what problem?
nopcommerce use entity framework lazy loading , proxied entities tracking changes, when instantiate new entity (as in new productviewdetails()) entity framework context know nothing entity , context can´t track changes.
- if entity exists repository getbyid method instead of instantiating it, context return , attached , proxied entity context tack every change , when call update changes save.
- if entity new 1 should call add method of repository.
Comments
Post a Comment