public void Add() { using (var ScenGenContext = new ScenarioGeneratorModel("UnitTestUser", Connection)) { ScenGenContext.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); var record = new RiskFactorDimension(); var item = new RiskFactorDimensionDetail(); record.RiskFactorDimensionDetails.Add(item); item.Name = "XXX"; var dimType = new RiskFactorDimType(); dimType.Name = "XXX"; item.RiskFactorDimType = dimType; ScenGenContext.RiskFactorDimensionDetails.Add(item); ScenGenContext.SaveChanges(); var result = ScenGenContext.RiskFactorDimensions.Include("RiskFactorDimType").Include("RiskFactorDimensionDetail").Where(x => x.RiskFactorDimensionDetail.Name == "XXX").ToList(); Assert.AreEqual(1, result.Count()); Assert.IsNotNull(result[0].RiskFactorDimensionDetail); Assert.IsNotNull(result[0].RiskFactorDimensionDetail.RiskFactorDimType); } }
public RiskFactorDimensionDetail AddRiskFactorDimension() { var record = new RiskFactorDimension(); var detail = new RiskFactorDimensionDetail(); record.RiskFactorDimensionDetails.Add(detail); detail.RiskFactorDimension = record; detail.Default(Context.UserName); record.Default(Context.UserName); detail.Name = "Risk Factor Dimension Name"; if (Context.RiskFactorDimensionDetails.Local.Count() > 0) { detail.RiskFactorDimensionDetailID = Context.RiskFactorDimensionDetails.Local.Max(x => x.RiskFactorDimensionDetailID) + 1; } else { detail.RiskFactorDimensionDetailID = 1; } Context.RiskFactorDimensionDetails.Add(detail); return detail; }
internal static void AddDimensions(ScenarioGeneratorModel context) { DateTime now = context.AsOfDate; var record = new RiskFactorDimension() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; var itemdimType = context.RiskFactorDimTypes.Where(x => x.Name == "Tenor").First(); var item = new RiskFactorDimensionDetail() { Name = "Instrument Tenor", StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; item.RiskFactorDimType = itemdimType; record.RiskFactorDimensionDetails.Add(item); context.RiskFactorDimensions.Add(record); record = new RiskFactorDimension() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; itemdimType = context.RiskFactorDimTypes.Where(x => x.Name == "Tenor").First(); item = new RiskFactorDimensionDetail() { Name = "Underlying Tenor", StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; item.RiskFactorDimType = itemdimType; record.RiskFactorDimensionDetails.Add(item); context.RiskFactorDimensions.Add(record); record = new RiskFactorDimension() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; itemdimType = context.RiskFactorDimTypes.Where(x => x.Name == "Country").First(); item = new RiskFactorDimensionDetail() { Name = "Country of Risk", StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; item.RiskFactorDimType = itemdimType; record.RiskFactorDimensionDetails.Add(item); context.RiskFactorDimensions.Add(record); record = new RiskFactorDimension() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; itemdimType = context.RiskFactorDimTypes.Where(x => x.Name == "Currency").First(); item = new RiskFactorDimensionDetail() { Name = "Currency", StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; item.RiskFactorDimType = itemdimType; record.RiskFactorDimensionDetails.Add(item); context.RiskFactorDimensions.Add(record); record = new RiskFactorDimension() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; itemdimType = context.RiskFactorDimTypes.Where(x => x.Name == "Currency").First(); item = new RiskFactorDimensionDetail() { Name = "Underlying Currency", StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; item.RiskFactorDimType = itemdimType; record.RiskFactorDimensionDetails.Add(item); context.RiskFactorDimensions.Add(record); record = new RiskFactorDimension() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; itemdimType = context.RiskFactorDimTypes.Where(x => x.Name == "Country").First(); item = new RiskFactorDimensionDetail() { Name = "Country of Origin", StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; item.RiskFactorDimType = itemdimType; record.RiskFactorDimensionDetails.Add(item); context.RiskFactorDimensions.Add(record); record = new RiskFactorDimension() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; itemdimType = context.RiskFactorDimTypes.Where(x => x.Name == "Region").First(); item = new RiskFactorDimensionDetail() { Name = "Region of Risk", StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; item.RiskFactorDimType = itemdimType; record.RiskFactorDimensionDetails.Add(item); context.RiskFactorDimensions.Add(record); record = new RiskFactorDimension() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; itemdimType = context.RiskFactorDimTypes.Where(x => x.Name == "Rating").First(); item = new RiskFactorDimensionDetail() { Name = "Obligor Rating", StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; item.RiskFactorDimType = itemdimType; record.RiskFactorDimensionDetails.Add(item); context.RiskFactorDimensions.Add(record); record = new RiskFactorDimension() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; itemdimType = context.RiskFactorDimTypes.Where(x => x.Name == "Sector").First(); item = new RiskFactorDimensionDetail() { Name = "Instrument Sector", StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; item.RiskFactorDimType = itemdimType; record.RiskFactorDimensionDetails.Add(item); context.RiskFactorDimensions.Add(record); record = new RiskFactorDimension() { StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; itemdimType = context.RiskFactorDimTypes.Where(x => x.Name == "Sector").First(); item = new RiskFactorDimensionDetail() { Name = "Underlying Sector", StartTime = now.AddDays(-100), EndTime = DateTime.MaxValue, CreatedAt = now, CreatedBy = "A", ApprovedAt = now.AddDays(-100), ApprovedBy = "B", Latest = true }; item.RiskFactorDimType = itemdimType; record.RiskFactorDimensionDetails.Add(item); context.RiskFactorDimensions.Add(record); context.SaveChanges(); }
protected override void BeforeSave() { DateTime timestamp = DateTime.UtcNow; //find all changed enities var modifiedEntities = Context.ChangeTracker.Entries<RiskFactorDimensionDetail>() .Where(p => p.State == EntityState.Modified) .Select(p => p.Entity); foreach (RiskFactorDimensionDetail detail in modifiedEntities) { //main entity, for which we keep versioning var mainEntity = detail.RiskFactorDimension; ICollection<RiskFactorDimensionDetail> mainEntityDetails = mainEntity.RiskFactorDimensionDetails; if (detail.Status == EntityStatus.ExpiredInSession) { foreach (RiskFactorDimensionDetail item in mainEntityDetails) { if (detail.RiskFactorDimensionDetailID != item.RiskFactorDimensionDetailID) { item.EndTime = detail.EndTime; item.ModifiedBy = detail.ModifiedBy; item.ModifiedAt = detail.ModifiedAt; } } mainEntity.EndTime = detail.EndTime; mainEntity.ModifiedBy = detail.ModifiedBy; mainEntity.ModifiedAt = detail.ModifiedAt; } if (detail.Status == EntityStatus.ApprovedInSession) { //entity has been approved //mark existing details as expired foreach (RiskFactorDimensionDetail item in mainEntityDetails) { if (detail.RiskFactorDimensionDetailID != item.RiskFactorDimensionDetailID) { item.Latest = false; item.EndTime = (DateTime)detail.ApprovedAt; detail.StartTime = (DateTime)detail.ApprovedAt; } } //check if main entity is approved - if not copy details if (mainEntity.Status != EntityStatus.Approved) { mainEntity.ApprovedAt = detail.ApprovedAt; mainEntity.ApprovedBy = detail.ApprovedBy; mainEntity.StartTime = detail.StartTime; } } else if (detail.Status == EntityStatus.UnapprovedInSession) { //new version of the entity //we are creating a copy of currently edited entity //and add it back to context var copy = new RiskFactorDimensionDetail(); mainEntity.RiskFactorDimensionDetails.Add(copy); //copy values from original Context.Entry(copy).CurrentValues.SetValues(detail); //reset auditing copy.ResetCopy(); //revert the changes - done implicitly Context.Entry(detail).State = EntityState.Unchanged; //remove latest flag detail.Latest = false; } } }