title: Entity Framework and ASP.net MVC - Updating Entity Model with Many-to-Many relationship
body: <p>Using Asp.Net Identity and Entity Framework, I'am trying to update AspNetUser, and change its AspNetRole. </p>
  
  <p>This is my UserDto class :</p>
  
  <pre><code>public class UserDto : BaseDto
    {
          public string Uuid { get; set; }
          public string Email { get; set; }
          public string FullName { get; set; }
          public string UserName { get; set; }
          public List&lt;RoleDto&gt; Roles { get; set; }
          public int NumberOfLikes { get; set; }
          public int NumberOfComments { get; set; }
          public int NumberOfReviews { get; set; }
    }
  </code></pre>
  
  <p>This is my RoleDto class:</p>
  
  <pre><code>public class RoleDto : BaseDto
      {
          [ScaffoldColumn(false)]
          public string Uuid { get; set; }
  
          [Required(ErrorMessage = "This field is required")]
          [MinLength(3, ErrorMessage = "Minimum 3 characters")]
          [DisplayName("Role Name")]
          public string Name { get; set; }
  
          [ScaffoldColumn(false)]
          public int NumberOfUsers { get; set; }
      }
  </code></pre>
  
  <p>This is my Update method in UserController:</p>
  
  <pre><code>        [HttpPost]
          public ActionResult Edit(UserViewModel vm)
          {
              UserDto dto = new UserDto
              {
                  Uuid = vm.Uuid,
                  Email = vm.Email,
                  FullName = vm.FullName,
                  UserName = vm.UserName,
                  Roles = new List&lt;RoleDto&gt;()
              };
              dto.Roles.Add(new RoleDto
              {
                  Uuid = vm.RoleId
              });
              OpUserUpdate update = new OpUserUpdate();
              update.userDto = dto;
  
              var result = this.Manager.ExecuteOperation(update);
  
              return RedirectToAction("Index");
          }
  </code></pre>
  
  <p>This is User Update Operation:</p>
  
  <pre><code>    public class OpUserUpdate : OpUserBase
      {
          public override OperationResult Execute(FoodRestaurantEntities entities)
          {
              var id = this.userDto.Roles[0].Uuid;
              if (!string.IsNullOrWhiteSpace(this.userDto.Uuid))
              {
                  var user = entities.AspNetUsers.FirstOrDefault(u =&gt; u.Id == this.userDto.Uuid);
                  var role = entities.AspNetRoles.FirstOrDefault(r =&gt; r.Id == id);
                  if(user != null)
                  {
                      // If is set email
                      if (!string.IsNullOrWhiteSpace(this.userDto.Email))
                          user.Email = this.userDto.Email;
  
                      // If is set full name
                      if (!string.IsNullOrWhiteSpace(this.userDto.FullName))
                          user.FullName = this.userDto.FullName;
  
                      // If is set full name
                      if (!string.IsNullOrWhiteSpace(this.userDto.UserName))
                          user.UserName = this.userDto.UserName;
  
                      user.AspNetRoles.Add(role);
                      entities.SaveChanges();
                      this.Result.Status = true;
                      this.Result.Message = "Successfully updated";
                  }
                  else
                  {
                      this.Result.Status = false;
                      this.Result.Message = "Not successfull";
                  }
  
              }
  
              return Result;
          }
      }
  </code></pre>
  
  <p>1. OperationManager class just executes operation
  2. The problem is, that <strong>user.AspNetRoles.Add(role)</strong> line just adds another Role to a user. For example, if user had User role, it would add Admin role also instead of changing it to Admin. What am I doing wrong? </p>