.Net Core 微服务 CQRS 中的 CurrentUser

CurrentUser in .Net Core Microservice CQRS

命名空间LED.OM.Core.Base

{ public 抽象 class BaseContext : DbContext { public BaseContext(DbContextOptions 选项):base(选项) { }

    public override int SaveChanges()
    {
        UpdateAuditEntities();
        return base.SaveChanges();
    }

    public override int SaveChanges(bool acceptAllChangesOnSuccess)
    {
        UpdateAuditEntities();
        return base.SaveChanges(acceptAllChangesOnSuccess);
    }

    public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        UpdateAuditEntities();
        return base.SaveChangesAsync(cancellationToken);
    }

    private void UpdateAuditEntities()
    { 
        var CurrentUser = Thread.CurrentPrincipal?.Identity?.Name ?? "";
        var modifiedEntries = ChangeTracker.Entries()
            .Where(x => x.Entity is Entity &&
                        (x.State == EntityState.Added || x.State == EntityState.Modified || x.State == EntityState.Deleted));



        foreach (var entry in modifiedEntries)
        {

            var entity = (Entity)entry.Entity;
            var now = DateTime.UtcNow;

            if (entry.State == EntityState.Added)
            {
                entity.CreatedDate = now;
                entity.CreatedBy = CurrentUser;
            }
            else if (entry.State == EntityState.Deleted && entry.Entity is not IHardDelete)
            { 
                // Varlığı değiştirilmedi olarak ayarlıyoruz.
                // (tüm varlığı Değiştirildi olarak işaretlersek, her alan güncelleme olarak Db'ye gönderilir)
                entry.State = EntityState.Unchanged;

                // Yalnızca IsDeleted alanını güncelleyin - yalnızca bu Db'ye gönderilir
                entity.IsDelete = true;  
            }
            else
            {
                base.Entry(entity).Property(x => x.CreatedBy).IsModified = false;
                base.Entry(entity).Property(x => x.CreatedDate).IsModified = false;
            }

            entity.UpdatedDate = now;
            entity.UpdatedBy = CurrentUser;
        }
    }


}

}

user.cs

entity.cs

我正在使用微服务。实体是我整个table中的公共字段。我想填写那些想登录的人创建和更新的字段。但是我找不到如何在 BaseContext 中填写此用户信息。

问题不在于微服务或 CQRS。问题是关于 Entity Framework 核心。所以你需要在 UpdateAuditEntities() 方法中检测修改的实体然后你可以填充 UpdatedDateUpdatedBy 个属性。

此外,此方法已在创建 (EntityState.Added) 操作时填充 CreatedDateCreatedBy 属性。

将 entry.Entity 转换为 LED.OM.Core.Base.Entities.Entity 时可能会出现异常。您应该使用此代码来检索修改后的条目: var modifiedEntries = ChangeTracker.Entries<LED.OM.Core.Base.Entities.Entity>()...

您应该像下面这样处理更新后的实体 (EntityState.Modified)

private void UpdateAuditEntities()
    { 
        var CurrentUser = Thread.CurrentPrincipal?.Identity?.Name ?? "";
        var modifiedEntries = ChangeTracker.Entries<LED.OM.Core.Base.Entities.Entity>()
            .Where(x => x.Entity is Entity &&
                        (x.State == EntityState.Added || x.State == EntityState.Modified || x.State == EntityState.Deleted));



        foreach (var entry in modifiedEntries)
        {

            var entity = (Entity)entry.Entity;
            var now = DateTime.UtcNow;

            if (entry.State == EntityState.Added)
            {
                entity.CreatedDate = now;
                entity.CreatedBy = CurrentUser;
            }
            else if (entry.State == EntityState.Modified)
            {
                entity.UpdatedDate = now;
                entity.UpdatedBy = CurrentUser;
            }
            else if (entry.State == EntityState.Deleted && entry.Entity is not IHardDelete)
            { 
                // Varlığı değiştirilmedi olarak ayarlıyoruz.
                // (tüm varlığı Değiştirildi olarak işaretlersek, her alan güncelleme olarak Db'ye gönderilir)
                entry.State = EntityState.Unchanged;

                // Yalnızca IsDeleted alanını güncelleyin - yalnızca bu Db'ye gönderilir
                entity.IsDelete = true;  
            }
            else
            {
                base.Entry(entity).Property(x => x.CreatedBy).IsModified = false;
                base.Entry(entity).Property(x => x.CreatedDate).IsModified = false;
            }

            entity.UpdatedDate = now;
            entity.UpdatedBy = CurrentUser;
        }
    }