コード例 #1
0
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #2
0
        public IHttpActionResult PutCustomer(string id, Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer.CustomerID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #3
0
ファイル: HomeController.cs プロジェクト: AgentEmily/MVC
        [HttpPost]//接收USER輸入的新增資料
        public ActionResult Insert()
        {
            Categories _category = new Categories();

            _category.CategoryName    = Request.Form["CategoryName"];
            _category.Description     = Request.Form["Description"];
            db.Entry(_category).State = EntityState.Added;
            db.SaveChanges();

            return(RedirectToAction("Index"));
        }
コード例 #4
0
        public ActionResult Create([Bind(Include = "CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                db.Customers.Add(customer);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(customer));
        }
コード例 #5
0
        public IHttpActionResult PostProducts(Products products)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.Products.Add(products);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = products.ProductID }, products));
        }
コード例 #6
0
        public ActionResult Create([Bind(Include = "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", product.CategoryID);
            ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName", product.SupplierID);
            return(View(product));
        }
コード例 #7
0
        public ActionResult Create([Bind(Include = "OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry")] Order order)
        {
            if (ModelState.IsValid)
            {
                db.Orders.Add(order);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CompanyName", order.CustomerID);
            ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "LastName", order.EmployeeID);
            ViewBag.ShipVia    = new SelectList(db.Shippers, "ShipperID", "CompanyName", order.ShipVia);
            return(View(order));
        }
コード例 #8
0
 public ActionResult Update(Customers willUpdated, bool isUpdate)
 {
     if (!string.IsNullOrEmpty(willUpdated.CustomerID))
     {
         var updatedEntity = dbModel.Entry(willUpdated);
         updatedEntity.State = System.Data.Entity.EntityState.Modified;
         dbModel.SaveChanges();
         return(RedirectToAction("Detail", new { userID = willUpdated.CustomerID, isUpdate = true }));
     }
     else
     {
         var added = dbModel.Entry(willUpdated);
         added.State = System.Data.Entity.EntityState.Added;
         dbModel.SaveChanges();
         return(RedirectToAction("Detail", new { userID = added.Entity.CustomerID, isUpdate = false }));
     }
 }
コード例 #9
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello QueryInterceptor.EntityFrameworkCore");

            Console.WriteLine("Enable ExpressionOptimizer");
            ExtensibilityPoint.QueryOptimizer = ExpressionOptimizer.visit;

            var visitor = new EqualsToNotEqualsVisitor();

            // doit

            IQueryable <int> query = Enumerable.Range(0, 10).AsQueryable().Where(n => n > 0 && n % 2 == 0);

            List <int> numbersEven = query.ToList();

            Console.WriteLine("numbersEven > 0 = {0}", string.Join(", ", numbersEven));


            List <int> numbersOdd = query.InterceptWith(visitor).Where(x => x >= 0).ToList();

            Console.WriteLine("numbersOdd  > 0 = {0}", string.Join(", ", numbersOdd));

            var context = new NorthwindModel();

            if (context.Database.EnsureCreated())
            {
                context.Cars.Add(new Car {
                    Brand = "Ford", Color = "Blue"
                });
                context.Cars.Add(new Car {
                    Brand = "Fiat", Color = "Red"
                });
                context.Cars.Add(new Car {
                    Brand = "Alfa", Color = "Black"
                });
                context.SaveChanges();
            }

            var carFirstOrDefault = context.Cars.AsQueryable().InterceptWith(visitor).Where(x => x.Color == "Blue").FirstOrDefault();

            Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented));

            var carFirstOrDefaultAsync = context.Cars.AsQueryable().InterceptWith(visitor).Where(x => x.Color == "Blue").FirstOrDefaultAsync();

            Console.WriteLine("carAsync {0}", JsonConvert.SerializeObject(carFirstOrDefaultAsync.Result, Formatting.Indented));

            var carToListAsync = context.Cars.AsQueryable().InterceptWith(visitor).Where(x => x.Color == "Blue").ToListAsync();

            Console.WriteLine("carToListAsync {0}", JsonConvert.SerializeObject(carToListAsync.Result, Formatting.Indented));

            Console.WriteLine("Press key...");
            Console.ReadLine();
        }
コード例 #10
0
        public IHttpActionResult PutProducts(int id, Products viewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != viewModel.ProductID)
            {
                return(BadRequest());
            }

            //db.Entry(viewModel).State = EntityState.Modified;

            var produto = db.Products.Find();

            db.Entry(produto).CurrentValues.SetValues(viewModel);

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #11
0
ファイル: Repository.cs プロジェクト: mort79f8/S3.Northwind
 public void Update(Employee employee)
 {
     model.Employees.AddOrUpdate(employee);
     model.SaveChanges();
 }
コード例 #12
0
 public void Commit()
 {
     _ctx.SaveChanges();
 }
コード例 #13
0
 public void Update(Employee employee)
 {
     model.Employees.AddOrUpdate(employee);  // Husk using System.Data.Entity.Migrations;
     model.SaveChanges();
 }