title: How can I extend Identity with custom attributes in ASP.NET MVC?
body: <p>I seem to have a hate/love relation with the Identity. I love it because it is a complete solution for most applications. But I hate it because extending is not an easy task. I feel it is more complected than it should be.</p>
  
  <p>I am trying to add custom attributes and foreign keys to my user model but it seems to be a very difficult task. </p>
  
  <p>I need to add a new <code>Identity</code> field called <code>UserId</code> since Id is a string which will be auto generated by the database. Then, I need to add a foreign key to <code>Company</code> model and another one to <code>Location</code> model.</p>
  
  <p>I followed the instruction in the <a href="https://stackoverflow.com/questions/28335353/how-to-extend-available-properties-of-user-identity">answer from this other question</a> in an attempt to add two new foreign keys and be able to get their value from my controllers.</p>
  
  <p>Here is what I have done so far. My <code>ApplicationUser</code> class after the modifications I made look like this</p>
  
  <pre><code>public class ApplicationUser : IdentityUser
  {
      [Key]
      public int MyUserId { get; set; }
  
      [ForeignKey("Company")]
      public int CompanyId { get; set; }
  
      [ForeignKey("Location")]
      public int CurrentLocationId { get; set; }
  
      public virtual Company Company { get; set; }
  
      public virtual Location Location { get; set; }
  
      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
  
          userIdentity.AddClaim(new Claim("MyUserId", this.MyUserId.ToString() ));
  
          userIdentity.AddClaim(new Claim("CompanyId", this.CompanyId.ToString() ));
  
          userIdentity.AddClaim(new Claim("CurrentLocationId", this.CurrentLocationId.ToString()));
  
          return userIdentity;
      }
  }
  </code></pre>
  
  <p>I also created an extensions class that allows me to get the values from the controllers like so</p>
  
  <pre><code>public static class IdentityExtensions
  {
      public static int GetComapnyId(this IIdentity identity)
      {
          var claim = ((ClaimsIdentity)identity).FindFirst("ComapnyId");
          // Test for null to avoid issues during local testing
          return (claim != null) ? Int32.Parse(claim.Value) : 0;
      }
  
      public static int GetCurrentLocationId(this IIdentity identity)
      {
          var claim = ((ClaimsIdentity)identity).FindFirst("CurrentLocationId");
          // Test for null to avoid issues during local testing
          return (claim != null) ? Int32.Parse(claim.Value) : 0;
      }
  
  
      public static int GetMyUserId(this IIdentity identity)
      {
          var claim = ((ClaimsIdentity)identity).FindFirst("MyUserId");
          // Test for null to avoid issues during local testing
          return (claim != null) ? Int32.Parse(claim.Value) : 0;
      }
  
  }
  </code></pre>
  
  <p>But I am running into the error "listed below" when I try to add a new migration</p>
  
  <p>Here is the error</p>
  
  <p>One or more validation errors were detected during model generation:</p>
  
  <blockquote>
    <p>ApplicationUser_Claims_Source_ApplicationUser_Claims_Target: : The
    types of all properties in the Dependent Role of a referential
    constraint must be the same as the corresponding property types in the
    Principal Role. The type of property 'MyUserId' on entity
    'IdentityUserClaim' does not match the type of property 'MyUserId' on
    entity 'ApplicationUser' in the referential constraint
    'ApplicationUser_Claims'.
    ApplicationUser_Logins_Source_ApplicationUser_Logins_Target: : The
    types of all properties in the Dependent Role of a referential
    constraint must be the same as the corresponding property types in the
    Principal Role. The type of property 'MyUserId' on entity
    'IdentityUserLogin' does not match the type of property 'MyUserId' on
    entity 'ApplicationUser' in the referential constraint
    'ApplicationUser_Logins'.
    ApplicationUser_Roles_Source_ApplicationUser_Roles_Target: : The types
    of all properties in the Dependent Role of a referential constraint
    must be the same as the corresponding property types in the Principal
    Role. The type of property 'MyUserId' on entity 'IdentityUserRole' does
    not match the type of property 'MyUserId' on entity 'ApplicationUser' in
    the referential constraint 'ApplicationUser_Roles'.</p>
  </blockquote>
  
  <p>This is the command I used to created the <code>InitialCreate</code> migration</p>
  
  <pre><code>Add-Migration InitialCreate
  </code></pre>
  
  <p>How can I add my foreign key and be able to get them from the controllers correctly?</p>