/// <summary> /// Deletes entity /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <bool> Delete(int id) { using (TodoAppDbContext context = _contextFactory.CreateDbContext()) { T entity = context.Set <T>().FirstOrDefault((e) => e.Id == id); context.Set <T>().Remove(entity); await context.SaveChangesAsync(); return(true); } }
private static async Task ResetTable <TEntity>(List <TEntity> entities) where TEntity : BaseEntity { IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.Development.json") .Build(); var connectionString = configuration.GetConnectionString("MySqlConnectionTest"); var options = new DbContextOptionsBuilder <TodoAppDbContext>() .UseMySql(connectionString).Options; using (var context = new TodoAppDbContext(options)) { await context.Database.MigrateAsync().ConfigureAwait(false); var dbSet = context.Set <TEntity>(); var allEntities = await dbSet.ToListAsync().ConfigureAwait(false); dbSet.RemoveRange(allEntities); //await context.SaveChangesAsync().ConfigureAwait(false); await dbSet.AddRangeAsync(entities).ConfigureAwait(false); await context.SaveChangesAsync().ConfigureAwait(false); } }
/// <summary> /// Gets all entities /// </summary> /// <returns></returns> public Task <IEnumerable <T> > GetAll() { using (TodoAppDbContext context = _contextFactory.CreateDbContext()) { IEnumerable <T> entities = context.Set <T>().ToList(); return((Task <IEnumerable <T> >)entities); } }
/// <summary> /// Gets entity /// </summary> /// <param name="id"></param> /// <returns></returns> public async Task <T> Get(int id) { using (TodoAppDbContext context = _contextFactory.CreateDbContext()) { T entity = context.Set <T>().FirstOrDefault((e) => e.Id == id); return(entity); } }
/// <summary> /// Gets all entities /// </summary> /// <returns></returns> public List <T> GetAllItems() { using (TodoAppDbContext context = _contextFactory.CreateDbContext()) { List <T> entities = context.Set <T>().ToList(); return(entities); } }
/// <summary> /// Gets all tasks /// </summary> /// <returns></returns> public async Task <IEnumerable <TaskModel> > GetAll() { using (TodoAppDbContext context = _contextFactory.CreateDbContext()) { IEnumerable <TaskModel> entities = await context.Set <TaskModel>().ToListAsync(); return(entities); } }
/// <summary> /// Creates an entity /// </summary> /// <param name="entity"></param> /// <returns></returns> public async Task <T> Create(T entity) { using (TodoAppDbContext context = _contextFactory.CreateDbContext()) { var createdEntity = context.Set <T>().Add(entity); context.SaveChanges(); return(createdEntity); } }
/// <summary> /// Updates entity /// </summary> /// <param name="id"></param> /// <param name="entity"></param> /// <returns></returns> public async Task <T> Update(int id, T entity) { using (TodoAppDbContext context = _contextFactory.CreateDbContext()) { entity.Id = id; context.Set <T>().Add(entity); await context.SaveChangesAsync(); return(entity); } }
public async Task <List <TEntity> > GetEntitiesAsync(Expression <Func <TEntity, bool> > include = null) => await _todoAppDbContext.Set <TEntity>().Where(include).ToListAsync().ConfigureAwait(false);