public void SaveCostProfitCenter(CostProfitCentersViewModel CostProfitCenterVM) { DateTime stamp = DateTime.UtcNow; if (CostProfitCenterVM.CostCenterID == 0) { CostProfitCenter c = new CostProfitCenter(); c.LastModified = stamp; c.LastModifiedBy = "System"; c.RecordAdded = stamp; c.RecordAddedBy = "System"; c.Description = CostProfitCenterVM.Description; c.IsCostCenter = CostProfitCenterVM.IsCostCenter; c.IsDeleted= false; context.CostProfitCenters.Add(c); context.SaveChanges(); Audit(c, stamp); } else { CostProfitCenter dbEntry = context.CostProfitCenters.Find(CostProfitCenterVM.CostCenterID); if (dbEntry != null) { Audit(dbEntry, CostProfitCenterVM, stamp); dbEntry.LastModified = stamp; dbEntry.LastModifiedBy = "System"; dbEntry.Description = CostProfitCenterVM.Description; dbEntry.IsCostCenter = CostProfitCenterVM.IsCostCenter; context.SaveChanges(); } } }
private void Audit(CostProfitCenter c, DateTime stamp) { var query = from p in context.ApplicationTableFields where p.IsDeleted == false && p.ApplicationTableID == 11 && p.IsAudited == true orderby p.Description select p; foreach (var item in query) { ApplicationTableField f = context.ApplicationTableFields.Find(item.ApplicationTableFieldID); ChangeLog cl = new ChangeLog(); cl.ApplicationTableFieldID = item.ApplicationTableFieldID; cl.RecordID = c.CostProfitCenterID; cl.ChangeMade = stamp; cl.UserName = "******"; cl.BeforeValue = "NewRecord"; switch (f.Description) { case "Description": cl.AfterValue = c.Description; break; case "IsCostCenter": cl.AfterValue = c.IsCostCenter.ToString(); break; } IChangeLogRepository repo = new EFChangeLogRepository(); repo.SaveChangeLog(cl); } }