title: ASP.NET MVC 5 How to update custom property of ApplicationUser
body: <p>as I wrote in title, I have this code:</p>
  
  <pre><code>public class ApplicationUser : IdentityUser
  {
      public virtual MapPosition MapPosition { get; set; }
  
      public ApplicationUser()
      {
          MapPosition = new MapPosition { PositionX = 0, PositionY = 0 };
      }
  
      public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser&gt; manager)
      {
          // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
          var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
          // Add custom user claims here
          return userIdentity;
      }
  }
  
  public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
  {
      public DbSet&lt;MapPosition&gt; MapPositions { get; set; }
  
      public ApplicationDbContext()
          : base("DefaultConnection", throwIfV1Schema: false)
      {
      }
  
      public static ApplicationDbContext Create()
      {
          return new ApplicationDbContext();
      }
  }
  </code></pre>
  
  <p>And in my controller I have method that I call from @Ajax.ActionLink in my view:</p>
  
  <pre><code>public string ChangeXPosition()
      {
          var manager = new UserManager&lt;ApplicationUser&gt;(new UserStore&lt;ApplicationUser&gt;(new ApplicationDbContext()));
          // Get the current logged in User and look up the user in ASP.NET Identity
          currentUser = manager.FindById(User.Identity.GetUserId());
          currentUser.MapPosition.PositionX++;
  
          //manager.Update(currentUser);
          Debug.WriteLine("currentUser.MapPosition.PositionX: " + currentUser.MapPosition.PositionX);
  
          return "currentUser.MapPosition.PositionX: " + currentUser.MapPosition.PositionX;
      }
  </code></pre>
  
  <p>I want to save to database changed value of currentUser.MapPosition.PositionX.
  I have found numerous ways to solve this problem, but none of them worked with my project.</p>
  
  <p>I tried those solutions:</p>
  
  <p><a href="https://stackoverflow.com/questions/22818645/how-to-update-identityuser-with-custom-properties-using-mvc5-and-entity-framewor">how-to-update-identityuser-with-custom-properties-using-mvc5-and-entity-framewor</a></p>
  
  <p><a href="https://stackoverflow.com/questions/20444022/updating-user-data-asp-net-identity">updating-user-data-asp-net-identity</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19348966/mvc5-applicationuser-custom-properties">mvc5-applicationuser-custom-properties</a></p>
  
  <p><strong>UPDATE</strong>
  Ok so I tried to do as you said, but still doesn't work:</p>
  
  <pre><code>public class ApplicationUser : IdentityUser
  {
      public virtual MapPosition MapPosition { get; set; }
  
      public ApplicationUser()
      {
      }
  }
  </code></pre>
  
  <p>This version works, my controller method does change the value in the table.
  But it works only for user, that have been already created. When I create a new User, his new MapPositions record is not created.</p>
  
  <pre><code>public class ApplicationUser : IdentityUser
  {
      private MapPosition _mapPosition;
      public virtual MapPosition MapPosition
      {
          get { return _mapPosition ?? (_mapPosition = new MapPosition()); }
      }
  
      public ApplicationUser()
      {
      }
  }
  </code></pre>
  
  <p>This version doesn't work at all, doesn't change the value in database and doesn't create record in MapPositions when new user is created.</p>