// Create public static void Insert(OffDay_Supplier_Mapping model) { using (var db = new MouldEntities()) { var canInsert = db.OffDay_Supplier_Mappings.SingleOrDefault(s => s.ID == model.ID && s.OffDayID == model.OffDayID && s.SupplierID == model.SupplierID); if (canInsert == null) { db.OffDay_Supplier_Mappings.Add(model); db.SaveChanges(); } } }
// Update public static void Update(OffDay_Supplier_Mapping model) { using (var db = new MouldEntities()) { var canUpdate = db.OffDay_Supplier_Mappings.SingleOrDefault(s => s.ID == model.ID); if (canUpdate != null) { canUpdate.OffDayID = model.OffDayID; canUpdate.SupplierID = model.SupplierID; db.SaveChanges(); } } }
private void bwSave_DoWork(object sender, DoWorkEventArgs e) { if (insertOrUpdate == true) { OffDayController.Insert(offDay); offDayCurrentList.Add(offDay); foreach (var supp in supplierCurrentList) { var offDayMapInsert = new OffDay_Supplier_Mapping() { SupplierID = supp.SupplierID, OffDayID = offDay.OffDayID }; OffDayMapController.Insert(offDayMapInsert); } } if (insertOrUpdate == false) { // Update OffDay OffDayController.Update(offDayCurrent); // Delete OffDayMap RemoveOffDayMap(offDayCurrent.OffDayID); // Insert OffDayMap foreach (var supp in supplierCurrentList) { var offDayMapInsert = new OffDay_Supplier_Mapping() { SupplierID = supp.SupplierID, OffDayID = offDayCurrent.OffDayID }; OffDayMapController.Insert(offDayMapInsert); } } }