RepositoryBase.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 
  2. global using Contracts;
  3. global using Entities;
  4. global using System.Linq.Expressions;
  5. global using Entities.Models;
  6. using Microsoft.EntityFrameworkCore;
  7. namespace Repository;
  8. public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
  9. {
  10. protected RepositoryContext RepositoryContext { get; set; }
  11. public RepositoryBase(RepositoryContext repositoryContext)
  12. {
  13. RepositoryContext = repositoryContext;
  14. }
  15. public IQueryable<T> FindAll() => RepositoryContext.Set<T>().AsNoTracking();
  16. public IQueryable<T> FindByCondition(Expression<Func<T, bool>> expression) =>
  17. RepositoryContext.Set<T>().Where(expression).AsNoTracking();
  18. public void Create(T entity) => RepositoryContext.Set<T>().Add(entity);
  19. public void CreateBulk(ICollection<T> entities) => RepositoryContext.Set<T>().AddRange(entities);
  20. public void UpdateBulk(ICollection<T> entities) => RepositoryContext.Set<T>().UpdateRange(entities);
  21. public void Update(T entity) => RepositoryContext.Set<T>().Update(entity);
  22. public void Delete(T entity) => RepositoryContext.Set<T>().Remove(entity);
  23. public void DeleteBulk(ICollection<T> entities) => RepositoryContext.Set<T>().RemoveRange(entities);
  24. public void Detach(T entity) => RepositoryContext.Entry(entity).State = EntityState.Detached;
  25. public void DetachBulk(ICollection<T> entities)
  26. {
  27. foreach (var item in entities) {
  28. RepositoryContext.Entry(item).State = EntityState.Detached;
  29. }
  30. }
  31. }