i'm developing pos in wpf. crud operations, i'm using entity framework.
- created wpf view productadd product add view snap
- created class productcontroller in controller folder
made object of entity framework in productcontroller class productcontroller calss snap
created method: saveproduct(product product) taking product object argument , saving database using ef.
and xaml.cs i'm calling productcontroller class's saveproduct method , sending new product data it.
private void button_click(object sender, routedeventargs e) { productcontroller pc = new productcontroller(); product product = new product(); product.product_name = product_name.text.tostring(); product.unitprice = convert.toint32(unit_price.text.tostring()); product.category_id = 1; pc.saveproduct(product); messagebox.show("product added successfully"); this.close(); }
and in productcontroller following code updating database
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using pizzalounge.models; namespace pizzalounge.controllers { public class productcontroller { pizzaloungeentities db = new pizzaloungeentities(); public void saveproduct(product product) { db.products.add(product); db.savechanges(); } } }
- the code executes doesn't save product in database. p.s. have used db.savechanges().
am missing or using wrong approach update database?
you using |datadirectory| in connection string. if debugging in visual studio, database using in bin/debug folder. unfortunately if @ db through server explorer has different connection string don't see changes.
also if database property "copy output directory" set copy every time debug overwrite db , won't see data added. can check if happening using new db context in same debug session add records. if new context can records db know must being written (as other checks listed in comments)
this can fixed changing copy output directory never copy or copy if newer.