Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Repository.cs 1.6KB

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