using Microsoft.EntityFrameworkCore; using SharedLibraryCore.Database; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace SharedLibraryCore.Services { // https://stackoverflow.com/questions/43677906/crud-operations-with-entityframework-using-generic-type public class GenericRepository where TEntity : class { private DatabaseContext _context; private DbSet _dbSet; protected DbContext Context { get { if (_context == null) { _context = new DatabaseContext(); } return _context; } } protected DbSet DBSet { get { if (_dbSet == null) { _dbSet = this.Context.Set(); } return _dbSet; } } public virtual async Task> FindAsync(Expression> predicate, Func, IOrderedQueryable> orderExpression = null) { return await this.GetQuery(predicate, orderExpression).ToListAsync(); } public virtual IEnumerable Find(Expression> predicate, Func, IOrderedQueryable> orderExpression = null) { return this.GetQuery(predicate, orderExpression).AsEnumerable(); } public virtual IQueryable GetQuery(Expression> predicate = null, Func, IOrderedQueryable> orderExpression = null) { IQueryable qry = this.DBSet; foreach (var property in this.Context.Model.FindEntityType(typeof(TEntity)).GetNavigations()) qry = qry.Include(property.Name); if (predicate != null) qry = qry.Where(predicate); if (orderExpression != null) return orderExpression(qry); return qry; } public virtual void Insert(T entity) where T : class { DbSet dbSet = this.Context.Set(); dbSet.Add(entity); } public virtual TEntity Insert(TEntity entity) { return DBSet.Add(entity).Entity; } public virtual void Update(T entity) where T : class { DbSet dbSet = this.Context.Set(); dbSet.Attach(entity); this.Context.Entry(entity).State = EntityState.Modified; } public virtual void Update(TEntity entity) { this.Attach(entity); this.Context.Entry(entity).State = EntityState.Modified; } public virtual void Delete(T entity) where T : class { DbSet dbSet = this.Context.Set(); if (this.Context.Entry(entity).State == EntityState.Detached) dbSet.Attach(entity); dbSet.Remove(entity); } public virtual void Delete(TEntity entity) { if (this.Context.Entry(entity).State == EntityState.Detached) this.Attach(entity); this.DBSet.Remove(entity); } public virtual void Delete(object[] id) where T : class { DbSet dbSet = this.Context.Set(); T entity = dbSet.Find(id); dbSet.Attach(entity); dbSet.Remove(entity); } public virtual void Delete(object id) { TEntity entity = this.DBSet.Find(id); this.Delete(entity); } public virtual void Attach(TEntity entity) { if (this.Context.Entry(entity).State == EntityState.Detached) this.DBSet.Attach(entity); } public virtual void SaveChanges() { this.Context.SaveChanges(); } public virtual Task SaveChangesAsync() { return this.Context.SaveChangesAsync(); } } }