title: How UserManager is linked with DbContext?
body: <p>I have the following code:</p>
  
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
      public ApplicationDbContext()
          : base("AssetContext", throwIfV1Schema: false)
      {
      }
  
      public static ApplicationDbContext Create()
      {
          return new ApplicationDbContext();
      }
  }
  
  
  public class ApplicationUserManager : UserManager&lt;ApplicationUser&gt;
  {
      public ApplicationUserManager(IUserStore&lt;ApplicationUser&gt; store)
          : base(store)
      {
      }
  
      public static ApplicationUserManager Create(
                IdentityFactoryOptions&lt;ApplicationUserManager&gt; options,
                IOwinContext context)
      {
          ApplicationDbContext dbContext = context.Get&lt;ApplicationDbContext&gt;();
          return Create(options, dbContext);
      }
      .....
  }
  
      private ApplicationUserManager _userManager;
      public ApplicationUserManager UserManager
      {
          get
          {
              return _userManager ?? HttpContext.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;();
          }
          private set
          {
              _userManager = value;
          }
      }
  </code></pre>
  
  <p>then I have the controller method:</p>
  
  <pre><code>    public async Task&lt;ActionResult&gt; EditUser(UserEditViewModel model)
      {
                  var user = mapper.Map&lt;UserEditViewModel, AspNetUser&gt;(model, _db.AspNetUsers.Where(p =&gt; p.UserName == model.UserName).FirstOrDefault());
  
                      user.CompanyId = null;
                      user.LockoutEnabled = false;
                      user.LockoutEndDateUtc = null;
  
  
                  _db.SaveChanges();
  
                      var roles = UserManager.GetRoles(user.Id);
                      UserManager.AddToRole(user.Id, model.RoleName);
                      UserManager.RemoveFromRoles(user.Id, roles.ToArray());
  
                  return RedirectToAction("UserList");
      }
  </code></pre>
  
  <p>(please, don't say me about DI, I'm not an owner of code :) )</p>
  
  <p>Problem is that all changes in user :</p>
  
  <pre><code>                    user.CompanyId = null;
                      user.LockoutEnabled = false;
                      user.LockoutEndDateUtc = null;
  </code></pre>
  
  <p>is rewritten when I call </p>
  
  <pre><code>UserManager.AddToRole(user.Id, model.RoleName);
  </code></pre>
  
  <p>why it happens?</p>