public Product Save(Product product) { if (product.ProductId == 0) { db.Products.Add(product); } else { db.Entry(product).State = System.Data.Entity.EntityState.Modified; } db.SaveChanges(); return(product); }
public ActionResult Delete(int id) { using (StoreContext db = new StoreContext()) { T_Persona persona = db.T_Personas.Find(id); if (persona == null) { return(HttpNotFound()); } persona.Estado = false; db.Entry(persona).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } }
public ActionResult Create(Product product) { if (product.Id > 0) { _context.Entry(product).State = EntityState.Modified; } else { _context.Products.Add(product); } _context.SaveChanges(); return(RedirectToAction("Index")); }
public void ShouldAddACategoryWithDbSet() { var category = new Category { CategoryName = "Foo" }; _storeContext.Categories.Add(category); Assert.Equal(EntityState.Added, _storeContext.Entry(category).State); Assert.True(category.Id < 0); Assert.Null(category.TimeStamp); _storeContext.SaveChanges(); Assert.Equal(EntityState.Unchanged, _storeContext.Entry(category).State); Assert.True(category.Id == 0); Assert.NotNull(category.TimeStamp); Assert.Equal(1, _storeContext.Categories.Count()); }
public async Task <int> UpdateOrder(OrderItem order) { var orderToUpdate = await _context.Orders.SingleOrDefaultAsync(s => s.ID == order.ID); _context.Entry(orderToUpdate).State = EntityState.Modified; orderToUpdate.Name = order.Name; orderToUpdate.StreetAddress = order.StreetAddress; orderToUpdate.City = order.City; orderToUpdate.State = order.State; orderToUpdate.ZipCode = order.ZipCode; orderToUpdate.TrackingID = order.TrackingID; orderToUpdate.UserID = order.UserID; return(await _context.SaveChangesAsync()); }
public bool Delete(int id) { try { _storeContext.Entry(new Customer { CustomerId = id }).State = EntityState.Deleted; _storeContext.SaveChanges(); } catch (System.Exception) { return(false); } return(true); }
public async Task <Genre> Update(Genre entity) { if (entity == null) { throw new Exception("Entity cannot be null"); } _dbContext.Genres.Attach(entity); var entry = _dbContext.Entry(entity); entry.State = EntityState.Modified; await _dbContext.SaveChangesAsync(); return(entry.Entity); }
public bool Edit(SubCategory entity) { SubCategory selected = GetById(entity.Id); if (selected != null) { selected.Name = entity.Name; selected.Products = entity.Products; selected.Category = entity.Category; db.Entry(selected).State = EntityState.Modified; db.SaveChanges(); return(true); } return(false); }
public bool Edit(UserComment entity) { UserComment selected = GetById(entity.Id); if (selected != null) { selected.Text = entity.Text; selected.Product = entity.Product; selected.User = entity.User; db.Entry(selected).State = EntityState.Modified; db.SaveChanges(); return(true); } return(false); }
public bool Edit(Manufacturer entity) { Manufacturer selected = GetById(entity.Id); if (selected != null) { selected.CompanyName = entity.CompanyName; selected.ManufacturerContacts = entity.ManufacturerContacts; selected.Products = entity.Products; db.Entry(selected).State = EntityState.Modified; db.SaveChanges(); return(true); } return(false); }
public ActionResult GenderUpdate([DataSourceRequest] DataSourceRequest request, Gender gender) { if (gender != null && ModelState.IsValid) { using (var db = new StoreContext()) { var entity = new Gender(); entity.GenderID = gender.GenderID; entity.GenderName = gender.GenderName; db.Genders.Attach(entity); db.Entry(entity).State = EntityState.Modified; db.SaveChanges(); } } return(Json(new[] { gender }.ToDataSourceResult(request, ModelState))); }
public IActionResult OrderComplete(int orderId, string caller) { Orders order = _context.Orders.Find(orderId); order.Completed = DateTime.Now; _context.Entry(order).State = EntityState.Modified; _context.SaveChanges(); if (caller == "orders") { return(RedirectToAction("Index")); } return(RedirectToAction("Index", "Admin")); }
public ActionResult UpdateBudgetOrganization([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")] IEnumerable <BudgetOrganization> item) { var entities = new List <BudgetOrganization>(); if (ModelState.IsValid) { using (var db = new StoreContext()) { foreach (var itm in item) { Organization organization = db.Organizations.Find(itm.OrganizationId); OrganizationType organizationType = db.OrganizationTypes.Find(organization.OrganizationTypeId); var entity = new BudgetOrganization { BudgetOrganizationId = itm.BudgetOrganizationId, BudgetId = itm.BudgetId, OrganizationId = itm.OrganizationId, BudgetCost = itm.BudgetCost, Budget1 = itm.BudgetCost * organizationType.Budget1, Budget2 = itm.BudgetCost * organizationType.Budget2, Budget3 = itm.BudgetCost * organizationType.Budget3, Id = userId, BudgetLineId = itm.BudgetLineId, ChangeDate = itm.ChangeDate, ChangeBaseId = itm.ChangeBaseId }; entities.Add(entity); db.BudgetOrganizations.Attach(entity); db.Entry(entity).State = EntityState.Modified; } db.SaveChanges(); } } return(Json(entities.ToDataSourceResult(request, ModelState, itm => new BudgetOrganization { BudgetOrganizationId = itm.BudgetOrganizationId, BudgetId = itm.BudgetId, OrganizationId = itm.OrganizationId, BudgetCost = itm.BudgetCost, Budget1 = itm.Budget1, Budget2 = itm.Budget2, Budget3 = itm.Budget3, BudgetLineId = itm.BudgetLineId, ChangeDate = itm.ChangeDate, ChangeBaseId = itm.ChangeBaseId }))); }
public ActionResult UpdateReferralDistributionOrganization([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")] IEnumerable <ReferralDistributionOrganization> item) { var entities = new List <ReferralDistributionOrganization>(); if (ModelState.IsValid) { using (var db = new StoreContext()) { foreach (var itm in item) { Organization organization = db.Organizations.Find(itm.OrganizationId); OrganizationType organizationType = db.OrganizationTypes.Find(organization.OrganizationTypeId); var entity = new ReferralDistributionOrganization { ReferralDistributionOrganizationId = itm.ReferralDistributionOrganizationId, ReferralDistributionId = itm.ReferralDistributionId, OrganizationId = itm.OrganizationId, ReferralDistributionCount = itm.ReferralDistributionCount, Id = userId, BudgetLineId = itm.BudgetLineId, ChangeDate = itm.ChangeDate, ChangeBaseId = itm.ChangeBaseId }; entities.Add(entity); db.ReferralDistributionOrganizations.Attach(entity); db.Entry(entity).State = EntityState.Modified; } db.SaveChanges(); } } else { List <ModelErrorCollection> errors = ModelState.Select(x => x.Value.Errors) .Where(y => y.Count > 0) .ToList(); } return(Json(entities.ToDataSourceResult(request, ModelState, itm => new ReferralDistributionOrganization { ReferralDistributionOrganizationId = itm.ReferralDistributionOrganizationId, ReferralDistributionId = itm.ReferralDistributionId, OrganizationId = itm.OrganizationId, ReferralDistributionCount = itm.ReferralDistributionCount, BudgetLineId = itm.BudgetLineId, ChangeDate = itm.ChangeDate, ChangeBaseId = itm.ChangeBaseId }))); }
public void ShouldDeleteACategoryWithTimestampData() { var category = new Category { CategoryName = "foo" }; _db.Categories.Add(category); _db.SaveChanges(); var context = new StoreContext(); var catToDelete = new { Id = category.Id, TimeStamp = category.TimeStamp }; context.Entry(category).State = EntityState.Deleted; var affected = context.SaveChanges(); Assert.Equal(1, affected); }
public ActionResult Edit([Bind(Include = "ID,Title,Content,UserID,BookID")] Comment comment) { if (base.isPermitted()) { if (ModelState.IsValid) { db.Entry(comment).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.BookID = new SelectList(db.Books, "ID", "Name", comment.BookID); ViewBag.UserID = new SelectList(db.Users, "ID", "FirstName", comment.UserID); return(View(comment)); } return(HttpNotFound("Or no access")); }
public async Task UpdateLeagueLadder(string league, List <LadderPlayerModel> ladder) { try { var leagueLadder = await _store.Ladders.FirstOrDefaultAsync(t => t.Name == league); leagueLadder.Finished = DateTime.Now; leagueLadder.Running = false; leagueLadder.Ladder = ladder; _store.Entry(leagueLadder).State = EntityState.Modified; await _store.SaveChangesAsync(); } catch (Exception e) { throw e; } }
public ActionResult Edit(Product product) { try { if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); } catch (Exception) { return(View(product)); } }
public IActionResult PutItem(int id, [FromBody] Item item) { if (id != item.Id) { return(BadRequest()); } if (_db.Items.Find(id) == null) { return(NotFound()); } _db.Entry(item).State = EntityState.Modified; _db.SaveChanges(); return(NoContent()); }
//TODO: Check For Cascade Delete public int Delete(int id, byte[] timeStamp, bool persist = true) { var entry = GetEntryFromChangeTracker(id); if (entry != null) { if (timeStamp != null && entry.TimeStamp.SequenceEqual(timeStamp)) { return(Delete(entry, persist)); } throw new Exception("Unable to delete due to concurrency violation."); } Db.Entry(new T { Id = id, TimeStamp = timeStamp }).State = EntityState.Deleted; return(persist ? SaveChanges() : 0); }
public virtual async Task <T> Update(T obj) { try { obj.UpdateRecorde(); _storeContext.Entry(obj).State = EntityState.Modified; await _storeContext.SaveChangesAsync(); return(obj); } catch (Exception e) { _logger.LogError(e.Message); _logger.LogError(e.StackTrace); return(null); } }
// Renders Order details as soon as an order has been submitted public ActionResult ViewOrder(int id) { using (var context = new StoreContext()) { var order = context.Orders.Find(id); var gadgetOrders = context.GadgetOrders.Where(go => go.OrderID == id); foreach (GadgetOrder gadgetOrder in gadgetOrders) { context.Entry(gadgetOrder).Reference(go => go.Gadget).Load(); order.Gadgets.Add(gadgetOrder.Gadget); } return(View(order)); } }
public void ShouldDeleteCategoryWithStampData() { var cat = new Category { CategoryName = "Foo" }; _db.Categories.Add(cat); _db.SaveChanges(); using (var context = new StoreContext()) { Category catToDelete = new Category { Id = cat.Id, TimeStamp = cat.TimeStamp }; context.Entry(catToDelete).State = EntityState.Deleted; var affected = context.SaveChanges(); Assert.Equal(1, affected); } }
public ActionResult Edit(Product product) { try { // TODO: Add update logic here if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); } catch { return(View(product)); } }
public ActionResult Edit([Bind(Include = "Id,Name,Password,PasswordConfirmation,Email,RegisterOn,EditOn")] Customer customer) { try { if (ModelState.IsValid) { customer.EditOn = DateTime.Now; //Set edit time db.Entry(customer).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DbEntityValidationException exception) { Console.WriteLine(exception.Message); } return(View(customer)); }
public void ShouldNotDeleteACategoryWithoutTimestampData() { var category = new Category { CategoryName = "Foo" }; _db.Categories.Add(category); _db.SaveChanges(); var context = new StoreContext(); var catToDelete = new Category { Id = category.Id }; context.Entry(catToDelete).State = EntityState.Deleted; var ex = Assert.Throws <DbUpdateConcurrencyException>(() => context.SaveChanges()); Assert.Equal(1, ex.Entries.Count); Assert.Equal(category.Id, ((Category)ex.Entries[0].Entity).Id); }
public bool validation(string email, string password) { UserInfo userObj = db.UserInfos .Where(p => p.Email == email && p.Password == password) .FirstOrDefault(); if (userObj != null) { userObj.IsLoggedIn = true; db.Entry(userObj).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return(true); } else { return(false); } }
public ActionResult Edit([Bind(Include = "ID,OrderID,StoreID,InstrumentID")] OrderDetail orderDetail) { try { if (ModelState.IsValid) { db.Entry(orderDetail).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists please see your system administrator"); throw; } return(View(orderDetail)); }
public async Task PutCoupon(int id, Coupon coupon) { _context.Entry(coupon).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CouponExists(id)) { throw new BadRequestException(Enum.InternalCode.NotFound, "Coupon not found"); } else { throw; } } }
public ActionResult EditProduct(Product product) { try { // TODO: Add update logic here // siempre hay que hacer esta validaciòn para verificar que sea valido el modelo. if (ModelState.IsValid) { // Se va a modificar este objeto db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(product)); } catch { return(View()); } }