public List <T> GetAll <T>() where T : class { using (var context = new OnlineTestContext(_dbContextOptionBuilder.Options)) { DbSet <T> dbSet = context.Set <T>(); var entityList = dbSet.AsNoTracking().ToList(); return(entityList); } }
public T Get <T>(int id) where T : class { using (var context = new OnlineTestContext(_dbContextOptionBuilder.Options)) { DbSet <T> dbSet = context.Set <T>(); var entity = dbSet.Find(id); return(entity); } }
public T Delete <T>(T entity) where T : class { using (var context = new OnlineTestContext(_dbContextOptionBuilder.Options)) { DbSet <T> dbSet = context.Set <T>(); dbSet.Remove(entity); context.SaveChanges(); return(entity); } }
public bool AddAll <T>(List <T> entityList) where T : class { using (var context = new OnlineTestContext(_dbContextOptionBuilder.Options)) { try { DbSet <T> dbSet = context.Set <T>(); foreach (var entity in entityList) { dbSet.Add(entity); } context.SaveChanges(); return(true); } catch (Exception ex) { return(false); } } }