Exemple #1
0
        public async Task <T> AddAsync(T entity)
        {
            context.Set <T>().Add(entity);
            await context.SaveChangesAsync();

            return(entity);
        }
        public virtual IList <T> GetList(Func <T, bool> where,
                                         params Expression <Func <T, object> >[] navigationProperties)
        {
            try
            {
                List <T> list;
                using (var context = new CheckoutDbContext())
                {
                    IQueryable <T> dbQuery = context.Set <T>();

                    //Apply eager loading
                    foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                    {
                        dbQuery = dbQuery.Include <T, object>(navigationProperty);
                    }

                    list = dbQuery
                           .AsNoTracking()
                           .Where(where)
                           .ToList <T>();
                }
                return(list);
            }
            catch (DbEntityValidationException e)
            {
                Log(FleshExeptionOut(e));
                throw;
            }
        }
        public virtual T GetSingle(Func <T, bool> where,
                                   params Expression <Func <T, object> >[] navigationProperties)
        {
            try
            {
                T item = null;
                using (var context = new CheckoutDbContext())
                {
                    IQueryable <T> dbQuery = context.Set <T>();

                    //Apply eager loading
                    foreach (Expression <Func <T, object> > navigationProperty in navigationProperties)
                    {
                        dbQuery = dbQuery.Include <T, object>(navigationProperty);
                    }

                    item = dbQuery
                           //.AsNoTracking() //Don't track any changes for the selected item
                           .FirstOrDefault(where); //Apply where clause
                }
                return(item);
            }
            catch (DbEntityValidationException ex)
            {
                Log(FleshExeptionOut(ex));
                throw;
            }
        }