Initial version
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using EonaCat.gRPC.Core.Entities;
|
||||
|
||||
namespace EonaCat.gRPC.Repository.DatabaseContext;
|
||||
|
||||
public class AppDbContext : DbContext
|
||||
{
|
||||
private readonly ILoggerFactory _loggerFactory = new LoggerFactory();
|
||||
|
||||
public AppDbContext(DbContextOptions<AppDbContext> context) : base(context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseLoggerFactory(_loggerFactory);
|
||||
optionsBuilder.UseSqlServer().LogTo(Console.WriteLine).EnableDetailedErrors();
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
UpdateEntryLog();
|
||||
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
||||
}
|
||||
|
||||
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
|
||||
{
|
||||
UpdateEntryLog();
|
||||
return base.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
private void UpdateEntryLog()
|
||||
{
|
||||
foreach (var entry in ChangeTracker.Entries<BaseEntity>())
|
||||
{
|
||||
switch (entry.State)
|
||||
{
|
||||
case EntityState.Added:
|
||||
entry.Entity.CreatedAt = DateTime.UtcNow;
|
||||
entry.Entity.UpdatedAt = DateTime.UtcNow;
|
||||
break;
|
||||
case EntityState.Modified:
|
||||
entry.Entity.UpdatedAt = DateTime.UtcNow;
|
||||
break;
|
||||
case EntityState.Deleted:
|
||||
entry.Entity.UpdatedAt = DateTime.UtcNow;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public DbSet<UserEntity> Users { get; set; } = null!;
|
||||
}
|
||||
Reference in New Issue
Block a user