phronCare/Models/Repositories/GenericRepository.cs
Leandro Hernan Rojas 26ad549a0e
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 2m56s
Add Patch 2 in API
2025-04-03 17:48:18 -03:00

44 lines
961 B
C#

using Microsoft.EntityFrameworkCore;
using Models.Interfaces;
namespace Models.Repositories
{
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
private readonly DbContext _context;
private readonly DbSet<T> _dbSet;
public GenericRepository(DbContext context)
{
_context = context;
_dbSet = _context.Set<T>();
}
public async Task<T?> GetByIdAsync(int id)
{
return await _dbSet.FindAsync(id);
}
public async Task<IEnumerable<T>> GetAllAsync()
{
return await _dbSet.ToListAsync();
}
public async Task AddAsync(T entity)
{
await _dbSet.AddAsync(entity);
}
public void Update(T entity)
{
_dbSet.Update(entity);
}
public void Delete(T entity)
{
_dbSet.Remove(entity);
}
}
}