public void ApplyChanges() { var newConnection = new dbContextDataContext(); //If Patient Changed. if(PatientPropertiesChanged()) { try { _sourcePatient = (from p in newConnection.tblPatients where p.PatientID == _sourcePatient.PatientID select p).SingleOrDefault(); if(_sourcePatient != null) { _sourcePatient.UpdatePatient(EditCopyPatient); newConnection.SubmitChanges(); } else { _sourcePatient = new tblPatient(); _sourcePatient.UpdatePatient(EditCopyPatient); newConnection.tblPatients.InsertOnSubmit(_sourcePatient); newConnection.SubmitChanges(); } } catch (Exception) { MessageBox.Show(@"Database not responding. Please check your connection."); } } //If Patient Address Changed. if(PatientAddressPropertiesChanged()) { try { _sourceAddress = (from p in newConnection.tblPatientAddrs where p.PatientID == _sourcePatient.PatientID select p).FirstOrDefault(); if (_sourceAddress != null) { _sourceAddress.UpdatePatientAddress(EditCopyAddress); newConnection.SubmitChanges(); } else { _sourceAddress = new tblPatientAddr(); newConnection.tblPatientAddrs.InsertOnSubmit(_sourceAddress); newConnection.SubmitChanges(); } } catch (Exception) { MessageBox.Show(@"Database not responding. Please check your connection."); } } //Claimants of this patient changed. if(ClaimantsChanged() == false) { var added = this.EditCopyClaimantList.Except(this._sourceClaimantList); var deleted = this._sourceClaimantList.Except(this.EditCopyClaimantList); if (deleted.Any()) { using (newConnection) { foreach (var y in deleted.Select(x => (tblPatientClaimant)(from p in newConnection.tblPatientClaimants where p.PatientID == this._sourcePatient.PatientID && p.ClaimantID == x.ClaimantID select p).First())) { newConnection.tblPatientClaimants.DeleteOnSubmit(y); } newConnection.SubmitChanges(); } } if (added.Any()) { var newConnection1 = new dbContextDataContext(); using (newConnection1) { foreach (var y in added.Select(x => new tblPatientClaimant { ClaimantID = x.ClaimantID, PatientID = _sourcePatient.PatientID })) { newConnection1.tblPatientClaimants.InsertOnSubmit(y); } newConnection1.SubmitChanges(); } } } }
public void Initialize() { var newConnection = new dbContextDataContext(); EditCopyPatient = entityClone.Clone(_sourcePatient); try { _sourceAddress = (from p in newConnection.tblPatientAddrs where p.PatientID == _sourcePatient.PatientID select p).FirstOrDefault(); if(_sourceAddress != null) { } else { _sourceAddress = new tblPatientAddr(); } _sourceClaimantList = (from c in newConnection.tblClaimants join p in newConnection.tblPatientClaimants on c.ClaimantID equals p.ClaimantID where p.PatientID == _sourcePatient.PatientID select c).ToList(); _sourceAccident = newConnection.tblPatientAccidents.Where(od => od.PatientID == _sourcePatient.PatientID).FirstOrDefault(); if (_sourceAccident == null) { _sourceAccident = new tblPatientAccident(); } EditCopyAccident = entityClone.Clone(_sourceAccident); EditCopyAddress = entityClone.Clone(_sourceAddress); EditCopyClaimantList = entityCollectionClone.ClaimantsClone(_sourceClaimantList); } catch(Exception) { MessageBox.Show(@"Database not responding. Please check your connection."); } }