123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
- using Microsoft.EntityFrameworkCore;
- using MTWorkHR.Core.Entities.Base;
- using MTWorkHR.Identity.Models;
- namespace MTWorkHR.Identity.DBContext
- {
- public class HRIdentityDBContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
- {
- // private readonly GlobalInfo _globalInfo;
- public HRIdentityDBContext(DbContextOptions<HRIdentityDBContext> options/*, GlobalInfo globalInfo*/) : base(options) {
- // this._globalInfo = globalInfo;
- }
- public DbSet<Permission> Permissions { get; set; }
- public DbSet<RolePermission> RolePermissions { get; set; }
- protected override void OnModelCreating(ModelBuilder builder)
- {
- base.OnModelCreating(builder);
- builder.ApplyConfigurationsFromAssembly(typeof(HRIdentityDBContext).Assembly);
- }
- #region SaveChanges
- public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
- {
- BeforeSaveProccess();
- return base.SaveChangesAsync(cancellationToken);
- }
- public override int SaveChanges()
- {
- BeforeSaveProccess();
- return base.SaveChanges();
- }
- public override int SaveChanges(bool acceptAllChangesOnSuccess)
- {
- BeforeSaveProccess();
- return base.SaveChanges(acceptAllChangesOnSuccess);
- }
- private void BeforeSaveProccess()
- {
- var changes = from e in this.ChangeTracker.Entries()
- where e.State != EntityState.Unchanged
- select e;
- foreach (var change in changes)
- {
- if (change.State == EntityState.Added)
- {
- //if (change.Entity.GetType() == typeof(IAuditEntityx<>))
- if (change.Entity is IAudit)
- {
- // ((IAudit)change.Entity).CreateUser = _globalInfo.UserId;
- ((IAudit)change.Entity).CreateDate = DateTime.Now;
- }
- }
- else if (change.State == EntityState.Modified)
- {
- if (change.Entity is IAudit
- && ((change.Entity is IFullAudit && !((IFullAudit)change.Entity).IsDeleted) || change.Entity is not IFullAudit))
- {
- // ((IAudit)change.Entity).UpdateUser = _globalInfo.UserId;
- ((IAudit)change.Entity).UpdateDate = DateTime.Now;
- }
- if (change.Entity is IFullAudit && ((IFullAudit)change.Entity).IsDeleted)
- {
- // ((IFullAudit)change.Entity).DeleteUserId = _globalInfo.UserId;
- }
- }
- else if (change.State == EntityState.Deleted)
- {
- }
- }
- }
- #endregion
- }
- }
|