12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- 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<HRDataContext> options) : base(options) { }
- public DbSet<Company> Companies { get; set; }
- public DbSet<Project> Projects { get; set; }
- // public DbSet<AttachmentType> AttachmentTypes { get; set; }
- public DbSet<UserTaskStatus> UserTaskStatuses { get; set; }
- public DbSet<UserTaskAttachment> UserTaskAttachments { get; set; }
- public DbSet<UserTask> UserTasks { get; set; }
- public DbSet<UserTaskHistory> UserTaskHistories { get; set; }
- //------------------------Logs------------------------
- public DbSet<UserLog> UserLogs { get; set; }
- public DbSet<AuthLog> AuthLogs { get; set; }
- public DbSet<FileLog> FileLogs { get; set; }
- public DbSet<RoleLog> RoleLogs { get; set; }
- public DbSet<SettingLog> SettingLogs { get; set; }
- protected override void OnModelCreating(ModelBuilder modelBuilder)
- {
- base.OnModelCreating(modelBuilder);
- SetGlobalFilters(modelBuilder);
- modelBuilder.ApplyConfigurationsFromAssembly(typeof(HRDataContext).Assembly);
- }
- public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
- {
- foreach (var entry in base.ChangeTracker.Entries<AuditEntity>().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);
- }
- private void SetGlobalFilters(ModelBuilder builder)
- {
- builder.SetQueryFilterOnAllEntities<ISoftDelete>(p => !p.IsDeleted);
- }
- }
- }
|