title: What is the right way to extend AspnetUserRoles table in ASPNET.Core 3.1?
body: <p><strong>Situation</strong></p>
  
  <p>I'm using Identity ASP.NET Core 3.1 with Angular 8 Template. I want to extend the ASPNETUserRoles table and add another custom key column CompanyId in it.</p>
  
  <p>By Default Identity provides:</p>
  
  <pre><code>public virtual TKey UserId { get; set; }
  public virtual TKey RoleId { get; set; }
  </code></pre>
  
  <p>As I modified my DbContext from UserId (string) to UserId (long), DbContext looks like:</p>
  
  <pre><code>public class CompanyDBContext : KeyApiAuthorizationDbContext&lt;User, Role, UserRole, long&gt;
  {
      public CompanyDBContext(
          DbContextOptions options,
          IOptions&lt;OperationalStoreOptions&gt; operationalStoreOptions) : base(options, operationalStoreOptions)
          {
          }
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              base.OnModelCreating(modelBuilder);
          }
      public DbSet&lt;Company&gt; Companies { get; set; }
  
  }
  </code></pre>
  
  <p><strong>KeyApiAuthorizationDbContext</strong></p>
  
  <pre><code>public class KeyApiAuthorizationDbContext&lt;TUser, TRole, IdentityUserRole, TKey&gt; : IdentityDbContext&lt;TUser, TRole, TKey, IdentityUserClaim&lt;TKey&gt;, IdentityUserRole&lt;TKey&gt;, IdentityUserLogin&lt;TKey&gt;, IdentityRoleClaim&lt;TKey&gt;, IdentityUserToken&lt;TKey&gt;&gt;, IPersistedGrantDbContext
      where TUser : IdentityUser&lt;TKey&gt;
      where TRole : IdentityRole&lt;TKey&gt;
      where IdentityUserRole : IdentityUserRole&lt;TKey&gt;
      where TKey : IEquatable&lt;TKey&gt;
  {
      private readonly IOptions&lt;OperationalStoreOptions&gt; _operationalStoreOptions;
  
      /// &lt;summary&gt;
      /// Initializes a new instance of &lt;see cref="ApiAuthorizationDbContext{TUser, TRole, TKey}"/&gt;.
      /// &lt;/summary&gt;
      /// &lt;param name="options"&gt;The &lt;see cref="DbContextOptions"/&gt;.&lt;/param&gt;
      /// &lt;param name="operationalStoreOptions"&gt;The &lt;see cref="IOptions{OperationalStoreOptions}"/&gt;.&lt;/param&gt;
      public KeyApiAuthorizationDbContext(
          DbContextOptions options,
          IOptions&lt;OperationalStoreOptions&gt; operationalStoreOptions)
          : base(options)
      {
          _operationalStoreOptions = operationalStoreOptions;
      }
  
      /// &lt;summary&gt;
      /// Gets or sets the &lt;see cref="DbSet{PersistedGrant}"/&gt;.
      /// &lt;/summary&gt;
      public DbSet&lt;PersistedGrant&gt; PersistedGrants { get; set; }
  
      /// &lt;summary&gt;
      /// Gets or sets the &lt;see cref="DbSet{DeviceFlowCodes}"/&gt;.
      /// &lt;/summary&gt;
      public DbSet&lt;DeviceFlowCodes&gt; DeviceFlowCodes { get; set; }
  
      Task&lt;int&gt; IPersistedGrantDbContext.SaveChangesAsync() =&gt; base.SaveChangesAsync();
  
      /// &lt;inheritdoc /&gt;
      protected override void OnModelCreating(ModelBuilder builder)
      {
          base.OnModelCreating(builder);
          builder.ConfigurePersistedGrantContext(_operationalStoreOptions.Value);
      }
  }
  </code></pre>
  
  <p><strong>Entities</strong></p>
  
  <pre><code>public class User : IdentityUser&lt;long&gt; {}
  public class Role : IdentityRole&lt;long&gt; {}
  public class UserRole : IdentityUserRole&lt;long&gt;
  {
      public long CompanyId { get; set; }
  }
  </code></pre>
  
  <p><strong>Problem Occur</strong></p>
  
  <p>When I registered my user and it returns true then I'll add a current user in UserRole table like below but when my debugger reached <code>await _context.SaveChangesAsync();</code> method its shows me an exception </p>
  
  <pre><code>if (result.Succeeded)
  {
      foreach (var role in model.Roles.Where(x =&gt; x.IsChecked = true))
      {
          var entity = new Core.Entities.Identity.UserRole()
          {
              UserId = model.User.Id,
              RoleId = role.Id,
              CompanyId = companycode
          };
  
              _context.UserRoles.Add(entity);
      }
      await _context.SaveChangesAsync();
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/UArwO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UArwO.png" alt="enter image description here"></a></p>
  
  <p>I don't know where I'm making my mistake? If above steps of overriding userrole are wrong then assist me on that. </p>
  
  <p>I'm also sharing my migration details for your information may be something I'm doing wrong there.</p>
  
  <p><strong>Migrations</strong></p>
  
  <pre><code>migrationBuilder.CreateTable(
  name: "Companies",
  columns: table =&gt; new
  {
     Id = table.Column&lt;long&gt;(nullable: false)
         .Annotation("SqlServer:Identity", "1, 1"),
                  Name = table.Column&lt;string&gt;(nullable: false),
                  Code = table.Column&lt;string&gt;(nullable: false),
                  Logo = table.Column&lt;string&gt;(nullable: true)
         },
         constraints: table =&gt;
         {
                  table.PrimaryKey("PK_Companies", x =&gt; x.Id);
  });
  
  
  migrationBuilder.CreateTable(
                  name: "AspNetUserRoles",
                  columns: table =&gt; new
                  {
                      UserId = table.Column&lt;long&gt;(nullable: false),
                      RoleId = table.Column&lt;long&gt;(nullable: false),
                      CompanyId = table.Column&lt;long&gt;(nullable: false)
                  },
                  constraints: table =&gt;
                  {
                      table.PrimaryKey("PK_AspNetUserRoles", x =&gt; new { x.UserId, x.RoleId, x.CompanyId });
                      table.ForeignKey(
                          name: "FK_AspNetUserRoles_AspNetRoles_RoleId",
                          column: x =&gt; x.RoleId,
                          principalTable: "AspNetRoles",
                          principalColumn: "Id",
                          onDelete: ReferentialAction.Cascade);
                      table.ForeignKey(
                          name: "FK_AspNetUserRoles_AspNetUsers_UserId",
                          column: x =&gt; x.UserId,
                          principalTable: "AspNetUsers",
                          principalColumn: "Id",
                          onDelete: ReferentialAction.Cascade);
                      table.ForeignKey(
                          name: "FK_AspNetUserRoles_Companies_CompanyId",
                          column: x =&gt; x.CompanyId,
                          principalTable: "Companies",
                          principalColumn: "Id",
                          onDelete: ReferentialAction.Cascade);
                  });
  </code></pre>
  
  <p><strong>Migration Snapshot</strong></p>
  
  <pre><code>modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim&lt;long&gt;", b =&gt;
                  {
                      b.Property&lt;int&gt;("Id")
                          .ValueGeneratedOnAdd()
                          .HasColumnType("int")
                          .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
  
                      b.Property&lt;string&gt;("ClaimType")
                          .HasColumnType("nvarchar(max)");
  
                      b.Property&lt;string&gt;("ClaimValue")
                          .HasColumnType("nvarchar(max)");
  
                      b.Property&lt;long&gt;("RoleId")
                          .HasColumnType("bigint");
  
                      b.HasKey("Id");
  
                      b.HasIndex("RoleId");
  
                      b.ToTable("AspNetRoleClaims");
                  });
  </code></pre>