コード例 #1
0
 public T Single(Expression <Func <T, bool> > predicate)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         return(context.Set <T>().FirstOrDefault(predicate));
     }
 }
コード例 #2
0
 public List <T> Filter(Expression <Func <T, bool> > predicate)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         return((List <T>)context.Set <T>().Where(predicate).ToList());
     }
 }
コード例 #3
0
 public List <T> GetAll()
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         return((List <T>)context.Set <T>().ToList());
     }
 }
コード例 #4
0
        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();
            }
        }
コード例 #5
0
 public Product GetWithCategory(int productID)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         return(context.Set <Product>().Include("Category").FirstOrDefault(x => x.ProductID == productID));
     }
 }
コード例 #6
0
        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();
            }
        }
コード例 #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));
     }
 }
コード例 #8
0
 public void Create(T entity)
 {
     using (VirtualStoreContext context = new VirtualStoreContext())
     {
         context.Set <T>().Add(entity);
         context.SaveChanges();
     }
 }
コード例 #9
0
 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();
     }
 }
コード例 #10
0
        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());
            }
        }