RepositoryBase.cs 1013 B

123456789101112131415161718192021222324252627
  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 Update(T entity) => RepositoryContext.Set<T>().Update(entity);
  20. public void Delete(T entity) => RepositoryContext.Set<T>().Remove(entity);
  21. public void Detach(T entity) => RepositoryContext.Entry(entity).State = EntityState.Detached;
  22. }