예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        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>());
        }