public ValueTypeEntityFrameworkRepositoryBase( IHttpContextAccessor httpContextAccessor, ILogger <EntityFrameworkRepositoryBase <TIdentity, TModel, TDbContext> > logger, ICache cache, IAuditingColumns auditingColumns, TDbContext context) : base(httpContextAccessor, logger, cache, auditingColumns, context) { Contract.Requires(httpContextAccessor != null); Contract.Requires(cache != null); Contract.Requires(auditingColumns != null); Contract.Requires(context != null); Contract.Ensures(this.Context != null); }
public EntityFrameworkRepositoryBase( IHttpContextAccessor httpContextAccessor, ILogger <EntityFrameworkRepositoryBase <TIdentity, TModel, TDbContext> > logger, ICache cache, IAuditingColumns auditingColumns, TDbContext context) { Contract.Requires(httpContextAccessor != null); Contract.Requires(cache != null); Contract.Requires(context != null); Contract.Requires(auditingColumns != null); Contract.Ensures(this.Context != null); this.HttpContextAccessor = httpContextAccessor; this.Logger = logger; this.Cache = cache; this.AuditingColumns = auditingColumns; this.Context = context; }
internal static void AuditChanges( this DbContext source, HttpContext httpContext, IAuditingColumns auditingColumns) { var employeeId = httpContext.GetLoggedInEmployeeId(); var now = DateTime.Now; foreach (var entry in source.ChangeTracker.Entries().Where(e => e.State == EntityState.Added || e.State == EntityState.Modified)) { if (entry.State == EntityState.Added) { var createdByIdProperty = entry.Properties.FirstOrDefault(p => p.Metadata.Name == auditingColumns.CreatedByColumnName); if (createdByIdProperty != null) { createdByIdProperty.CurrentValue = employeeId; } var createdAtProperty = entry.Properties.FirstOrDefault(p => p.Metadata.Name == auditingColumns.CreatedAtColumnName); if (createdAtProperty != null) { createdAtProperty.CurrentValue = now; } } var updatedByIdProperty = entry.Properties.FirstOrDefault(p => p.Metadata.Name == auditingColumns.UpdatedByColumnName); if (updatedByIdProperty != null) { updatedByIdProperty.CurrentValue = employeeId; } var updatedAtProperty = entry.Properties.FirstOrDefault(p => p.Metadata.Name == auditingColumns.UpdatedAtColumnName); if (updatedAtProperty != null) { updatedAtProperty.CurrentValue = now; } } }