namespace LED.OM.Core.Base
{ public abstract class BaseContext : DbContext { public BaseContext(DbContextOptions options) : base(options) { }
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;
}
}
}
}
I am working with microservice. The entity is the common field in my entire table. I want to fill in the fields created and updated by those who want to log in. But I couldn't find how to fill this user info in BaseContext.
The problem is not about microservices or CQRS. The problem is about Entity Framework Core. So you need to detect the modified entities in UpdateAuditEntities()
method then you can fill the UpdatedDate
and
UpdatedBy
properties.
Also, this method already fills the CreatedDate
and CreatedBy
properties in creating (EntityState.Added)
operations.
You can get an exception when casting entry.Entity to LED.OM.Core.Base.Entities.Entity. You should use this code for retrieving modified entries: var modifiedEntries = ChangeTracker.Entries<LED.OM.Core.Base.Entities.Entity>()...
You should handle the updated entities (EntityState.Modified)
like below:
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;
}
}