c# - Data modified show old value when called after update -
in app have method updates values of item this:
foreach (var orderdetailinfo in _order.mlistorderdetail) { orderdetailinfo info = orderdetailinfo; var inventoryqry = in db.inventory i.invent_ide == info.minventoryid select i; inventory originalitem = inventoryqry.single(); inventory itemtomodify = originalitem; itemtomodify.invent_nb_in_stock = (orderdetailinfo.morderdetailquantity + originalitem.invent_nb_in_stock); db.entry(originalitem).currentvalues.setvalues(itemtomodify); db.savechanges(); }
now if in table @ originalitem
, value updated. before being updated, value 4, , order
updated quantity 5.
my problem when load data after upper code has run (so, after update invent_nb_in_stock
column):
var inventoryqry = inv in db.inventory inv.invent_ide == inventide select inv; return inventoryqry.single();
the item shows the last value holded before building, , not current value. in case, value 4, not 5. why? can me figure out problem?
since might because of huge method make, posting here entire method contains chunk of code displayed here:
internal void validatesupplierorder(orderinfo _order, list<orderdetailinfo> _listoldorderdetails) { using (blueberry_mtgentities db = new blueberry_mtgentities()) { using (transactionscope scope = transactionutils.createtransactionscope()) { try { orders ordertomodify = dataconverter.orderinfotoorderdata(_order); var orderqry = o in db.orders o.order_ide == _order.morderid select o; orders originalorder = orderqry.single(); // first step: need remove old order details no more related // order adding. foreach (var orderdetail in _listoldorderdetails) { orderdetailinfo detail = orderdetail; var orderdetailqry = od in db.order_detail od.order_detail_ide == detail.morderdetailid select od; order_detail itemtoremove = orderdetailqry.singleordefault(); db.order_detail.remove(itemtoremove); db.savechanges(); } foreach (order_detail orderdetailtocreate in _order.mlistorderdetail.select(dataconverter.orderdetailinfotoorderdetaildata)) { db.order_detail.add(orderdetailtocreate); db.savechanges(); } foreach (var orderdetailinfo in _order.mlistorderdetail) { orderdetailinfo info = orderdetailinfo; var inventoryqry = in db.inventory i.invent_ide == info.minventoryid select i; inventory originalitem = inventoryqry.single(); inventory itemtomodify = originalitem; itemtomodify.invent_nb_in_stock = (orderdetailinfo.morderdetailquantity + originalitem.invent_nb_in_stock); db.entry(originalitem).currentvalues.setvalues(itemtomodify); db.savechanges(); } db.entry(originalorder).currentvalues.setvalues(ordertomodify); db.savechanges(); } catch(exception ex) { throw new exception(ex.message); } scope.complete(); } } }
well, found out issue, due to... own missing knowledge!
let's take these lines of codes:
foreach (var orderdetailinfo in _order.mlistorderdetail) { orderdetailinfo info = orderdetailinfo; var inventoryqry = in db.inventory i.invent_ide == info.minventoryid select i; inventory originalitem = inventoryqry.single(); inventory itemtomodify = originalitem; itemtomodify.invent_nb_in_stock = (orderdetailinfo.morderdetailquantity + originalitem.invent_nb_in_stock); db.entry(originalitem).currentvalues.setvalues(itemtomodify); db.savechanges(); }
my guess (and is) somehow pattern use modify value not one. maybe because copying object instead of creating new 1 , setting actual values needed.
so did this:
foreach (var orderdetailinfo in _order.mlistorderdetail) { orderdetailinfo info = orderdetailinfo; var inventoryqry = in db.inventory i.invent_ide == info.minventoryid select i; inventory originalitem = inventoryqry.single(); int newstock = (int)(info.morderdetailquantity + originalitem.invent_nb_in_stock); inventory itemtomodify = new inventory { card_ide = originalitem.card_ide, invent_ide = originalitem.invent_ide, invent_item_state = originalitem.invent_item_state, invent_nb_in_stock = newstock, invent_nb_qt_adjust = 0, invent_nb_qt_ebay_auction = originalitem.invent_nb_qt_ebay_auction, invent_nb_qt_ebay_store = originalitem.invent_nb_qt_ebay_store, invent_nb_qt_finep = originalitem.invent_nb_qt_finep, invent_nb_qt_send = 0, invent_nb_qt_sold = originalitem.invent_nb_qt_sold }; db.entry(originalitem).currentvalues.setvalues(itemtomodify); db.entry(originalitem).state = entitystate.modified; db.savechanges(); }
and although not seems elegant, works!
Comments
Post a Comment