Exemplo n.º 1
0
        public void UpdateProduct(int id, ProductwithUnitsViewModel products)
        {
            var p = (from g in _context.Products where g.ProductId == id select g).SingleOrDefault();

            if (p == null)
            {
                throw new Exception("Not Found.");
            }

            ProductUnits u = (from i in _context.ProductUnits where i.IsThisPrimaryUnit == true
                              where i.ProductId == p.ProductId select i).SingleOrDefault();

            p.ProductUnits.Remove(u);
            _context.SaveChanges();
            //foreach (var rw in u )
            //{
            //    _context.ProductUnits.Remove(rw);
            //}



            p.ProductGroupId = products.ProductGroupId;
            p.ProductName    = products.ProductName;
            p.IsActive       = products.IsActive;
            p.ProductUnits   = new List <ProductUnits>
            {
                new ProductUnits {
                    IsThisPrimaryUnit = true, UnitId = products.UnitId
                }
            };
            _context.SaveChanges();
        }
Exemplo n.º 2
0
        public void AddProuct(ProductwithUnitsViewModel products)
        {
            var p = new Models.Masters.Products
            {
                ProductGroupId = products.ProductGroupId,
                ProductName    = products.ProductName,
                IsActive       = products.IsActive,
                ProductUnits   = new List <ProductUnits>
                {
                    new ProductUnits {
                        IsThisPrimaryUnit = true, UnitId = products.UnitId
                    }
                }
            };

            _context.Products.Add(p);
            _context.SaveChanges();
        }