Ejemplo n.º 1
0
        /// <summary>
        /// Applies a filter and a query and returns the resulting list of items.
        /// </summary>
        /// <param name="filter">The filter<see cref="Expression{Func{T, bool}}"/>.</param>
        /// <param name="orderBy">The orderBy<see cref="Func{IQueryable{T}, IOrderedQueryable{T}}"/>.</param>
        /// <param name="includeProperties">The includeProperties<see cref="string"/>.</param>
        /// <returns>The result of the query.</returns>
        public virtual IEnumerable <T> Get(
            Expression <Func <T, bool> > filter = null,
            Func <IQueryable <T>, IOrderedQueryable <T> > orderBy = null,
            string includeProperties = "")
        {
            using (var ctx = new AuctionDBContext())
            {
                var dbSet = ctx.Set <T>();

                IQueryable <T> query = dbSet;

                foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                {
                    query = query.Include(includeProperty);
                }

                if (filter != null)
                {
                    query = query.Where(filter);
                }

                if (orderBy != null)
                {
                    return(orderBy(query).ToList());
                }
                else
                {
                    return(query.ToList());
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Retrieves the entity identified by the id.
 /// </summary>
 /// <param name="id">The id<see cref="object"/>.</param>
 /// <returns>The object identified by the id.</returns>
 public virtual T GetByID(object id)
 {
     using (var ctx = new AuctionDBContext())
     {
         return(ctx.Set <T>().Find(id));
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// The Insert.
        /// </summary>
        /// <param name="entity">The entity<see cref="T"/>.</param>
        public virtual void Insert(T entity)
        {
            using (var ctx = new AuctionDBContext())
            {
                var dbSet = ctx.Set <T>();
                dbSet.Add(entity);

                ctx.SaveChanges();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// The Update.
        /// </summary>
        /// <param name="item">The item<see cref="T"/>.</param>
        public virtual void Update(T item)
        {
            using (var ctx = new AuctionDBContext())
            {
                var dbSet = ctx.Set <T>();
                dbSet.Attach(item);
                ctx.Entry(item).State = EntityState.Modified;

                ctx.SaveChanges();
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// The Delete.
        /// </summary>
        /// <param name="entityToDelete">The entityToDelete<see cref="T"/>.</param>
        public virtual void Delete(T entityToDelete)
        {
            using (var ctx = new AuctionDBContext())
            {
                var dbSet = ctx.Set <T>();

                if (ctx.Entry(entityToDelete).State == EntityState.Detached)
                {
                    dbSet.Attach(entityToDelete);
                }

                dbSet.Remove(entityToDelete);

                ctx.SaveChanges();
            }
        }
Ejemplo n.º 6
0
 public void Create(T item)
 {
     _context.Set <T>().Add(item);
 }