public Prescription Create(Prescription model) { var dal = model.ToDAL(); _db.Prescription.Add(dal); _db.SaveChanges(); _db.SaveChanges(); return(dal.ToBLL()); }
public bool Update(Prescription model) { var dal = model.ToDAL(); var prescriptionMedicine = _db.PrescriptionMedicine.Where(pm => pm.PrescriptionId == model.Id).ToList(); try { var pmIds = dal.PrescriptionMedicine.Select(pm => pm.PrescriptionMedicineId).ToList(); var persistenceIds = new List <int>(); { foreach (var pm in prescriptionMedicine) { if (pmIds.Contains(pm.PrescriptionMedicineId)) { var dalPm = dal.PrescriptionMedicine.First(e => e.PrescriptionMedicineId == pm.PrescriptionMedicineId); pm.MedicineId = dalPm.MedicineId; pm.Amount = dalPm.Amount; //_db.PrescriptionMedicine.Update(pm); persistenceIds.Add(pm.PrescriptionMedicineId); } else { _db.PrescriptionMedicine.Remove(pm); } } _db.SaveChanges(); } foreach (var medicine in dal.PrescriptionMedicine) { if (!persistenceIds.Contains(medicine.PrescriptionMedicineId)) { var newPm = new PrescriptionMedicine { Amount = medicine.Amount, MedicineId = medicine.MedicineId, PrescriptionId = medicine.PrescriptionId }; _db.Add(newPm); _db.SaveChanges(); } } } catch (Exception e) { return(false); } try { var updated = _db.Prescription .First(e => e.PrescriptionId == dal.PrescriptionId); updated.Buyer = dal.Buyer; updated.SaleTime = dal.SaleTime; updated.UserId = dal.UserId; _db.Prescription.Update(updated); _db.SaveChanges(); return(true); } catch { return(false); } }