/// <summary> /// Creates a new cart for a user, called if a cart doesn't already exist for the user in question /// </summary> /// <param name="cartItem">CartItem information for creation</param> /// <returns>Successful result of CartItem creation</returns> public async Task <Cart> Create(Cart cart) { _context.Entry(cart).State = EntityState.Added; await _context.SaveChangesAsync(); return(cart); }
/// <summary> /// Creates a new Flummery object in the database. /// </summary> /// <param name="flummeryVM"> /// Flummery: the object to be created in the database /// </param> /// <returns> /// Task<Flummery>: the Flummery object reflecting what was saved to the database, wrapped in a Task /// </returns> public async Task <Flummery> CreateFlummery(Flummery flummery) { _context.Entry(flummery).State = EntityState.Added; await _context.SaveChangesAsync(); return(flummery); }
/// <summary> /// Creates a new order for a user, called if a order doesn't already exist for the user in question /// </summary> /// <param name="orderItem">OrderItem information for creation</param> /// <returns>Successful result of OrderItem creation</returns> public async Task <OrderCart> Create(OrderCart order) { _context.Entry(order).State = EntityState.Added; await _context.SaveChangesAsync(); return(order); }
/// <summary> /// Create - Method that allow us to create a new product. /// </summary> /// <param name="product">The product we want to create.</param> /// <returns>Information about the newly created product.</returns> public async Task <Product> Create(Product product) { _context.Entry(product).State = EntityState.Added; await _context.SaveChangesAsync(); return(product); }
public virtual void Delete(TEntity entityToDelete) { if (ctx.Entry(entityToDelete).State == EntityState.Detached) { dbSet.Attach(entityToDelete); } dbSet.Remove(entityToDelete); }
/// <summary> /// Create the product /// </summary> /// <param name="product"></param> /// <returns></returns> public async Task <Product> CreateProduct(Product product) { product.Image = await _image.GetBlob(product.Name, _config["ImageContainer"]); _context.Entry(product).State = EntityState.Added; await _context.SaveChangesAsync(); return(product); }
/// <summary> /// Delete a product /// </summary> /// <param name="id">Id of Product to be deleted</param> /// <returns>Task of completion for product deletion</returns> public async Task Delete(int id) { Products product = await _context.Products.FindAsync(id); if (product != null) { _context.Entry(product).State = EntityState.Deleted; await _context.SaveChangesAsync(); } }
public ActionResult Edit([Bind(Include = "Id,Name,Price,UrlImage")] Product product) { if (ModelState.IsValid) { _db.Entry(product).State = EntityState.Modified; _db.SaveChanges(); return(RedirectToAction("Product")); } return(View(product)); }
/// <summary> /// Adds a new Cart to the database /// </summary> /// <param name="email">The email to attach the cart to</param> /// <returns>Newly created cart</returns> public async Task <Cart> Create(string email) { Cart cart = new Cart(); cart.UserEmail = email; cart.IsActive = true; _context.Entry(cart).State = EntityState.Added; await _context.SaveChangesAsync(); return(cart); }
public ActionResult Edit([Bind(Include = "UserId,FirstName,LastName,Email,IsEmployee,Password")] User user) { if (ModelState.IsValid) { user.Password = Encrypt(user.Password); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(user)); }
/// <summary> /// Update Order in database. /// </summary> /// <param name="item">Should be an Order type.</param> public void Modify(object item) { if (item as Order != null) { dbContext.Entry(item).State = EntityState.Modified; } else { throw new Exception("Wrong type."); } }
public ActionResult Edit([Bind(Include = "OrderId,UserId,BranchId,Date,TotalCost,Status")] Order order) { if (ModelState.IsValid) { db.Entry(order).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.BranchId = new SelectList(db.Branches, "BranchId", "Name", order.BranchId); ViewBag.UserId = new SelectList(db.Users, "UserId", "FirstName", order.UserId); return(View(order)); }
public async Task <IActionResult> PutCustomer(int id, Customer customer) { if (id != customer.id) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutUsers(int id, Users users) { if (id != users.userId) { return(StatusCode(400, "User is not matching which you are updating.")); } _context.Entry(users).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UsersExists(id)) { return(StatusCode(404, "This user does not exists.")); } else { throw; } } return(StatusCode(200, "User updated successfully.")); }
public async Task <IActionResult> PutProduct(int id, Product product) { if (id != product.ProductId) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Product product; SelectList categories; using (StoreDbContext db = new StoreDbContext()) { product = db.Products.Find(id); db.Entry(product).Reference(p => p.Category).Load(); categories = new SelectList(db.ProductCategories.ToList(), "Id", "Name"); } ViewBag.Categories = categories; if (product == null) { return(HttpNotFound()); } else { return(View(new ProductViewModel(product))); } }
public async Task <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 { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutStore(int id, Store store) { if (id != store.StoreId) { return(BadRequest()); } _context.Entry(store).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StoreExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutUser([FromRoute] int id, [FromBody] User user) { if (!ModelState.IsValid) { return(BadRequest(ErrorMessages.Invalid)); } if (id != user.ID) { return(BadRequest(ErrorMessages.Invalid)); } _context.Entry(user).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound(ErrorMessages.EntryDeleted)); } else { return(BadRequest(ErrorMessages.EntryChanged)); } } return(NoContent()); }
public async Task <IActionResult> PutTestAPI(int id, TestAPI testAPI) { if (id != testAPI.Id) { return(BadRequest()); } _context.Entry(testAPI).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TestAPIExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutCreditCard([FromRoute] int id, [FromBody] CreditCard creditCard) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != creditCard.id) { return(BadRequest()); } _context.Entry(creditCard).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CreditCardExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutProducts(int id, Products products) { if (id != products.productId) { return(StatusCode(400, "Product is not matching which you are updating")); } _context.Entry(products).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductsExists(id)) { return(StatusCode(404, "This product does not exists.")); } else { throw; } } return(StatusCode(200, "Product updated successfully.")); }
public async Task <Product> UpdateAsync(Product product) { _context.Entry(product).State = EntityState.Modified; await _context.SaveChangesAsync(); return(product); }
public PartialViewResult Edit(Meat meat_, HttpPostedFileBase file) { if (ModelState.IsValid) { if (file != null && Path.GetExtension(file.FileName) == ".jpg") { var fileName = Path.GetFileName(file.FileName); var deletefile = Path.Combine(Server.MapPath("~/Images/Meat/" + meat_.type + "/"), meat_.image); if (System.IO.File.Exists(deletefile)) { System.IO.File.Delete(deletefile); } var path = Path.Combine(Server.MapPath("~/Images/Meat/" + meat_.type + "/"), fileName); meat_.image = fileName; file.SaveAs(path); } db.Entry(meat_).State = EntityState.Modified; db.SaveChanges(); return(PartialView("_Ok")); } return(PartialView("_Invalid")); }
/// <summary> /// 入库,如果有增加数量,如果没有新增 /// </summary> /// <param name="st"></param> public void InsertStoreItem(StoreTable st) { using (var dbContext = new StoreDbContext()) { var item = dbContext.StoreTable.Where(u => u.store_item_id == st.store_item_id).FirstOrDefault(); if (item == null) { st.kcid = Guid.NewGuid(); dbContext.Insert <StoreTable>(st); } else { decimal totalnumber = item.number.Value; string guid = item.kcid.ToString(); Guid tempguid = new Guid(guid); st.number += totalnumber; st.kcid = tempguid; ///问题的原因在于,我们之前已经附加过当前实体,如果再进行Attach的时候,就会报这样的错。 // 解决办法:1.销毁之前的上下文,重新开启上下文。(等于白说) //2.更改当前上下文的实体的状态。(这个是问题关键) var entry = dbContext.Set <StoreTable>().Find(tempguid); if (entry != null) { dbContext.Entry <StoreTable>(entry).State = System.Data.EntityState.Detached; //这个是在同一个上下文能修改的关键 } dbContext.Update <StoreTable>(st); } } }
public ActionResult AddCourse(EditCourseViewModel model, HttpPostedFileBase file) { if (model.Course.CourseId > 0) { // modyfikacja kursu db.Entry(model.Course).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("AddCourse", new { potwierdzenie = true })); } else { // Sprawdzenie, czy użytkownik wybrał plik if (file != null && file.ContentLength > 0) { if (ModelState.IsValid) { // Generowanie pliku var fileExt = Path.GetExtension(file.FileName); var filename = Guid.NewGuid() + fileExt; var path = Path.Combine(Server.MapPath(AppConfig.CourseImagesRelativeFolder), filename); file.SaveAs(path); model.Course.ImgFileName = filename; model.Course.DateAdded = DateTime.Now; db.Entry(model.Course).State = EntityState.Added; db.SaveChanges(); return(RedirectToAction("AddCourse", new { confirmation = true })); } else { var category = db.Categories.ToList(); model.Category = category; return(View(model)); } } else { ModelState.AddModelError("", "No image has been added."); var category = db.Categories.ToList(); model.Category = category; return(View(model)); } } }
/// <summary> /// Adds a specified item to a specified cart /// </summary> /// <param name="cartId">Id of cart</param> /// <param name="productId">Id of product</param> /// <returns>Successful completion of task</returns> public async Task AddToCart(int cartId, int productId) { Products product = await _context.Products.FirstOrDefaultAsync(x => x.Id == productId); CartItem cartItem = new CartItem() { CartId = cartId, ProductId = productId, DateAdded = DateTime.Now, Quantity = 1 }; cartItem.Product = product; _context.Entry(cartItem).State = EntityState.Added; await _context.SaveChangesAsync(); }
/// <summary> /// Adds a new order to the database /// </summary> /// <param name="input">The information for the new order</param> /// <returns>The new order</returns> public async Task <Order> Create(Order order) { order.OrderDate = DateTime.Now; _context.Entry(order).State = EntityState.Added; await _context.SaveChangesAsync(); return(order); }
public async static Task <int> SaveOrder(Order myOrder) { Order order = _dbContext.Orders.FirstOrDefault(a => a.OrderId == myOrder.OrderId); if (order == null) { _dbContext.Orders.Add(myOrder); _dbContext.Entry(myOrder).State = EntityState.Added; } else { order.PaymentTransactionId = myOrder.PaymentTransactionId; _dbContext.Entry(myOrder).State = EntityState.Modified; } return(await _dbContext.SaveChangesAsync()); }
public void SaveOrder(Order order) { db.Entry(order).State = order.ID == 0 ? EntityState.Added : EntityState.Modified; db.SaveChanges(); }