public async Task <bool> Remove(Class model) { _db.Entry(model).State = EntityState.Deleted; await _db.SaveChangesAsync(); return(true); }
public async Task Update(FoodEditInfo foodEditInfo) { var food = new Food(foodEditInfo.Name, new Money(foodEditInfo.Price.Value), foodEditInfo.FoodType); //{ // Id=foodEditInfo.Id, // Name = foodEditInfo.Name, // Price = new Money(foodEditInfo.Price.Value), // FoodType = foodEditInfo.FoodType, // Description = foodEditInfo.Description //}; food.Id = foodEditInfo.Id; food.Description = foodEditInfo.Description; //food.UpdatePrice(foodEditInfo.Price); _db.Entry(food).State = EntityState.Modified; _db.Entry(food.Price).State = EntityState.Modified; await _db.SaveChangesAsync(); _logger.LogInformation($"Food by Id [{food.Id}] Updated.", food); }
public async Task Update(FoodEditInfo foodEditInfo) { var food = new Food(foodEditInfo.Id, foodEditInfo.Name, foodEditInfo.Description, foodEditInfo.Price, foodEditInfo.FoodType); _db.Entry(food.Price).State = EntityState.Modified; _db.Entry(food).State = EntityState.Modified; await _db.SaveChangesAsync(); _logger.LogInformation($"Food by Id [{food.Id}] Updated.", food); }
public TObject Update(TObject updated, object key) { if (updated == null) { return(null); } TObject existing = DbContext.Set <TObject>().Find(key); if (existing != null) { DbContext.Entry(existing).CurrentValues.SetValues(updated); DbContext.SaveChanges(); } return(existing); }
public async Task Update(FoodEditInfo foodEditInfo) { var food = new Domain.Food { ID = foodEditInfo.Id, Name = foodEditInfo.Name, Price = foodEditInfo.Price, FoodType = foodEditInfo.FoodType, Description = foodEditInfo.Description }; _db.Entry(food).State = EntityState.Modified; await _db.SaveChangesAsync(); _logger.LogInformation($"Food by Id [{food.ID}] Updated.", food); }
/// <summary> /// Commits all changes added on database /// </summary> /// <param name="CommitAllChangesOnSuccess"></param> /// <returns></returns> public bool Update <TEntity>(TEntity entity, bool CommitAllChangesOnSuccess = true) where TEntity : class { DbSet <TEntity> dbSet = _context.Set <TEntity>(); if (_context.Entry <TEntity>(entity).State == EntityState.Modified) { try { dbSet.Attach(entity); _context.SaveChanges(CommitAllChangesOnSuccess); //_context.Entry<TEntity>(entity).State = EntityState.Modified; return(true); } catch (DbUpdateException ex) { return(false); } } return(false); }
public virtual async Task<ActionResult> Edit(EditUserViewModel model) { if (ModelState.IsValid) { string userid = null; string username = model.OldUserName ?? model.UserName; using (var Db = new PortalDbContext()) { var user = Db.Users.First(u => u.UserName == username); // Update the user data: user.UserName = model.UserName; user.FirstName = model.FirstName; user.LastName = model.LastName; user.Email = model.Email; Db.Entry(user).State = System.Data.Entity.EntityState.Modified; userid = user.Id; await Db.SaveChangesAsync(); } // Update Facebook and Google id var usr = await UserManager.FindByIdAsync(userid); if (usr == null) { return View("Error"); } var userLogins = await UserManager.GetLoginsAsync(userid); // Facebook var fbLogin = userLogins.SingleOrDefault(l => l.LoginProvider == "Facebook"); if (fbLogin != null) { if (model.FacebookId.Empty()) { var res = await UserManager.RemoveLoginAsync(userid, fbLogin); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to remove Facebook account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } else { if (fbLogin.ProviderKey != model.FacebookId) { await UserManager.RemoveLoginAsync(userid, fbLogin); await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Facebook", model.FacebookId)); } } } else { if (model.FacebookId.NotEmpty()) { var res = await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Facebook", model.FacebookId)); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to add Facebook account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } } // Google var gLogin = userLogins.SingleOrDefault(l => l.LoginProvider == "Google"); if (gLogin != null) { if (model.GoogleId.Empty()) { var res = await UserManager.RemoveLoginAsync(userid, gLogin); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to remove Google account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } else { if (gLogin.ProviderKey != model.GoogleId) { await UserManager.RemoveLoginAsync(userid, fbLogin); await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Facebook", model.FacebookId)); } } } else { if (model.GoogleId.NotEmpty()) { var res = await UserManager.AddLoginAsync(usr.Id, new UserLoginInfo("Google", model.GoogleId)); if (!res.Succeeded) DependencyResolver.Current.GetService<IAlertsProvider>().Add(string.Format("Failed to add Google account: {0}", string.Concat(res.Errors)), AlertType.Danger, true); } } return RedirectToAction("Index"); } // If we got this far, something failed, redisplay form return View(model); }
public void UpdateUser(ApplicationUser user) { context.Entry(user).State = EntityState.Modified; }