hrms-api-backend/BMA.EHR.Infrastructure/Persistence/LeaveDbContext.cs

79 lines
2.4 KiB
C#
Raw Normal View History

using BMA.EHR.Application.Common.Interfaces;
2023-11-27 11:52:28 +07:00
using BMA.EHR.Domain.Models.Leave.Commons;
using BMA.EHR.Domain.Models.Leave.Requests;
using BMA.EHR.Domain.Models.Leave.TimeAttendants;
using Microsoft.EntityFrameworkCore;
namespace BMA.EHR.Infrastructure.Persistence
{
public class LeaveDbContext : DbContext, ILeaveDbContext
{
#region " Check-In "
public DbSet<DutyTime> DutyTimes { get; set; }
public DbSet<UserTimeStamp> UserTimeStamps { get; set; }
public DbSet<ProcessUserTimeStamp> ProcessUserTimeStamps { get; set; }
2023-11-23 10:35:55 +07:00
public DbSet<UserDutyTime> UserDutyTimes { get; set; }
public DbSet<AdditionalCheckRequest> AdditionalCheckRequests { get; set; }
public DbSet<UserCalendar> UserCalendars { get; set; }
public DbSet<CheckInJobStatus> CheckInJobStatuses { get; set; }
2023-11-27 11:52:28 +07:00
#endregion
#region " Leave System "
public DbSet<LeaveType> LeaveTypes { get; set; }
public DbSet<LeaveRequest> LeaveRequests { get; set; }
public DbSet<LeaveDocument> LeaveDocuments { get; set; }
public DbSet<LeaveBeginning> LeaveBeginnings { get; set; }
2023-11-27 11:52:28 +07:00
public DbSet<LeaveRequestApprover> LeaveRequestApprovers { get; set; }
#endregion
public DbSet<LeaveProcessJobStatus> LeaveProcessJobStatuses { get; set; }
public LeaveDbContext(DbContextOptions<LeaveDbContext> options) : base(options)
{
}
public Task<int> SaveChangesAsync()
{
return base.SaveChangesAsync();
}
public void Attatch<T>(T entity) where T : class
{
Attach(entity);
}
public void Detach<T>(T entity) where T : class
{
base.Entry(entity).State = EntityState.Detached;
}
2026-06-19 10:51:18 +07:00
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Composite unique index on the natural key for LeaveBeginning.
// Prevents duplicate rows when concurrent requests (e.g. UI calling /user/check twice)
// race through the get-or-create flow in LeaveBeginningRepository.
modelBuilder.Entity<LeaveBeginning>()
.HasIndex(b => new { b.ProfileId, b.LeaveYear, b.LeaveTypeId })
.HasDatabaseName("IX_LeaveBeginnings_ProfileId_LeaveYear_LeaveTypeId")
.IsUnique();
}
}
}