using Microsoft.EntityFrameworkCore; using MTWorkHR.Core.Entities; using MTWorkHR.Core.Entities.Base; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MTWorkHR.Infrastructure.Data { public class HRDataContext : DbContext { public HRDataContext(DbContextOptions options) : base(options) { } public DbSet Companies { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfigurationsFromAssembly(typeof(HRDataContext).Assembly); base.OnModelCreating(modelBuilder); } public override Task SaveChangesAsync(CancellationToken cancellationToken = default) { foreach (var entry in base.ChangeTracker.Entries().Where(q=> q.State == EntityState.Added || q.State == EntityState.Modified)) { entry.Entity.UpdateDate = DateTime.Now; if(entry.State == EntityState.Added) { entry.Entity.CreateDate = DateTime.Now; } } return base.SaveChangesAsync(cancellationToken); } } }