0

I'm trying to find a way that would allow me to update a UserProfile entity along with a list of Roles that the user is assigned to. I've written the code below, but it doesn't work.

public void UpdateUserProfile(UserProfile userProfile)
{
  context.Entry(userProfile).State = EntityState.Added;

  var databaseRoleIds = context.Roles
    .Where(r => r.UserProfiles
      .Any(u => u.UserId == userProfile.UserId))
      .Select(r => r.RoleId).ToList();

  var clientRoleIds = userProfile.Roles.Select(r => r.RoleId).ToList();

  var removedRoleIds = databaseRoleIds.Except(clientRoleIds).ToList();

  var addedRoleIds = clientRoleIds.Except(databaseRoleIds).ToList();

  var unchangedRoleIds = removedRoleIds.Union(addedRoleIds).ToList();

  foreach (var roleId in unchangedRoleIds)
  {
    var role = context.Roles.Find(roleId);
    context.Entry(role).State = EntityState.Unchanged;
  }

  foreach (var roleId in removedRoleIds)
  {
    userProfile.RemoveRole(context.Roles.Find(roleId));
  }

  foreach (var roleId in addedRoleIds)
  {
    userProfile.AddRole(context.Roles.Find(roleId));
  }

  context.Entry(userProfile).State = EntityState.Modified;

}

Here is the unitOfWork

namespace MvcWebsite.WorkUnits
{
public class WorkUnit : IWorkUnit, IDisposable
{
private MvcContext context = new MvcContext();
private RoleRepository roleRepository;
private UserProfileRepository userProfileRepository;

public IRoleRepository RoleRepository
{
  get
  {
    if (this.roleRepository == null)
    {
      roleRepository = new RoleRepository(context);
    }
    return roleRepository;
  }
}

public IUserProfileRepository UserProfileRepository
{
  get
  {
    if (this.userProfileRepository == null)
    {
      userProfileRepository = new UserProfileRepository(context);
    }
    return userProfileRepository;
  }
}

public void Save()
{
  context.SaveChanges();
}

private bool disposed = false;

protected virtual void Dispose(bool disposing)
{
  if (!this.disposed)
  {
    if (disposing)
    {
      context.Dispose();
    }
  }
  this.disposed = true;
}

public void Dispose()
{
  Dispose(true);
  GC.SuppressFinalize(this);
}

}
}

... and here is the HttpPost Edit method

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(UserProfileEditViewModel model)
    {
      try
      {
        if (ModelState.IsValid)
        {
          var clientUserProfile = new UserProfile();
          clientUserProfile.UserId = model.UserId;
          clientUserProfile.UserName = model.UserName;
          clientUserProfile.FirstName = model.FirstName;
          clientUserProfile.LastName = model.LastName;
          clientUserProfile.Email = model.Email;
          clientUserProfile.RowVersion = model.RowVersion;
          clientUserProfile.Roles = new List<Role>();
          foreach(var role in model.Roles)
          {
            if (role.Assigned)
            {
              clientUserProfile.Roles.Add(new Role
              {
                RoleId = role.RoleId,
                RoleName = role.RoleName,
                RowVersion = role.RowVersion,
              });
            }

          }
          unitOfWork.UserProfileRepository.UpdateUserProfile(clientUserProfile);
          unitOfWork.Save();
          return RedirectToAction("Details", new { id = clientUserProfile.UserId });

        }
      }
      catch (DataException ex)
      {
        ModelState.AddModelError("", "Unable to save changes.  Try again, and if the problem persists, see your system administrator.");
      }
      return View(model);
    }

Does anyone have any idea why this isn't working? Or could suggest a tutorial somewhere that actually works. Any help, as always, is greatly appreciated.

2
  • You're changing the entity state around, but you're not calling SaveChanges(). Do you do that outside of the function?
    – bcr
    Jun 6, 2013 at 1:58
  • Yes, I've employed the UnitOfWork pattern, so this is method comes from UnitOfWork.UserProfileRepository.UpdateUserProfile. This UnitOfWork holds a single context that is passed to each repository used and then calls SaveChanges on that context. I've edited the code above to show the controller method.
    – MickySmig
    Jun 6, 2013 at 12:28

2 Answers 2

0

Instead of creating a new UserProfile in your controller, get the UserProfile from the repository, modify its fields, then send it back to UpdateUserProfile and call Save.

2
  • That was what I thought of at first, but that will change the OriginalValue of RowVersion, if another user has updated in the meantime. Entity Framework uses the (read only) OriginalValues when it compares entities to assess if a concurrency violation has occured
    – MickySmig
    Jun 6, 2013 at 13:40
  • It seems you are facing two distinct issues: 1) Updating an Entity and 2) Concurrency Strategy.
    – Jasen
    Jun 6, 2013 at 18:17
0

Finally found that I had it totally wrong in the first place. I wasn't changing the relationships at all. I've included the code below, which allows me to attach the parent entity as modified and then mark the relationships as added and deleted as required

public void UpdateUserProfile(UserProfile userProfile)
{
  context.Entry(userProfile).State = EntityState.Modified;
  var objectContext = ((IObjectContextAdapter)context).ObjectContext;

  foreach (var role in userProfile.Roles)
  {
    context.Entry(role).State = EntityState.Unchanged;
  }

  var databaseRoleIds = context.Roles
    .Where(r => r.UserProfiles
      .Any(u => u.UserId == userProfile.UserId))
      .Select(r => r.RoleId)
      .ToList();

  var clientRoleIds = userProfile.Roles.Select(r => r.RoleId).ToList();

  var removedRoleIds = databaseRoleIds.Except(clientRoleIds).ToList();

  var addedRoleIds = clientRoleIds.Except(databaseRoleIds).ToList();

  foreach (var roleId in removedRoleIds)
  {
    var role = context.Roles.Find(roleId);
    objectContext
      .ObjectStateManager
      .ChangeRelationshipState(userProfile, role, u => u.Roles, EntityState.Deleted);
  }

  foreach (var roleId in addedRoleIds)
  {
    var role = context.Roles.Find(roleId);
    objectContext
      .ObjectStateManager
      .ChangeRelationshipState(userProfile, role, u => u.Roles, EntityState.Added);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.