示例#1
0
        public async Task Update <T>(T model, int id) where T : class
        {
            MVCSchoolAppContext context = new MVCSchoolAppContext();

            try
            {
                T modelFound = await context.Set <T>().FindAsync(id);

                if (modelFound != null)
                {
                    context.Entry(modelFound).CurrentValues.SetValues(model);
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
        public static List <Student> Search(string searchParameter)
        {
            MVCSchoolAppContext context = new MVCSchoolAppContext();

            return(context.Students.Where(s => s.FirstName.Contains(searchParameter) ||
                                          s.LastName.Contains(searchParameter) ||
                                          s.Email.Contains(searchParameter) ||
                                          s.Phone.Contains(searchParameter)).ToList());
        }
示例#3
0
        public async Task <List <T> > Read <T>() where T : class
        {
            MVCSchoolAppContext context = new MVCSchoolAppContext();

            try
            {
                return(await context.Set <T>().ToListAsync());
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
示例#4
0
        public async Task <T> SearchById <T>(int?id) where T : class
        {
            MVCSchoolAppContext context = new MVCSchoolAppContext();

            try
            {
                return(await context.Set <T>().FindAsync(id));
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
示例#5
0
        public async Task Create <T>(T model) where T : class
        {
            MVCSchoolAppContext context = new MVCSchoolAppContext();

            try
            {
                context.Set <T>().Add(model);
                await context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
示例#6
0
        public async Task Delete <T>(int id) where T : class
        {
            MVCSchoolAppContext context = new MVCSchoolAppContext();

            try
            {
                var result = await context.Set <T>().FindAsync(id);

                context.Set <T>().Remove(result);
                await context.SaveChangesAsync();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }