public static bool UpdateCustomer(string id, string name, string phone, int type, int gender, int age, string address, List <Health> addlist, List <Health> removelist) { using (var context = new ControllerModel()) { try { var customer = context.Customers.Where(cus => cus.localid == id).FirstOrDefault(); var select_type = context.Types.Where(o => o.typeid == type).FirstOrDefault(); if (customer != null) { customer.name = name; customer.phone = phone; customer.gender = gender; customer.Types = select_type; customer.age = age; customer.address = address; customer.dayupdate = DateTime.Now; } ; foreach (Health health in removelist) { var ch = context.CustomerHealths.Where(o => o.Customers.localid == id && o.Health.healthid == health.healthid); if (ch.Count() > 0) { context.CustomerHealths.RemoveRange(ch); } } context.SaveChanges(); foreach (Health health in addlist) { var ch = context.CustomerHealths.Where(o => o.Customers.localid == id && o.Health.healthid == health.healthid); var heal = context.Healths.Where(o => o.healthid == health.healthid).FirstOrDefault(); if (ch.Count() == 0) { var ht = new CustomerHealth { Customers = customer, Health = heal, dayadd = DateTime.Now, dayupdate = DateTime.Now }; context.CustomerHealths.Add(ht); } } context.SaveChanges(); return(true); } catch (Exception ex) { return(false); } } }
public static bool AddCustomer(string id, string name, string phone, int type, int gender, int age, string address, List <Health> listwithout) { using (var context = new ControllerModel()) { try { var select_type = context.Types.Where(o => o.typeid == type).FirstOrDefault(); var customer = new Customers { localid = id, name = name, phone = phone, Types = select_type, gender = gender, age = age, address = address, available = true, dayadd = DateTime.Now, dayupdate = DateTime.Now }; context.Customers.Add(customer); addedCustomer = customer; foreach (Health healt in listwithout) { var selecthealth = context.Healths.Where(o => o.healthid == healt.healthid).FirstOrDefault(); var health = new CustomerHealth { Customers = context.Customers.Where(cus => cus.localid == id).FirstOrDefault(), Health = selecthealth, dayadd = DateTime.Now, dayupdate = DateTime.Now }; context.CustomerHealths.Add(health); } context.SaveChanges(); return(true); } catch (Exception ex) { return(false); } } }