You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EFRepository.cs 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using BlackRock.Reporting.API.Core.Interfaces;
  2. using BlackRock.Reporting.API.Core.Models;
  3. using Microsoft.EntityFrameworkCore;
  4. namespace BlackRock.Reporting.API.Persistence.Repositories
  5. {
  6. public class EFRepository<TEntity> : IRepository<TEntity> where TEntity : class, IBaseEntity
  7. {
  8. private readonly BRDbContext context;
  9. public EFRepository(BRDbContext context)
  10. {
  11. this.context = context;
  12. }
  13. public async Task<IEnumerable<TEntity>> GetAllAsync()
  14. {
  15. return await context.Set<TEntity>().ToListAsync();
  16. }
  17. public async Task<TEntity> GetByIdAsync(int id)
  18. {
  19. return await context.Set<TEntity>()
  20. .FindAsync(id);
  21. }
  22. public async Task AddAsync(TEntity entity)
  23. {
  24. await context.Set<TEntity>().AddAsync(entity);
  25. }
  26. public void Update(TEntity entity)
  27. {
  28. context.Set<TEntity>().Update(entity);
  29. }
  30. public void UpdateRange(IEnumerable<TEntity> entities)
  31. {
  32. context.Set<TEntity>().UpdateRange(entities);
  33. }
  34. public async Task AddRangeAsync(IEnumerable<TEntity> entities)
  35. {
  36. await context.Set<TEntity>().AddRangeAsync(entities);
  37. }
  38. public void Remove(TEntity entity)
  39. {
  40. context.Set<TEntity>().Remove(entity);
  41. }
  42. public void RemoveRange(IEnumerable<TEntity> entities)
  43. {
  44. context.Set<TEntity>().RemoveRange(entities);
  45. }
  46. }
  47. }