欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Entity Framework Core 生成跟踪列-阴影属性

程序员文章站 2022-05-13 21:53:18
摘自:https://www.cnblogs.com/tdfblog/p/entity-framework-core-generate-tracking-columns.html Ef Core 官方文档: https://docs.microsoft.com/zh-cn/ef/core/ 文章列举 ......

摘自:

Ef Core 官方文档: 

文章列举了三种:读写属性、只读属性、不定义属性(阴影属性 Shadow )。

下面是摘的一种阴影属性简单实现:

一、添加阴影属性:

  1. 在 OnModelCreating 方法中声明阴影属性:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.AddAuditableShadowProperties();

    base.OnModelCreating(modelBuilder);
}

  2. 这里是 AddAuditableShadowProperties 实现:

    public static readonly string CreatedByUserId = nameof(CreatedByUserId);
    public static readonly string ModifiedByUserId = nameof(ModifiedByUserId);
    public static readonly string CreatedDateTime = nameof(CreatedDateTime);
    public static readonly string ModifiedDateTime = nameof(ModifiedDateTime);

    public static void AddAuditableShadowProperties(this ModelBuilder modelBuilder)
        {
            var models = modelBuilder.Model
                                     .GetEntityTypes()
                                     .Where(e => typeof(IEntityBase).IsAssignableFrom(e.ClrType));
            foreach (var entityType in models)
            {
                modelBuilder.Entity(entityType.ClrType)
                            .Property<long?>(CreatedByUserId);
                modelBuilder.Entity(entityType.ClrType)
                            .Property<long?>(ModifiedByUserId);

                modelBuilder.Entity(entityType.ClrType)
                            .Property<DateTimeOffset?>(CreatedDateTime);
                modelBuilder.Entity(entityType.ClrType)
                            .Property<DateTimeOffset?>(ModifiedDateTime);
            }
        }

二、修改阴影属性:

  1. 重写 SaveChange 方法:

    public override int SaveChanges()
        {
            ChangeTracker.DetectChanges();
            
            //保存之前修改阴影属性
            SetShadowProperties();  

            // for performance reasons, to avoid calling DetectChanges() again.
            ChangeTracker.AutoDetectChangesEnabled = false;
            var result = base.SaveChanges();
            ChangeTracker.AutoDetectChangesEnabled = true;
            return result;
        }
    private void SetShadowProperties()
        {
            // we can't use constructor injection anymore, because we are using the `AddDbContextPool<>`
            var httpContextAccessor = this.GetService<IHttpContextAccessor>();
            ChangeTracker.SetAuditableEntityPropertyValues(httpContextAccessor);
        }

  2. 修改阴影属性:

    public static void SetAuditableEntityPropertyValues(this ChangeTracker changeTracker, IHttpContextAccessor httpContextAccessor)
        { 
            var httpContext = httpContextAccessor?.HttpContext;
            // var userAgent = httpContext?.Request?.Headers["User-Agent"].ToString();
            // var userIp = httpContext?.Connection?.RemoteIpAddress?.ToString();
            var now = DateTimeOffset.Now;

            var modifiedEntries = changeTracker.Entries<IEntityBase>().Where(x => x.State == EntityState.Modified);
            foreach (var modifiedEntry in modifiedEntries)
            {
                modifiedEntry.Property(ModifiedDateTime).CurrentValue = now;
                modifiedEntry.Property(ModifiedByUserId).CurrentValue = 953833173853548544;
            }

            var addedEntries = changeTracker.Entries<IEntityBase>().Where(x => x.State == EntityState.Added);
            foreach (var addedEntry in addedEntries)
            {
                addedEntry.Property(CreatedDateTime).CurrentValue = now;
                addedEntry.Property(CreatedByUserId).CurrentValue = 953833173853548544;
            }
        }