RepositoryLog.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using Microsoft.EntityFrameworkCore;
  2. using MTWorkHR.Core.IRepositories.Base;
  3. using MTWorkHR.Infrastructure.DBContext;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MTWorkHR.Infrastructure.Repositories
  12. {
  13. public class RepositoryLog<T>: IRepositoryLog<T> where T : class
  14. {
  15. protected readonly HRDataContext context;
  16. private readonly DbSet<T> dbSet;
  17. public RepositoryLog(HRDataContext _Context)
  18. {
  19. context = _Context;
  20. dbSet = context.Set<T>();
  21. }
  22. public async Task<T> AddAsync(T entity)
  23. {
  24. await dbSet.AddAsync(entity);
  25. return entity;
  26. }
  27. public async Task DeleteAsync(T entity)
  28. {
  29. dbSet.Remove(entity);
  30. }
  31. public async Task<IReadOnlyList<T>> GetAllAsync()
  32. {
  33. return await dbSet.ToListAsync();
  34. }
  35. public async Task<T> GetByIdAsync(long id)
  36. {
  37. return await context.Set<T>().FindAsync(id);
  38. }
  39. public IQueryable<T> AsQueryable()
  40. {
  41. return dbSet.AsQueryable<T>();
  42. }
  43. }
  44. }