/// <summary> /// Get's entity changes history in a date range /// </summary> /// <typeparam name="T">Entity type</typeparam> /// <typeparam name="TC">Context type</typeparam> /// <param name="context">The current context</param> /// <param name="from">Starting date to compare</param> /// <param name="to">Ending date to compare</param> /// <param name="entityKey">The key of the entity to check</param> /// <returns></returns> public static IEnumerable <AuditLog> GetHistory <T, TC>(this TC context, DateTime from, DateTime to, EntityKey entityKey) where T : class where TC : DbContext, IAuditDbContext { if (entityKey == null) { throw new ArgumentNullException("entityKey"); } var key = entityKey.GetEntityString(); var entityType = typeof(T); return(context.AuditLogs.Where(l => l.EntityFullName.Equals(entityType.FullName, StringComparison.InvariantCultureIgnoreCase) && l.Created >= from && l.Created <= to && l.EntityId == key).OrderBy(l => l.Created).ToList()); }
/// <summary> /// Get the state of an entity /// </summary> /// <typeparam name="T">Entity type</typeparam> /// <typeparam name="TC">Context type</typeparam> /// <param name="context">The current context</param> /// <param name="date">snapshot date</param> /// <param name="entityKey">the key of the entity to check</param> /// <returns></returns> public static IEnumerable <AuditRecord <T> > GetSnapshot <T, TC>(this TC context, DateTime date, EntityKey entityKey) where T : class where TC : DbContext, IAuditDbContext { if (entityKey == null) { throw new ArgumentNullException("entityKey"); } var d = date.Date; var key = entityKey.GetEntityString(); var entityType = typeof(T); var logs = context.AuditLogs.Where(q => q.EntityFullName.Equals(entityType.FullName, StringComparison.InvariantCultureIgnoreCase) && SqlFunctions.DateDiff("DAY", q.Created, d) == 0 && q.EntityId == key).ToList(); return(logs.OrderBy(l => l.Created).Select(log => new AuditRecord <T> { Entity = Utils.Deserialize <T>(log.Entity), Date = log.Created })); }