12345678910111213141516171819202122232425262728293031323334353637 |
-
- global using Contracts;
- global using Entities;
- global using System.Linq.Expressions;
- global using Entities.Models;
- using Microsoft.EntityFrameworkCore;
- namespace Repository;
- public abstract class RepositoryBase<T> : IRepositoryBase<T> where T : class
- {
- protected RepositoryContext RepositoryContext { get; set; }
- public RepositoryBase(RepositoryContext repositoryContext)
- {
- RepositoryContext = repositoryContext;
- }
- public IQueryable<T> FindAll() => RepositoryContext.Set<T>().AsNoTracking();
- public IQueryable<T> FindByCondition(Expression<Func<T, bool>> expression) =>
- RepositoryContext.Set<T>().Where(expression).AsNoTracking();
- public void Create(T entity) => RepositoryContext.Set<T>().Add(entity);
- public void CreateBulk(ICollection<T> entities) => RepositoryContext.Set<T>().AddRange(entities);
- public void UpdateBulk(ICollection<T> entities) => RepositoryContext.Set<T>().UpdateRange(entities);
- public void Update(T entity) => RepositoryContext.Set<T>().Update(entity);
- public void Delete(T entity) => RepositoryContext.Set<T>().Remove(entity);
- public void DeleteBulk(ICollection<T> entities) => RepositoryContext.Set<T>().RemoveRange(entities);
- public void Detach(T entity) => RepositoryContext.Entry(entity).State = EntityState.Detached;
- public void DetachBulk(ICollection<T> entities)
- {
- foreach (var item in entities) {
- RepositoryContext.Entry(item).State = EntityState.Detached;
- }
- }
- }
|