public async Task UpdateUserAsync(User user) { var _user = await _context.Users.SingleOrDefaultAsync(u => u.UserId == user.UserId); var changedUser = new User() { UserId = user.UserId, Name = user.Name, Email = user.Email, Password = user.Password.Length > 1 ? user.Password : _user.Password }; try { var entityEntry = _context.Attach(changedUser); var entity = entityEntry.Entity; //var entityState = entityEntry.State = EntityState.Modified; var entityStateName = entityEntry.Property(x => x.Name); var entityStateEmail = entityEntry.Property(x => x.Email); var entityStatePw = entityEntry.Property(x => x.Password); entityStateName.IsModified = entityStateEmail.IsModified = entityStatePw.IsModified = true; var entitiesUpdates = _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } }
protected override IdentityResult Handle(UpdateAccount request) { var user = new User { FirstName = request.FirstName, LastName = request.LastName, Email = request.Email, PasswordHash = request.PasswordHash, UpdateDate = DateTime.UtcNow }; _dbContext.Attach(user); _dbContext.Update(user); try { _dbContext.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { _logger.LogWarning($"Error with update account. Trace {ex.Message}"); return(IdentityResult.Failed(new IdentityError { Description = $"Error with update account" })); } return(IdentityResult.Success); }
public async Task <ActionResult <Favourite> > PostFavourite(Favourite favourite) { _context.Attach(favourite); User user = _context.Users.Where(u => u.Id == favourite.UserId).Include(p => p.FavGames).FirstOrDefault(); _context.Attach(user); if (user.Auth_token == favourite.Auth_token) { } _context.Entry(user).Collection(e => e.FavGames).IsModified = true; _context.Entry(user).State = EntityState.Modified; _context.Users.Update(user).Entity.FavGames.Add(favourite); await _context.SaveChangesAsync(); return(new JsonResult(user)); }
public async Task AddScoreAsync(int score, string userEmail) { user = await _context.Users.Where(u => u.Email == userEmail).FirstOrDefaultAsync(); if (score > user.Score) { user.Score = score; _context.Attach(user).State = EntityState.Modified; await _context.SaveChangesAsync(); } }
public async Task <ActionResult <FavouriteDeveloper> > PostFavouriteDeveloper(FavouriteDeveloper favouriteDeveloper) { User user = _context.Users.Where(u => u.Id == favouriteDeveloper.UserId).Include(p => p.FavPublishers).FirstOrDefault(); _context.Attach(user); _context.FavouriteDeveloper.Add(favouriteDeveloper); _context.Users.Update(user).Entity.FavDeveloper.Add(favouriteDeveloper); await _context.SaveChangesAsync(); return(CreatedAtAction("GetFavouriteDeveloper", new { id = favouriteDeveloper.Id }, favouriteDeveloper)); }
public void Setting_IsModified_should_not_be_dependent_on_other_properties() { Guid id; using (var context = new UserContext()) { id = context.Add( new User { Name = "A", LongName = "B" }).Entity.Id; context.SaveChanges(); } using (var context = new UserContext()) { var user = context.Attach( new User { Id = id, Name = "NewA", LongName = "NewB" }).Entity; context.Entry(user).Property(x => x.Name).IsModified = false; context.Entry(user).Property(x => x.LongName).IsModified = true; Assert.False(context.Entry(user).Property(x => x.Name).IsModified); Assert.True(context.Entry(user).Property(x => x.LongName).IsModified); context.SaveChanges(); } using (var context = new UserContext()) { var user = context.Find <User>(id); Assert.Equal("A", user.Name); Assert.Equal("NewB", user.LongName); } }
public async Task <IActionResult> OnPostAsync(int id) { if (!ModelState.IsValid) { return(Page()); } if (id == 0) { _context.User.Add(User); } else { _context.Attach(User).State = EntityState.Modified; } await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }