Ejemplo n.º 1
0
 //新增資料方法
 public void InsertProducts(Products newData)
 {
     //將資料加入資料庫實體
     db.Products.Add(newData);
     //儲存資料庫變更
     db.SaveChanges();
 }
Ejemplo n.º 2
0
        public IHttpActionResult PutCategory(int id, Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != category.CategoryID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 3
0
        public IHttpActionResult PutSupplier(int id, Supplier supplier)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != supplier.SupplierID)
            {
                return(BadRequest());
            }

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

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
0
        public virtual bool Insert(T entity)
        {
            bool result;

            try
            {
                if (entity == null)
                {
                    throw new ArgumentNullException("entity");
                }

                Table.Add(entity);
                Context.SaveChanges();
                result = true;
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        msg += $"Property: {validationError.PropertyName} Error: {validationError.ErrorMessage}";
                    }
                }

                var fail = new Exception(msg, dbEx);
                //throw fail;
                result = false;
            }

            return(result);
        }
Ejemplo n.º 6
0
 //Ekleme
 public void Add(Category category)
 {
     if (category != null)
     {
         db.Categories.Add(category);
         db.SaveChanges();
     }
 }
Ejemplo n.º 7
0
        public void Duzenle(Shipper model)
        {
            Shipper orjinalKat = Bul(model.ShipperID);

            orjinalKat.CompanyName = model.CompanyName;
            orjinalKat.Phone       = model.Phone;
            db.SaveChanges();
        }
Ejemplo n.º 8
0
        public void Duzenle(Category model)
        {
            Category orjinalKat = Bul(model.CategoryID);

            orjinalKat.CategoryName = model.CategoryName;
            orjinalKat.Description  = model.Description;
            db.SaveChanges();
        }
Ejemplo n.º 9
0
        private static void DeleteNewInsertedCustomer()
        {
            var customer = context.Customers.First();

            context.Customers.Remove(customer);
            context.SaveChanges();
            Console.WriteLine("(Row affected)");
        }
Ejemplo n.º 10
0
 public void Delete(TEntity entity)
 {
     if (db.Entry(entity).State == EntityState.Detached)
     {
         dbSet.Attach(entity);
     }
     dbSet.Remove(entity);
     db.SaveChanges();
 }
Ejemplo n.º 11
0
        public void Duzenle(Product model)
        {
            Product orjinalKat = Bul(model.ProductID);

            orjinalKat.ProductName  = model.ProductName;
            orjinalKat.Category     = model.Category;
            orjinalKat.UnitPrice    = model.UnitPrice;
            orjinalKat.UnitsInStock = model.UnitsInStock;
            db.SaveChanges();
        }
Ejemplo n.º 12
0
        public static void Main()
        {
            using (var db = new NORTHWNDEntities())
            {
                string companyName = "Telerik";

                var telerikCustomers = from c in db.Customers
                                       where c.CompanyName == companyName
                                       select c;

                foreach (var customer in telerikCustomers)
                {
                    db.Customers.Remove(customer);
                }

                db.SaveChanges();

                // Create and add customer in NORTHWND database
                Customer testCustomer = new Customer()
                {
                    CompanyName = companyName,
                    CustomerID = "ABC42"
                };

                Console.WriteLine("First customer is added");
                db.Customers.Add(testCustomer);
                Console.WriteLine("Changes saved");
                db.SaveChanges();

                // Create two other customers and try to add them in NORTHWND database
                // If we try to add 'incorrectTestCustomer' SaveChanges will rollback operation (no customers added)
                // If we comment adding 'incorrectTestCustomer' this will work correct (will commit changes)
                Customer secondTestCustomer = new Customer()
                {
                    CompanyName = companyName,
                    CustomerID = "ABC98"
                };

                Customer incorrectTestCustomer = new Customer()
                {
                    CompanyName = companyName,
                    CustomerID = "ABC98"
                };

                Console.WriteLine("Second customer is added");
                db.Customers.Add(secondTestCustomer);

                Console.WriteLine("Adding incorrect Customer");
                // Uncomment line below. This will trow exception and call rollback.
                //db.Customers.Add(incorrectTestCustomer);
                //Console.WriteLine("Trying to save changes. This will throw exception");

                db.SaveChanges();
            }
        }
Ejemplo n.º 13
0
        public ActionResult Create([Bind(Include = "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Products products)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(products);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(products));
        }
Ejemplo n.º 14
0
        public ActionResult Create([Bind(Include = "CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax")] Customers customers)
        {
            if (ModelState.IsValid)
            {
                db.Customers.Add(customers);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(customers));
        }
        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"));
            }

            return(View(order));
        }
        public HttpResponseMessage Post(Shippers ship)
        {
            db.Shippers.Add(ship);
            int EtkilenenSatirSayisi = db.SaveChanges();

            if (EtkilenenSatirSayisi > 0)
            {
                return(Request.CreateResponse(HttpStatusCode.Created));
            }
            return(Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, "Nesne oluşturulamadı"));
        }
Ejemplo n.º 17
0
        public ActionResult Create([Bind(Include = "user_name,phone,email,pass,role")] User user)
        {
            if (ModelState.IsValid)
            {
                db.User.Add(user);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(user));
        }
Ejemplo n.º 18
0
        public string CreateEmployee(Employee employee)
        {
            if (!ModelState.IsValid)
            {
                return("Model is invalid");
            }
            _db.Employees.Add(employee);
            var result = _db.SaveChanges();

            return("Cource is created");
        }
Ejemplo n.º 19
0
        public ActionResult Create([Bind(Include = "CategoryID,CategoryName,Description,Picture")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(category));
        }
Ejemplo n.º 20
0
        public ActionResult Create([Bind(Include = "UserID,UserName,Password")] Login login)
        {
            if (ModelState.IsValid)
            {
                db.Logins.Add(login);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(login));
        }
Ejemplo n.º 21
0
        public void Duzenle(Employee model)
        {
            Employee orjinalKat = Bul(model.EmployeeID);

            orjinalKat.FirstName = model.FirstName;
            orjinalKat.LastName  = model.LastName;
            orjinalKat.Title     = model.Title;
            orjinalKat.Address   = model.Address;
            orjinalKat.City      = model.City;
            orjinalKat.Country   = model.Country;
            db.SaveChanges();
        }
        public void Duzenle(Supplier model)
        {
            Supplier orjinalKat = Bul(model.SupplierID);

            orjinalKat.CompanyName  = model.CompanyName;
            orjinalKat.ContactName  = model.ContactName;
            orjinalKat.ContactTitle = model.ContactTitle;
            orjinalKat.City         = model.City;
            orjinalKat.Country      = model.Country;
            orjinalKat.Address      = model.Address;
            db.SaveChanges();
        }
Ejemplo n.º 23
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);
            return(View(product));
        }
Ejemplo n.º 24
0
        public ActionResult Create(Employees employees)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employees);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ReportsTo = new SelectList(db.Employees, "EmployeeID", "LastName", employees.ReportsTo);
            return(View(employees));
        }
Ejemplo n.º 25
0
        public ActionResult Create([Bind(Include = "EmployeeID,LastName,FirstName,Title,TitleOfCourtesy,BirthDate,HireDate,Address,City,Region,PostalCode,Country,HomePhone,Extension,Photo,Notes,ReportsTo,PhotoPath")] Employees employees)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employees);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ReportsTo = new SelectList(db.Employees, "EmployeeID", "LastName", employees.ReportsTo);
            return(View(employees));
        }
Ejemplo n.º 26
0
        public ActionResult Create(Products products)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(products);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "CategoryName", products.CategoryID);
            ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "CompanyName", products.SupplierID);
            return(View(products));
        }
Ejemplo n.º 27
0
        public ActionResult Create([Bind(Include = "OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry")] Orders orders)
        {
            if (ModelState.IsValid)
            {
                db.Orders.Add(orders);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerID", "CompanyName", orders.CustomerID);
            ViewBag.EmployeeID = new SelectList(db.Employees, "EmployeeID", "LastName", orders.EmployeeID);
            ViewBag.ShipVia    = new SelectList(db.Shippers, "ShipperID", "CompanyName", orders.ShipVia);
            return(View(orders));
        }
Ejemplo n.º 28
0
        public string AddCustomer(Customer customer)
        {
            var newcustomer = _dbContext.Customers.Add(customer);

            _dbContext.SaveChanges();
            if (newcustomer != null)
            {
                return(customer.CustomerID);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 29
0
        public ActionResult Delete(int id)
        {
            Shipper shipper = db.Shippers.Find(id);

            if (shipper != null)
            {
                db.Shippers.Remove(shipper);
                db.SaveChanges();
                return(Json("ok", JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json("error", JsonRequestBehavior.AllowGet));
            }
        }
Ejemplo n.º 30
0
        public ActionResult DeleteProducts(string productID)
        {
            int     prodID  = int.Parse(productID);
            Product product = db.Products.Find(prodID);

            db.Products.Remove(product);
            if (db.SaveChanges() > 0)
            {
                return(Content("Success"));
            }
            else
            {
                return(Content("Failed"));
            }
        }
Ejemplo n.º 31
0
        public ActionResult Edit(Employee e, string btnSave)
        {
            if (btnSave == "Save")
            {
                if (!ModelState.IsValid)
                {
                    return(RedirectToAction("Index"));
                }

                NORTHWNDEntities nwe = new NORTHWNDEntities();
                nwe.Configuration.ProxyCreationEnabled = false;
                Employee original = nwe.Employees.Where(emp => emp.EmployeeID == employeeId).SingleOrDefault();

                foreach (PropertyInfo propertyInfo in ((Employee)original).GetType().GetProperties())
                {
                    if (propertyInfo.GetValue(e) == null)
                    {
                        propertyInfo.SetValue(e, propertyInfo.GetValue(original));
                    }
                }
                nwe.Entry(original).CurrentValues.SetValues(e);
                nwe.SaveChanges();

                return(View("ProfileView", e));
            }
            else
            {
                return(View("Index"));
            }
        }
Ejemplo n.º 32
0
        public static void AddCustomer(Customer customer)
        {
            using (var db = new NORTHWNDEntities())
            {
                db.Customers.Add(customer);
                db.SaveChanges();
            }

            Console.WriteLine("Customer added.");
        }
Ejemplo n.º 33
0
        public static void Main()
        {
            using (var db = new NORTHWNDEntities())
            {
                Order firstOrder = new Order() { CustomerID = "VINET", EmployeeID = 5, ShipVia = 3 };
                Order secondOrder = new Order() { CustomerID = "VINET", EmployeeID = 5, ShipVia = 3 };
                Order thirdOrder = new Order() { CustomerID = "VINET", EmployeeID = 5, ShipVia = 3 };
                //Order orderWithDuplicatedPrimaryKey = new Order() { CustomerID = "INVAL", EmployeeID = 5, ShipVia = 3 };

                db.Orders.Add(firstOrder);
                db.SaveChanges();
                db.Orders.Add(secondOrder);
                db.SaveChanges();
                db.Orders.Add(thirdOrder);
                db.SaveChanges();
                //db.Orders.Add(orderWithDuplicatedPrimaryKey);
                //db.SaveChanges();
            }
        }
Ejemplo n.º 34
0
        public static void DeleteCustomer(Customer customer)
        {
            using (var db = new NORTHWNDEntities())
            {
                var customerFromDB = db.Customers.FirstOrDefault(x => x.CustomerID == customer.CustomerID);
                db.Customers.Remove(customerFromDB);
                db.SaveChanges();
            }

            Console.WriteLine("Customer deleted.");
        }
Ejemplo n.º 35
0
        public static void ModifyCustomerCompanyName(Customer customer, string newName)
        {
            using (var db = new NORTHWNDEntities())
            {
                var customerFromDB = db.Customers.FirstOrDefault(x => x.CustomerID == customer.CustomerID);
                customerFromDB.CompanyName = newName;
                db.SaveChanges();
            }

            Console.WriteLine("Customer updated.");
        }
        public ActionResult Save(Employee e, string btnSave)
        {
            if (btnSave == "Save")
            {
                if (!ModelState.IsValid)
                    return RedirectToAction("Index");

                NORTHWNDEntities nwe = new NORTHWNDEntities();
                nwe.Employees.Add(e);
                nwe.SaveChanges();
                return View("ProfileView", e);
            }
            else
                return View("Index");
        }
        public ActionResult Edit(EmployeeEditViewModel e, string btnSave)
        {
            if (btnSave == "Save")
            {
                if (!ModelState.IsValid)
                    return RedirectToAction("Index");

                NORTHWNDEntities nwe = new NORTHWNDEntities();
                nwe.Configuration.ProxyCreationEnabled = false;
                Employee original = nwe.Employees.Where(emp => emp.EmployeeID == employeeId).SingleOrDefault();
                CopyPropertyValues(e, original);
                nwe.SaveChanges();

                return View("ProfileView", original);
            }
            else
                return View("Index");
        }
        public ActionResult Edit(Employee e, string btnSave)
        {
            if (btnSave == "Save")
            {
                if (!ModelState.IsValid)
                    return RedirectToAction("Index");

                NORTHWNDEntities nwe = new NORTHWNDEntities();
                nwe.Configuration.ProxyCreationEnabled = false;
                Employee original = nwe.Employees.Where(emp => emp.EmployeeID == employeeId).SingleOrDefault();

                foreach (PropertyInfo propertyInfo in ((Employee)original).GetType().GetProperties())
                {
                    if (propertyInfo.GetValue(e) == null)
                        propertyInfo.SetValue(e, propertyInfo.GetValue(original));
                }
                nwe.Entry(original).CurrentValues.SetValues(e);
                nwe.SaveChanges();

                return View("ProfileView", e);
            }
            else
                return View("Index");
        }