//新增資料方法 public void InsertProducts(Products newData) { //將資料加入資料庫實體 db.Products.Add(newData); //儲存資料庫變更 db.SaveChanges(); }
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)); }
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)); }
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)); }
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); }
//Ekleme public void Add(Category category) { if (category != null) { db.Categories.Add(category); db.SaveChanges(); } }
public void Duzenle(Shipper model) { Shipper orjinalKat = Bul(model.ShipperID); orjinalKat.CompanyName = model.CompanyName; orjinalKat.Phone = model.Phone; db.SaveChanges(); }
public void Duzenle(Category model) { Category orjinalKat = Bul(model.CategoryID); orjinalKat.CategoryName = model.CategoryName; orjinalKat.Description = model.Description; db.SaveChanges(); }
private static void DeleteNewInsertedCustomer() { var customer = context.Customers.First(); context.Customers.Remove(customer); context.SaveChanges(); Console.WriteLine("(Row affected)"); }
public void Delete(TEntity entity) { if (db.Entry(entity).State == EntityState.Detached) { dbSet.Attach(entity); } dbSet.Remove(entity); db.SaveChanges(); }
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(); }
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(); } }
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)); }
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ı")); }
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)); }
public string CreateEmployee(Employee employee) { if (!ModelState.IsValid) { return("Model is invalid"); } _db.Employees.Add(employee); var result = _db.SaveChanges(); return("Cource is created"); }
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)); }
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)); }
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(); }
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)); }
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)); }
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)); }
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)); }
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)); }
public string AddCustomer(Customer customer) { var newcustomer = _dbContext.Customers.Add(customer); _dbContext.SaveChanges(); if (newcustomer != null) { return(customer.CustomerID); } else { return(null); } }
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)); } }
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")); } }
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")); } }
public static void AddCustomer(Customer customer) { using (var db = new NORTHWNDEntities()) { db.Customers.Add(customer); db.SaveChanges(); } Console.WriteLine("Customer added."); }
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(); } }
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."); }
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"); }