c# - Creating a new record resulting in creating identic new record in related table -
each time create new purchasedproduct
, , refer product
it, upon insertion table, table creates new identical product
, refer new one, instead of refering existing one.
so, have these 3 relevant tables :
..which means purchase
can have many purchasedproduct
, each represent purchased product
, how many of purchased (quantity).
these relevant codes :
public partial class fmaddeditpurchase : form { list<product> products; purchase purchase; public fmaddeditpurchase() { initializecomponent(); purchase = new purchase(); text = "add new purchase"; dtpdate.value = datetime.now.date; refreshpurchasedproduct(); loadproductlist(); } private void refreshpurchasedproduct() { list<purchasedproduct> ppquery = new list<purchasedproduct>(); bindingsource bi = new bindingsource(); if (purchase.purchasedproducts.count > 0) { using (var context = new dbkrunchworkcontext()) { bi.datasource = purchase.purchasedproducts. join(products, x => x.product, y => y, (x, y) => new { y.product_name, x.price, x.quantity }). tolist(); } } dgvpurchasedproduct.datasource = bi; dgvpurchasedproduct.refresh(); } private void loadproductlist() { using (var context = new dbkrunchworkcontext()) { products = context.products.tolist(); } cbproductname.datasource = products. select(x => x.product_name).tolist(); } private void btaddproduct_click(object sender, eventargs e) { decimal price = 0.0m; if (decimal.tryparse(tbprice.text, out price) && price > 0) { purchasedproduct temp = purchase.purchasedproducts. firstordefault( x => x.product == products[cbproductname.selectedindex] && x.price == price); if (temp == null) { purchasedproduct newpp = new purchasedproduct(); newpp.product = products[cbproductname.selectedindex]; newpp.purchase = purchase; newpp.quantity = (int)numquantity.value; newpp.price = price; if (newpp.product != null) { purchase.purchasedproducts.add(newpp); } } else { temp.quantity += (int)numquantity.value; } refreshpurchasedproduct(); } } private void btsave_click(object sender, eventargs e) { try { purchase.received_date = dtpdate.value; purchase.total_amount = decimal.parse(tbtotalprice.text); purchase.note = tbnote.text; using (var context = new dbkrunchworkcontext()) { (int = 0; < purchase.purchasedproducts.count; i++) { purchasedproduct pp = purchase.purchasedproducts.elementat(i); product p = context.products. firstordefault(x => x.id == pp.product.id); pp.product = p; } } } catch (exception) { } } }
and main form insert new record table after receiving dialogresult() == dialogresult.ok
above form
.
private void purchase_addnewrecord() { fmaddeditpurchase addform = new fmaddeditpurchase(); if (addform.showdialog() == dialogresult.ok && addform.purchase.total_amount > 0) { using (var context = new dbkrunchworkcontext()) { context.purchases.add(addform.purchase); context.savechanges(); } } }
example :
before
what did (+ save)
after (please note creating new product
instead of using old one)
you using multiple instance of dbcontext
in multiple methods of form.
you have issue because below code take objects purshase
graph , mark of them added
state.
private void purchase_addnewrecord() { fmaddeditpurchase addform = new fmaddeditpurchase(); if (addform.showdialog() == dialogresult.ok && addform.purchase.total_amount > 0) { using (var context = new dbkrunchworkcontext()) { context.purchases.add(addform.purchase); context.savechanges(); } } }
to solve must change state of every product
instance related purshase
instance follwoing code :
private void purchase_addnewrecord() { fmaddeditpurchase addform = new fmaddeditpurchase(); if (addform.showdialog() == dialogresult.ok && addform.purchase.total_amount > 0) { using (var context = new dbkrunchworkcontext()) { context.purchases.add(addform.purchase); foreach (var purchasedproduct in addform.purchase.purchasedproducts) { context.entry(purchasedproduct.product).state = entitystate.unchanged; } context.savechanges(); } } }
it not recommended, when using windows forms or wpf, create new instance of dbcontext
every method do. must create 1 per form creating field that.
Comments
Post a Comment