title: Seeding asp.net core Identity Role
body: <p>I'm trying to seed the AspNetRole table with initial system roles. </p>
  
  <p>Seeding Extension:</p>
  
  <pre><code>public static void EnsureRolesAreCreated(this IApplicationBuilder app) {
          Dictionary&lt;string, string&gt; roles = new Dictionary&lt;string, string&gt;
          {
              { "Administrator", "Global Access." },
              { "User", "Restricted to business domain activity." }
          };
  
          var context = app.ApplicationServices.GetService&lt;ApplicationDbContext&gt;();
          if (context.AllMigrationsApplied()) {
              var roleManager = app.ApplicationServices.GetService&lt;ApplicationRoleManager&gt;();
              foreach (var role in roles) {
                  if (!roleManager.RoleExistsAsync(role.Key).Result) {
                      roleManager.CreateAsync(new ApplicationRole() { Name = role.Key, System = true, Description = role.Value });
                  }
              }
          }
      }
  </code></pre>
  
  <p>which is called in startup class: app <code>app.EnsureRolesAreCreated();</code></p>
  
  <p>when the code above runs, the CreateAsync function throws an Exception:</p>
  
  <blockquote>
    <p>{Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> 
    System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Id', table 'AspNetRoles'; column does not allow nulls. INSERT fails.
    The statement has been terminated.</p>
  </blockquote>
  
  <p>I have tried <a href="https://github.com/aspnet/Identity/issues/606" rel="nofollow">This</a> and <a href="https://github.com/aspnet/Identity/issues/780" rel="nofollow">this</a>, still not working. </p>
  
  <pre><code>services.AddIdentity&lt;ApplicationUser, ApplicationRole&gt;(config =&gt; {
              config.User.RequireUniqueEmail = true;
              config.Lockout = new LockoutOptions {
                  AllowedForNewUsers = true,
                  DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30),
                  MaxFailedAccessAttempts = 5
              };
              config.Password = new PasswordOptions {
                  RequireDigit = true,
                  RequireNonAlphanumeric = false,
                  RequireUppercase = true,
                  RequireLowercase = true,
                  RequiredLength = 12,
              };
          })
          .AddEntityFrameworkStores&lt;ApplicationDbContext, int&gt;()
          .AddUserValidator&lt;ApplicationUserValidator&lt;ApplicationUser&gt;&gt;()
          .AddUserManager&lt;ApplicationUserManager&gt;()
          .AddRoleManager&lt;ApplicationRoleManager&gt;()
          .AddDefaultTokenProviders();
  
  
  
  public class ApplicationUser : IdentityUser&lt;int&gt; {}
  public class ApplicationRole : IdentityRole&lt;int&gt; {}
  
  
  protected override void OnModelCreating(ModelBuilder modelBuilder) {
  
          modelBuilder.Entity&lt;ApplicationUser&gt;(i =&gt; {
              i.HasKey(x =&gt; x.Id);
              i.Property(x =&gt; x.Id).ValueGeneratedOnAdd();
          });
          modelBuilder.Entity&lt;ApplicationRole&gt;(i =&gt; {
              i.HasKey(x =&gt; x.Id);
              i.Property(x =&gt; x.Id).ValueGeneratedOnAdd();
          });
          modelBuilder.Entity&lt;IdentityUserRole&lt;int&gt;&gt;(i =&gt; {
              i.HasKey(x =&gt; new { x.RoleId, x.UserId });
          });
  
          modelBuilder.Entity&lt;IdentityUserLogin&lt;int&gt;&gt;(i =&gt; {
              i.HasKey(x =&gt; new { x.ProviderKey, x.LoginProvider });
          });
  
          modelBuilder.Entity&lt;IdentityRoleClaim&lt;int&gt;&gt;(i =&gt; {
              i.HasKey(x =&gt; x.Id);
              i.Property(x =&gt; x.Id).ValueGeneratedOnAdd();
          });
  
          modelBuilder.Entity&lt;IdentityUserClaim&lt;int&gt;&gt;(i =&gt; {
              i.HasKey(x =&gt; x.Id);
              i.Property(x =&gt; x.Id).ValueGeneratedOnAdd();
          });
     }
  </code></pre>
  
  <p>I added <code>ValueGeneratedOnAdd()</code> in an attempt to force the ID generation. </p>