public virtual async Task <bool> Delete(int id) { using SchedulingContext _context = _contextFactory.CreateDbContext(); _context.Set <T>().Remove(await _context.Set <T>().FindAsync(id)); await _context.SaveChangesAsync(); return(true); }
public virtual async Task <T> Update(T item) { using SchedulingContext _context = _contextFactory.CreateDbContext(); _context.Set <T>().Update(item); await _context.SaveChangesAsync(); return(item); }
protected async Task <T> MakePersistent <T>(T entity) where T : Entity { var entry = Context.Entry(entity); if (entry.State == EntityState.Detached) { T foundEntity = Context.Set <T>().Find(entity.Id); if (foundEntity == null) { entry.State = EntityState.Added; } else { entry = Context.Entry(foundEntity); entry.CurrentValues.SetValues(entity); entry.State = EntityState.Modified; } } try { await Context.SaveChangesAsync(); } catch (Exception ex) { Logger.LogError(ex, "Exception occured! {0}", ex.Message); Logger.LogError(ex.StackTrace); var innerEx = ex.InnerException; while (innerEx != null) { Logger.LogError("----------------------------------------"); Logger.LogError(innerEx, "Exception occured! {0}", innerEx.Message); Logger.LogError(innerEx.StackTrace); innerEx = innerEx.InnerException; } throw; } return(entry.Entity); }
public virtual async Task <T> GetById(int id) { using SchedulingContext _context = _contextFactory.CreateDbContext(); return(await _context.Set <T>().FindAsync(id)); }
public virtual async Task <List <T> > GetAll() { using SchedulingContext _context = _contextFactory.CreateDbContext(); return(await _context.Set <T>().ToListAsync()); }
internal GenericRepository(SchedulingContext context) { this.context = context; this.dbSet = context.Set <TEntity>(); }