using EnVisage.Code; using Microsoft.AspNet.Identity.EntityFramework; using System.Collections.Generic; using System.Linq; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Data.Entity.Core.Objects; using System.Data.Entity.Infrastructure; using System; using EnVisage.Models.Entities; using System.Xml; using System.Web; namespace EnVisage.Models { // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. public class ApplicationUser : IdentityUser { public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string PhoneNumber { get; set; } public short Type { get; set; } public bool PreferredResourceAllocation { get; set; } public bool ShowAutomaticViews { get; set; } public bool PreferredTotalsDisplaying { get; set; } public decimal OverUnderCoefficient { get; set; } public ApplicationUser() { OverUnderCoefficient = 1; } } public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema:false) { } List historyList = new List(); public override int SaveChanges() { #region Storing Changes history var commonContext = new EnVisageEntities(); historyList.Clear(); var objectContext = ((IObjectContextAdapter)this).ObjectContext; List changes = objectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Modified).ToList(); changes.AddRange(objectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added).ToList()); changes.AddRange(objectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted).ToList()); foreach (ObjectStateEntry stateEntryEntity in changes) { if (!stateEntryEntity.IsRelationship && stateEntryEntity.Entity != null ) { History history = this.HistoryFactory(stateEntryEntity, objectContext); historyList.Add(history); } } if (historyList.Count > 0) { List ids = new List(); foreach (var history in historyList) { if (!ids.Contains(history.Id)) { ids.Add(history.Id); commonContext.Histories.Add(history); } else { history.Id = Guid.NewGuid(); if (!ids.Contains(history.Id)) commonContext.Histories.Add(history); } } } #endregion commonContext.SaveChanges(); return base.SaveChanges(); } private History HistoryFactory(ObjectStateEntry entry, ObjectContext context) { History history = new History(); var objectStateEntry = context.ObjectStateManager.GetObjectStateEntry(entry.Entity); history.Id = Guid.NewGuid(); if (entry.State != EntityState.Added) history.EntityId = new Guid(objectStateEntry.EntityKey.EntityKeyValues[0].Value.ToString()); history.TimeStamp = DateTime.Now; try { history.ModifiedBy = new Guid(HttpContext.Current.User.Identity.GetID()); } catch (Exception) { history.ModifiedBy = Guid.Empty; } history.ModificationType = entry.State.ToString(); history.EntityType = entry.Entity.GetType().Name.Split('_')[0]; history.XML = GetEntryValueInString(entry, true); return history; } private string GetEntryValueInString(ObjectStateEntry entry, bool isOrginal) { XmlDocument doc = new XmlDocument(); XmlElement el = (XmlElement)doc.AppendChild(doc.CreateElement("Entity")); XmlElement modified = null; el.SetAttribute("type", entry.Entity.GetType().Name.Split('_')[0]); var props = entry.Entity.GetType().GetProperties(); if (entry.State == EntityState.Modified || entry.State == EntityState.Deleted) foreach (var elem in props) { var name = elem.Name; int i = -1; try { i = entry.OriginalValues.GetOrdinal(elem.Name); } catch { } if (i >= 0) { var val = Convert.ToString(entry.OriginalValues[entry.OriginalValues.GetOrdinal(elem.Name)]); el.AppendChild(doc.CreateElement(name)).InnerText = Convert.ToString(val); if (entry.State == EntityState.Modified && val != Convert.ToString(elem.GetValue(entry.Entity))) { if (modified == null) modified = (XmlElement)el.AppendChild(doc.CreateElement("Modified")); modified.AppendChild(doc.CreateElement(name)).InnerText = Convert.ToString(elem.GetValue(entry.Entity)); } } } if (entry.State == EntityState.Added) foreach (var elem in props) { if (elem.Name == "Id") { var val = elem.GetValue(entry.Entity); el.AppendChild(doc.CreateElement(elem.Name)).InnerText = Convert.ToString(val); } } return doc.OuterXml; } protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) { modelBuilder.Properties().Configure(config => config.HasPrecision(6, 4)); base.OnModelCreating(modelBuilder); } } }