public void Edit(TEntity Entity) { if (Entity != null) { _context.Entry(Entity).State = EntityState.Modified; } }
public ActionResult <Product> Put(int id, [FromForm] Product product) { if (!ModelState.IsValid) { return(BadRequest("Not a valid model")); } var existingProduct = _context.Products.Find(id); if (existingProduct == null) { return(NotFound()); } if (product.Name != null) { existingProduct.Name = product.Name; } if (product.Price != null) { existingProduct.Price = product.Price; } _context.Entry(existingProduct).State = EntityState.Modified; _context.SaveChanges(); return(existingProduct); }
public async Task <T> Update(T entidade) { _context.Entry(entidade).State = EntityState.Modified; await _context.SaveChangesAsync(); return(entidade); }
public ActionResult Edit([Bind(Include = "Id,LastName,FirstName,Phone,Email,SellerAccount,Login,Password")] Seller seller) { if (ModelState.IsValid) { // Modification du vendeur dans le SecurityContext du projet ASP.NET using (var secudb = new SecurityDbContext()) { UserManager <MyIdentityUser> userManager = new MyIdentityUserManager(new UserStore <MyIdentityUser>(secudb)); var sellerToRemove = userManager.FindByEmail(seller.Email); var res = userManager.Delete(sellerToRemove); if (!res.Succeeded) { throw new System.Exception("database remove fail"); } IdentityRole userRole = RoleUtils.CreateOrGetRole("User"); MyIdentityUser sellerUser = new MyIdentityUser() { UserName = seller.Login, Email = seller.Email, Login = seller.Login };; var result = userManager.Create(sellerUser, seller.Password); if (!result.Succeeded) { throw new System.Exception("database insert fail"); } RoleUtils.AssignRoleToUser(userRole, sellerUser); } // Modification du vendeur dans le ProductContext du projet librairie db.Entry(seller).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(seller)); }
public void Update(UserViewModel user) { var model = _mapper.Map <UserViewModel, User>(user); _context.Entry(model).State = EntityState.Modified; _context.SaveChanges(); }
public override async Task <ProductModel> UpdateProduct(UpdateProductRequest request, ServerCallContext context) { var product = _mapper.Map <Product>(request.Product); bool isExist = await _productContext.Products.AnyAsync(x => x.ProductId == product.ProductId); if (!isExist) { throw new RpcException(new Status(StatusCode.NotFound, "Requested product doesn't exists!!!")); } _productContext.Entry(product).State = EntityState.Modified; try { await _productContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { Console.WriteLine(ex.Message); throw; } var productModel = _mapper.Map <ProductModel>(product); return(productModel); }
public async Task <IActionResult> PutShop([FromRoute] int id, [FromBody] Shop shop) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != shop.Id) { return(BadRequest()); } _context.Entry(shop).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShopExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task ParseAndPush() { WebClient client = new WebClient(); client.Encoding = Encoding.UTF8; string result = client.DownloadString("https://apidata.mos.ru/v1/datasets/593/rows?$top=3&api_key=fee68e1ff9da6aa97c7deb04d48c82d1"); List <ResultFromServer> resultServer = JsonConvert.DeserializeObject <List <ResultFromServer> >(result); var optionsBuilder = new DbContextOptionsBuilder <ProductContext>(); string newPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\")); string newnewpath = Path.Combine(newPath, "InnovativeProduct.WebService", "InnovativeProduct.db"); optionsBuilder.UseSqlite($"Data Source={newnewpath}"); var context = new ProductContext(options: optionsBuilder.Options); context.Database.ExecuteSqlRaw("DELETE FROM TransferNodes"); using (context) { foreach (var item in resultServer) { DomainObjects.Product product = new DomainObjects.Product(); product.Name = item.Cells.Name; product.ExpectedEffects = item.Cells.Effects; product.Indicators = item.Cells.Indicators; product.Tasks = item.Cells.Tasks; product.Specifications = item.Cells.TechnicalCharacteristics; context.Entry(product).State = EntityState.Added; context.SaveChanges(); } } await Task.CompletedTask; }
public async Task<IActionResult> PutCategory([FromRoute] long id, [FromBody] Category category) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != category.Id) { return BadRequest(); } _context.Entry(category).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(id)) { return NotFound(new { Message="分类不存在,无法修改"}); } else { throw; } } return NoContent(); }
public async Task <Product> UpdateProduct(int id, Product product) { _context.Entry(product).State = EntityState.Modified; await _context.SaveChangesAsync(); return(product); }
public async Task <ActionResult <Product> > PutProduct(Guid?id, Product product) { if (id == null || product == null) { return(BadRequest()); } var existingproduct = await _context.Products.FindAsync(id); if (existingproduct == null) { return(BadRequest(new { Message = $"Product with id:{id} does not exist" })); } product.Id = existingproduct.Id; _context.Entry(existingproduct).CurrentValues.SetValues(product); try { await _context.SaveChangesAsync(); return(Ok(new { Message = $"Product id:{existingproduct.Id} updated" })); } catch (Exception) { return(BadRequest()); } }
public void UpdateProduct(Product product) { //There are 3 ways of updating, // Entry() method in case you want to update all the fields in your object. //1. // _context.Entry(product).State = Microsoft.EntityFrameworkCore.EntityState.Modified; // Save(); //2. // var oldProd = _context.Products.Find(product.Id); // _context.Entry(oldProd).CurrentValues.SetValues(product); // Save(); //3. var dbProdEntry = _context.Entry(product); foreach (var property in dbProdEntry.OriginalValues.Properties) { var original = dbProdEntry.OriginalValues.GetValue <object>(property); var current = dbProdEntry.CurrentValues.GetValue <object>(property); if (original != null && !original.Equals(current)) { dbProdEntry.Property(property.Name).IsModified = true; } } Save(); }
public async Task <Product> UpdateAsync(Product entity) { Product local = _context.Set <Product>() .Local? .FirstOrDefault(entry => entry.Id.Equals(entity.Id)); if (local != null) { _context.Entry(local).State = EntityState.Detached; } _context.Entry(entity).State = EntityState.Modified; await _context.SaveChangesAsync(); return(entity); }
public async Task <IActionResult> PutUser(int id, [FromBody] User user) { if (id != user.Id) { return(BadRequest()); } _context.Entry(user).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public IHttpActionResult PutCategory(int id, Category category) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != category.Id) { 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 async Task <IActionResult> PutFeedback(string id, Feedback feedback) { if (id != feedback.FId) { return(BadRequest()); } _context.Entry(feedback).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FeedbackExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public IHttpActionResult PutProduct(int id, Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.Id) { return(BadRequest()); } db.Entry(product).State = EntityState.Modified; try { db.SaveChanges(); MyLogger.GetInstance().Info("Product Id : " + id + " updated."); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { MyLogger.GetInstance().Error("Product does not exists with ID : " + id); return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public override void Update(Rating entity) { Rating target = _Context.Ratings.Where(c => c.Id == entity.Id).FirstOrDefault(); _Context.Entry(target).CurrentValues.SetValues(entity); _Context.SaveChanges(); }
public ActionResult Edit(Product product, HttpPostedFileBase file) { var oldPhoto = product.Photo; try { if (ModelState.IsValid) { if (file != null) { product.Photo = "/Images/" + System.IO.Path.GetFileName(file.FileName); } else { product.Photo = oldPhoto; } db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index", "Products")); } } catch (Exception) { ModelState.AddModelError("", "Error"); } return(View()); }
public async Task <IActionResult> PutCommand(int id, Command command) { if (id != command.Id_command) { return(BadRequest()); } _context.Entry(command).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CommandExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutProduct([FromRoute] long id, [FromBody] Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.Id) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IHttpActionResult> PutProduct(int id, Product product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != product.Id) { 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> PutProduct(Guid id, Product product) { if (id != product.ID) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <T> Update(T data) { context.Entry(data).State = Microsoft.EntityFrameworkCore.EntityState.Modified; await context.SaveChangesAsync(); return(data); }
/// <summary> /// Deletes a product from the DB /// </summary> /// <param name="p"></param> /// <param name="context"></param> /// <returns></returns> public static async Task Delete(Product p, ProductContext context) { await context.AddAsync(p); context.Entry(p).State = EntityState.Deleted; await context.SaveChangesAsync(); }
public override void Update(Category entity) { Category target = _Context.Categories.Where(c => c.Id == entity.Id).FirstOrDefault(); _Context.Entry(target).CurrentValues.SetValues(entity); _Context.SaveChanges(); }
public bool Update(int id, Product product) { if (id != product.Id_product) { return(false); //Bad Request } _context.Entry(product).State = EntityState.Modified; try { _context.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(false); } else { throw; } } return(true); }
public async Task <IActionResult> PatchProductAsync(int id, ProductDescription description) { Product product = await _context.Products.FindAsync(id); if (product == null) { return(NotFound()); } product.Description = description.Description; _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public override void Update(Product entity) { Product target = _Context.Products.Where(c => c.Id == entity.Id).FirstOrDefault(); _Context.Entry(target).CurrentValues.SetValues(entity); _Context.SaveChanges(); }
public async Task <IActionResult> PutRegister(string id, Register register) { if (id != register.RId) { return(BadRequest()); } _context.Entry(register).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RegisterExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }