public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.Id)
            {
                return(BadRequest());
            }

            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Beispiel #2
0
        // This method will cre<ate and seed the database.
        public void Initialize(ProductsAppContext context)
        {
            // Create the database, if it does not already exists. If the database
            // already exists, no action is taken (and no effort is made to ensure it
            // is compatible with the model for this context).
            context.Database.EnsureCreated();

            //Look for any TodoItems
            if (context.Products.Any())
            {
                // Delete and re-create the database, if it had already been created.
                // You must delete all the tables in the database. We do this, because
                // "context.Database.EnsureDeleted()" doesn't work on an Azure SQL
                // database with our type of subscription.
                // The statements below doesn't work on SqLite. This is the reason why
                // we have two database initializer classes (one for SqLite and one for
                // SQL Server.

                //ERROR context.Database.ExecuteSqlRaw("DROP TABLE Products");
                context.Database.EnsureCreated();
            }

            List <Product> products = new List <Product>
            {
                new Product {
                    IsComplete = true, Name = "Use SQL Server"
                }
            };

            context.Products.AddRange(products);
            context.SaveChanges();
        }
        public Product CreateProduct(Product product)
        {
            Product prod = _context.Products.Add(product).Entity;

            _context.SaveChanges();
            return(prod);
        }
        // This method will cre<ate and seed the database.
        public void Initialize(ProductsAppContext context)
        {
            // Delete the database, if it already exists. You need to clean and build
            // the solution for this to take effect.
            context.Database.EnsureDeleted();

            // Create the database, if it does not already exists. If the database
            // already exists, no action is taken (and no effort is made to ensure it
            // is compatible with the model for this context).
            context.Database.EnsureCreated();

            List <Product> products = new List <Product>
            {
                new Product {
                    IsComplete = true, Name = "Use SqLite"
                },
                new Product {
                    IsComplete = false, Name = "Exam project"
                }
            };

            context.Products.AddRange(products);
            context.SaveChanges();
        }