Example #1
0
        /// <summary>
        /// More info: http://www.dotnettips.info/post/2507
        /// </summary>
        public static void SetAuditableEntityPropertyValues
            (this Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker changeTracker,
            Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor)
        {
            var httpContext = httpContextAccessor?.HttpContext;
            var userAgent   = httpContext?.Request?.Headers["User-Agent"].ToString();
            var userIp      = httpContext?.Connection?.RemoteIpAddress?.ToString();

            var now = System.DateTimeOffset.UtcNow;
            //var userId = getUserId(httpContext);

            var modifiedEntries = changeTracker.Entries <Models.AuditableEntity.IAuditableEntity>()
                                  .Where(x => x.State == Microsoft.EntityFrameworkCore.EntityState.Modified);

            foreach (var modifiedEntry in modifiedEntries)
            {
                modifiedEntry.Property(ModifiedDateTime).CurrentValue      = now;
                modifiedEntry.Property(ModifiedByBrowserName).CurrentValue = userAgent;
                modifiedEntry.Property(ModifiedByIp).CurrentValue          = userIp;
                //modifiedEntry.Property(ModifiedByUserId).CurrentValue = userId;
            }

            var addedEntries = changeTracker.Entries <Models.AuditableEntity.IAuditableEntity>()
                               .Where(x => x.State == Microsoft.EntityFrameworkCore.EntityState.Added);

            foreach (var addedEntry in addedEntries)
            {
                addedEntry.Property(CreatedDateTime).CurrentValue      = now;
                addedEntry.Property(CreatedByBrowserName).CurrentValue = userAgent;
                addedEntry.Property(CreatedByIp).CurrentValue          = userIp;
                //addedEntry.Property(CreatedByUserId).CurrentValue = userId;
            }
        }
        // Add auditing
        private void AddDateTimeAuditing(Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker tracker)
        {
            // for created entities
            tracker.Entries().Where(e => e.State == EntityState.Added).ToList()
            .ForEach(entity => {
                entity.Property("CreatedAt").CurrentValue  = System.DateTime.UtcNow;
                entity.Property("ModifiedAt").CurrentValue = System.DateTime.UtcNow;
            });

            // for modified entities
            tracker.Entries().Where(e => e.State == EntityState.Modified).ToList()
            .ForEach(entity => {
                entity.Property("ModifiedAt").CurrentValue = System.DateTime.UtcNow;
            });
        }
Example #3
0
        public static void FillAudit(this Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker changeTracker, Behaviours.Auditable.Auditable auditable = null)
        {
            auditable = auditable ?? new Auditable();
            foreach (var entry in changeTracker.Entries().Where(e => e.State == EntityState.Added || e.State == EntityState.Modified))
            {
                if (entry.Entity is IAuditable)
                {
                    switch (entry.State)
                    {
                    case EntityState.Added:
                        if (!String.IsNullOrEmpty(auditable.CreatedByName))
                        {
                            entry.Property(auditable.CreatedByName).CurrentValue = auditable.CreatedByAction();
                        }
                        if (!String.IsNullOrEmpty(auditable.CreatedAtName))
                        {
                            entry.Property(auditable.CreatedAtName).CurrentValue = auditable.CreatedAtAction();
                        }
                        break;

                    case EntityState.Deleted:
                    case EntityState.Modified:
                        if (!String.IsNullOrEmpty(auditable.UpdatedByName))
                        {
                            entry.Property(auditable.UpdatedByName).CurrentValue = auditable.UpdatedByAction();
                        }
                        if (!String.IsNullOrEmpty(auditable.UpdatedAtName))
                        {
                            entry.Property(auditable.UpdatedAtName).CurrentValue = auditable.UpdatedAtAction();
                        }
                        break;
                    }
                }
            }
        }
Example #4
0
        internal static void SoftDelete(this Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker changeTracker)
        {
            var entities = changeTracker.Entries()
                           .Where(e => e.State == EntityState.Deleted &&
                                  e.Metadata.GetProperties()
                                  .Any(p => p.Name == "Deleted")).ToList();

            foreach (var entity in entities)
            {
                entity.State = EntityState.Unchanged;
                entity.CurrentValues["Deleted"] = true;
            }
        }
Example #5
0
        private void AddAuditDetails(Microsoft.EntityFrameworkCore.ChangeTracking.ChangeTracker changeTracker)
        {
            var currentSystemuserId = _systemUserService.GetCurrentySystemuUserId();;

            changeTracker.Entries().Where(e => e.State == EntityState.Added).ToList()
            .ForEach(x => {
                x.Property("CreatedOn").CurrentValue  = System.DateTime.UtcNow;
                x.Property("ModifiedOn").CurrentValue = System.DateTime.UtcNow;

                if (currentSystemuserId != null && !x.Property("Id").CurrentValue.Equals(currentSystemuserId))
                {
                    x.Property("CreatedById").CurrentValue  = currentSystemuserId;
                    x.Property("ModifiedById").CurrentValue = currentSystemuserId;
                }
            });
            changeTracker.Entries().Where(e => e.State == EntityState.Modified).ToList()
            .ForEach(x => {
                x.Property("ModifiedOn").CurrentValue = System.DateTime.UtcNow;
                if (currentSystemuserId != null && !x.Property("Id").CurrentValue.Equals(currentSystemuserId))
                {
                    x.Property("ModifiedById").CurrentValue = currentSystemuserId;
                }
            });
        }