public T Single(Expression <Func <T, bool> > predicate)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         return(context.Set <T>().FirstOrDefault(predicate));
     }
 }
 public List <T> Filter(Expression <Func <T, bool> > predicate)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         return((List <T>)context.Set <T>().Where(predicate).ToList());
     }
 }
 public List <T> GetAll()
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         return((List <T>)context.Set <T>().ToList());
     }
 }
        public virtual void Create(T entity, List <Expression <Func <T, object> > > unchangeProp)
        {
            // se obtiene la lista de propiedades que deben marcarse con el estado Unchanged
            List <string> unchangelist = unchangeProp.Select(x => ((MemberExpression)x.Body).Member.Name).ToList();

            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                context.Set <T>().Add(entity);

                if (unchangeProp != null)
                {
                    // se toma la instancia del objeto que esta asignada a la propiedad
                    // y se asigna el estodo Unchanged
                    foreach (string property in unchangelist)
                    {
                        PropertyInfo propertyInfo = typeof(T).GetProperty(property);
                        var          value        = propertyInfo.GetValue(entity, null);

                        context.Entry(value).State = EntityState.Unchanged;
                    }
                }

                context.SaveChanges();
            }
        }
Example #5
0
 public Product GetWithCategory(int productID)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         return(context.Set <Product>().Include("Category").FirstOrDefault(x => x.ProductID == productID));
     }
 }
        public void RemoveEmployees(Territory territory, List <Employee> employees)
        {
            //validamos que haya algo que remover
            if (employees == null || employees.Count == 0)
            {
                return;
            }

            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                //recuperamos el terrotorio y sus empleados
                //esto es necesario porque el objeto donde se debe remover tiene que estar dentro del contexto de EF
                Territory territorySel = context.Set <Territory>().Include("Employees").FirstOrDefault(x => x.TerritoryId == territory.TerritoryId);

                if (territory.Employees == null || territory.Employees.Count == 0)
                {
                    return;
                }

                employees.ForEach(x =>
                {
                    //localizamos al empleado dentro de la coleccion que se recupero anteriormente
                    Employee employeeRemove = territorySel.Employees.First(e => e.EmployeeId == x.EmployeeId);
                    //se remueve de la coleccion haciendo uso de la instancia
                    territorySel.Employees.Remove(employeeRemove);
                });

                context.SaveChanges();
            }
        }
Example #7
0
 //El método GetById() podría haberse omitido ya que el mismo dato podría haberse recuperado
 //mediante la el método Single() que define el repositorio base, utilizando
 //Category category = new CategoryRepository().Simple(x => x.CategoryID == categoryId);
 public Category GetById(int categoryID)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         return(context.Set <Category>().FirstOrDefault(x => x.CategoryId == categoryID));
     }
 }
 public void Create(T entity)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         context.Set <T>().Add(entity);
         context.SaveChanges();
     }
 }
 public void Delete(Expression <Func <T, bool> > predicate)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         var entities = context.Set <T>().Where(predicate).ToList();
         entities.ForEach(x => context.Entry(x).State = EntityState.Deleted);
         context.SaveChanges();
     }
 }
        public List <T> Filter(Expression <Func <T, bool> > predicate, List <Expression <Func <T, object> > > includes)
        {
            List <string> includelist = new List <string>();

            foreach (var item in includes)
            {
                MemberExpression body = item.Body as MemberExpression;
                if (body == null)
                {
                    throw new ArgumentException("The body must be a member expression");
                }

                includelist.Add(body.Member.Name);
            }

            using (VirtualStoreContext context = new VirtualStoreContext())
            {
                DbQuery <T> query = context.Set <T>();

                includelist.ForEach(x => query = query.Include(x));

                return((List <T>)query.Where(predicate).ToList());
            }
        }