using EgwCoreLib.Lux.Data.DbModel.Cost; using Microsoft.EntityFrameworkCore; namespace EgwCoreLib.Lux.Data.Repository.Cost { public class ResourceRepository : BaseRepository, IResourceRepository { #region Public Constructors public ResourceRepository(IDbContextFactory ctxFactory) : base(ctxFactory) { } #endregion Public Constructors #region Public Methods public async Task AddAsync(ResourceModel entity) { await using var dbCtx = await CreateContextAsync(); await dbCtx.DbSetResource.AddAsync(entity); return await dbCtx.SaveChangesAsync() > 0; } public async Task DeleteAsync(ResourceModel entity) { await using var dbCtx = await CreateContextAsync(); dbCtx.DbSetResource.Remove(entity); return await dbCtx.SaveChangesAsync() > 0; } public async Task> GetAllAsync() { await using var dbCtx = await CreateContextAsync(); return await dbCtx.DbSetResource .Include(d => d.DriverNav) .Include(j => j.JobStepNav) .AsNoTracking() .ToListAsync(); } public async Task GetByIdAsync(int recId) { await using var dbCtx = await CreateContextAsync(); return await dbCtx.DbSetResource.FirstOrDefaultAsync(x => x.ResourceID == recId); } public async Task UpdateAsync(ResourceModel entity) { await using var dbCtx = await CreateContextAsync(); // Recuperiamo l'entità tracciata dal context var trackedEntity = await dbCtx.DbSetResource.FirstOrDefaultAsync(x => x.ResourceID == entity.ResourceID); if (trackedEntity != null) { // Aggiorna i valori dell'entità tracciata con quelli della nuova dbCtx.Entry(trackedEntity).CurrentValues.SetValues(entity); } else { dbCtx.DbSetResource.Update(entity); } return await dbCtx.SaveChangesAsync() > 0; } #endregion Public Methods } }