public void DeleteInvestorCommitment(InvestorCommitment commitment) { using (HqTrustData dbContext = new HqTrustData()) { InvestorCommitment old = dbContext.InvestorCommitmnents. FirstOrDefault(c => c.Id == commitment.Id); if (old == null) { throw new Exception($"Das Commitment mit der Id {commitment.Id} für den Investor {commitment.InvestorId} wurde nicht in der Datenbank gefunden"); } dbContext.InvestorCommitmnents.Remove(old); foreach (InvestorCommitmentDetail detail in commitment.InvestorCommitmentDetails) { InvestorCommitmentDetail icDetail = dbContext.InvestorCommitmentDetails.FirstOrDefault(d => d.Id == detail.Id); if (icDetail == null) { throw new Exception($"Das CommitmentDetail mit der Id {icDetail.Id} für das Commitment {old.Id} wurde nicht in der Datenbank gefunden"); } dbContext.InvestorCommitmentDetails.Remove(icDetail); } try { dbContext.SaveChanges(); } catch (Exception ex) { throw new Exception("Das Commitment konnte in der Datenbank nicht gelöscht werden" + Environment.NewLine + ex.InnerException.Message); } } }
private void OnAddCommitmentDetail() { SelectedInvestorCommitmentDetail.PropertyChanged -= SelectedInvestorCommitmentDetail_PropertyChanged; InvestorCommitmentDetail detail = new InvestorCommitmentDetail() { InvestorCommitmentId = InvestorCommitment.Id, CommitmentDate = DateTime.Now }; InvestorCommitmentDetails.Add(detail); SelectedInvestorCommitmentDetail = InvestorCommitmentDetails[InvestorCommitmentDetails.Count - 1]; SelectedInvestorCommitmentDetail.PropertyChanged += SelectedInvestorCommitmentDetail_PropertyChanged; }
public InvestorCommitment UpdateInvestorCommitments(InvestorCommitment commitment) { if (commitment.Id == 0) { // add new commitment using (HqTrustData dbContext = new HqTrustData()) { InvestorCommitment newCommitment = new InvestorCommitment(commitment); newCommitment.PeFund = null; dbContext.InvestorCommitmnents.Add(newCommitment); try { dbContext.SaveChanges(); return(newCommitment); } catch (Exception ex) { throw new Exception("Das Commitment konnte nicht in der Datenbank gespeichert werden" + Environment.NewLine + ex.InnerException.Message); } } } else { // update commitments using (HqTrustData dbContext = new HqTrustData()) { InvestorCommitment old = dbContext.InvestorCommitmnents. Include("InvestorCommitmentDetails"). FirstOrDefault(c => c.Id == commitment.Id); if (old == null) { throw new Exception($"Das Commitment mit der Id {commitment.Id} für den Investor {commitment.InvestorId} wurde nicht in der Datenbank gefunden"); } old.BankAccountId = commitment.BankAccountId; old.CommitmentAmount = commitment.CommitmentAmount; old.CommitmentPlannedAmount = commitment.CommitmentPlannedAmount; old.DateCommitmentAccepted = commitment.DateCommitmentAccepted; old.PeFundReference = commitment.PeFundReference; try { dbContext.SaveChanges(); } catch (Exception ex) { throw new Exception("Das Commitment konnte in der Datenbank nicht geändert werden" + Environment.NewLine + ex.InnerException.Message); } //foreach(InvestorCommitmentDetail detail in old.InvestorCommitmentDetails) for (int i = 0; i < old.InvestorCommitmentDetails.Count; i++) { InvestorCommitmentDetail detail = old.InvestorCommitmentDetails.ElementAt(i); InvestorCommitmentDetail oldDetail = dbContext.InvestorCommitmentDetails.FirstOrDefault(d => d.Id == detail.Id); if (oldDetail == null) { throw new Exception($"Das CommitmentDetail mit der Id {detail.Id} für das Commitment {old.Id} wurde nicht in der Datenbank gefunden"); } InvestorCommitmentDetail newDetail = commitment.InvestorCommitmentDetails.FirstOrDefault(n => n.Id == detail.Id); if (newDetail == null) { // record has been delete by the user: dbContext.InvestorCommitmentDetails.Remove(oldDetail); dbContext.SaveChanges(); } else { oldDetail.CommitmentAmount = newDetail.CommitmentAmount; oldDetail.CommitmentDate = newDetail.CommitmentDate; oldDetail.SecondaryCallsAfterCutOff = newDetail.SecondaryCallsAfterCutOff; oldDetail.SecondaryCutOffDate = newDetail.SecondaryCutOffDate; oldDetail.SecondaryDistributionsAfterCutOff = newDetail.SecondaryDistributionsAfterCutOff; oldDetail.SecondaryOpenCommitment = newDetail.SecondaryOpenCommitment; oldDetail.SecondaryPurchaseAmount = newDetail.SecondaryPurchaseAmount; dbContext.SaveChanges(); } } foreach (InvestorCommitmentDetail detail in commitment.InvestorCommitmentDetails) { if (detail.Id > 0) { continue; } dbContext.InvestorCommitmentDetails.Add(detail); } try { dbContext.SaveChanges(); return(commitment); } catch (Exception ex) { throw new Exception("Das Commitment konnte in der Datenbank nicht geändert werden" + Environment.NewLine + ex.InnerException.Message); } } } }