public async Task <T> Create(T entity) { using SchoolDBContext context = _contextFactory.CreateDbContext(); EntityEntry <T> createdResult = await context.Set <T>().AddAsync(entity); await context.SaveChangesAsync(); return(createdResult.Entity); }
public async Task <bool> Delete(int id) { using SchoolDBContext context = _contextFactory.CreateDbContext(); T entity = await context.Set <T>().FirstOrDefaultAsync((e) => e.Id == id); context.Set <T>().Remove(entity); await context.SaveChangesAsync(); return(true); }
public async Task <T> Update(int id, T entity) { using SchoolDBContext context = _contextFactory.CreateDbContext(); entity.Id = id; context.Set <T>().Update(entity); await context.SaveChangesAsync(); return(entity); }
public async Task <SchoolItem> GetSchoolAsync(int id) { using SchoolDBContext context = _contextFactory.CreateDbContext(); SchoolModel entity = await context.SchoolModels .FirstOrDefaultAsync((e) => e.Id == id); SchoolItem school = MakeSchoolFromModel(entity); return(school); }
public async Task <T> CreateOrUpdateList(List <T> entity) { using SchoolDBContext context = _contextFactory.CreateDbContext(); foreach (T t in entity) { await context.Set <T>().AddAsync(t); } await context.SaveChangesAsync(); return(null); }
public async Task <List <SchoolItem> > GetSchoolsAsync() { List <SchoolItem> schools = new List <SchoolItem>(); using (SchoolDBContext context = _contextFactory.CreateDbContext()) { IEnumerable <SchoolModel> entities = await context.SchoolModels .ToListAsync(); foreach (SchoolModel schoolModel in entities) { SchoolItem school = MakeSchoolFromModel(schoolModel); schools.Add(school); } } return(schools.ToList <SchoolItem>()); }