title: How to Create Tables Automatically in Code First Approach using PostgreSql
body: <p>I am trying to create the tables using code first approach with postgresql database. My DB context is below.While doing migration i am getting the below error.
  <strong>Cannot use table 'AspNetRoles' for entity type 'AspNetRole' since it is being used for entity type 'IdentityRole' and there is no relationship between their primary keys.</strong></p>
  
  <p>How to resolve this?</p>
  
  <pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
      {
          public DbSet&lt;Project&gt; Projects { get; set; }
          public DbSet&lt;Story&gt; Stories { get; set; }
          public DbSet&lt;Epic&gt; Epics { get; set; }
          public DbSet&lt;Favourite&gt; Favourites { get; set; }
          public DbSet&lt;Priority&gt; Priorities { get; set; }
          public DbSet&lt;ProjectPermission&gt; ProjectPermission { get; set; }
          public DbSet&lt;Theme&gt; Themes { get; set; }
          public DbSet&lt;AspNetRole&gt; AspNetRoles { get; set; }
          public DbSet&lt;AspNetRoleClaim&gt; AspNetRoleClaims { get; set; }
          public DbSet&lt;AspNetUser&gt; AspNetUsers { get; set; }
          public DbSet&lt;AspNetUserClaim&gt; AspNetUserClaims { get; set; }
          public DbSet&lt;AspNetUserLogin&gt; AspNetUserLogins { get; set; }
          public DbSet&lt;AspNetUserToken&gt; AspNetUserTokens { get; set; }
          public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options)
              : base(options)
          {
          }
  
          protected override void OnModelCreating(ModelBuilder builder)
          {
              builder.Entity&lt;AspNetUserToken&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.UserId);
                  entity.HasKey(e =&gt; e.LoginProvider);
                  entity.HasKey(e =&gt; e.Name);
  
              });
  
              builder.Entity&lt;AspNetRole&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.Id);
              });
  
              builder.Entity&lt;AspNetRoleClaim&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.Id);
              });
  
              builder.Entity&lt;AspNetUser&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.Id);
              });
  
              builder.Entity&lt;AspNetUserClaim&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.Id);
              });
  
              builder.Entity&lt;AspNetUserLogin&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.ProviderKey);
                  entity.HasKey(e =&gt; e.LoginProvider);
  
                  entity.HasIndex(e =&gt; e.UserId)
                      .HasName("fki_FK_AspNetUserLoginUserId");
  
              });
              builder.Entity&lt;Project&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.ProjectId);
  
                  entity.HasIndex(e =&gt; e.CreatedBy)
                      .HasName("fki_FK_ProjectCreatedBy");
  
                  entity.HasIndex(e =&gt; e.Owner)
                      .HasName("fki_FK_ProjectOwner");
  
  
                  entity.HasOne(d =&gt; d.AspNetUser)
                      .WithMany(p =&gt; p.Projects)
                      .HasForeignKey(d =&gt; d.CreatedBy)
                      .HasForeignKey(a =&gt; a.Owner)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_ProjectCreatedBy");
              });
  
              builder.Entity&lt;Story&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.StoryId);
  
                  entity.HasIndex(e =&gt; e.ProjectId)
                      .HasName("fki_FK_StoryProjectId");
  
                  entity.HasIndex(e =&gt; e.ThemeId)
                      .HasName("fki_FK_StoryThemeId");
  
                  entity.HasIndex(e =&gt; e.EpicId)
                      .HasName("fki_FK_StoryEpicId");
  
                  entity.HasIndex(e =&gt; e.PriorityId)
                      .HasName("fki_FK_StoryPriorityId");
  
                  entity.HasIndex(e =&gt; e.CreatedBy)
                      .HasName("fki_FK_StoryCreatedBy");
  
                  entity.HasOne(d =&gt; d.AspNetUser)
                      .WithMany(p =&gt; p.Stories)
                      .HasForeignKey(d =&gt; d.CreatedBy)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_StoryCreatedBy");
  
                  entity.HasOne(d =&gt; d.Project)
                      .WithMany(p =&gt; p.Stories)
                      .HasForeignKey(d =&gt; d.ProjectId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_StoryProjectId");
  
                  entity.HasOne(d =&gt; d.Epic)
                      .WithMany(p =&gt; p.Stories)
                      .HasForeignKey(d =&gt; d.EpicId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_StoryEpicId");
  
                  entity.HasOne(d =&gt; d.Theme)
                      .WithMany(p =&gt; p.Stories)
                      .HasForeignKey(d =&gt; d.ThemeId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_StoryThemeId");
  
                  entity.HasOne(d =&gt; d.Priority)
                      .WithMany(p =&gt; p.Stories)
                      .HasForeignKey(d =&gt; d.PriorityId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_StoryPriorityId");
              });
  
              builder.Entity&lt;Epic&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.EpicId);
  
                  entity.HasIndex(e =&gt; e.CreatedBy)
                      .HasName("fki_FK_EpicCreatedBy");
  
                  entity.HasIndex(e =&gt; e.ProjectId)
                      .HasName("fki_FK_EpicProjectId");
  
  
                  entity.HasOne(d =&gt; d.AspNetUser)
                      .WithMany(p =&gt; p.Epics)
                      .HasForeignKey(d =&gt; d.CreatedBy)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_EpicCreatedBy");
  
                  entity.HasOne(d =&gt; d.Project)
                      .WithMany(p =&gt; p.Epics)
                      .HasForeignKey(d =&gt; d.ProjectId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_EpicProjectId");
              });
  
              builder.Entity&lt;Favourite&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.FavouriteId);
  
                  entity.HasIndex(e =&gt; e.UserId)
                      .HasName("fki_FK_FavouriteUserId");
  
                  entity.HasIndex(e =&gt; e.ProjectId)
                      .HasName("fki_FK_FavouriteProjectId");
  
  
                  entity.HasOne(d =&gt; d.Project)
                      .WithMany(p =&gt; p.Favourites)
                      .HasForeignKey(d =&gt; d.ProjectId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_FavouriteProjectId");
              });
  
              builder.Entity&lt;Priority&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.PriorityId);
  
                  entity.HasIndex(e =&gt; e.CreatedBy)
                      .HasName("fki_FK_PriorityCreatedBy");
  
  
                  entity.HasOne(d =&gt; d.AspNetUser)
                      .WithMany(p =&gt; p.Priorities)
                      .HasForeignKey(d =&gt; d.CreatedBy)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_PriorityCreatedBy");
              });
              builder.Entity&lt;ProjectPermission&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.PermissionId);
  
                  entity.HasIndex(e =&gt; e.ProjectId)
                      .HasName("fki_FK_ProjectPermissionProjectId");
  
                  entity.HasOne(d =&gt; d.Project)
                      .WithMany(p =&gt; p.ProjectPermissions)
                      .HasForeignKey(d =&gt; d.ProjectId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_ProjectPermissionProjectId");
              });
  
              builder.Entity&lt;Theme&gt;(entity =&gt;
              {
                  entity.HasKey(e =&gt; e.ThemeId);
  
                  entity.HasIndex(e =&gt; e.CreatedBy)
                      .HasName("fki_FK_ThemesCreatedBy");
  
                  entity.HasIndex(e =&gt; e.ProjectId)
                      .HasName("fki_FK_ThemesProjectId");
  
                  entity.HasOne(d =&gt; d.AspNetUser)
                      .WithMany(p =&gt; p.Themes)
                      .HasForeignKey(d =&gt; d.CreatedBy)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_ThemesCreatedBy");
  
                  entity.HasOne(d =&gt; d.Project)
                      .WithMany(p =&gt; p.Themes)
                      .HasForeignKey(d =&gt; d.ProjectId)
                      .OnDelete(DeleteBehavior.ClientSetNull)
                      .HasConstraintName("FK_ThemesProjectId");
              });
              base.OnModelCreating(builder);
              // Customize the ASP.NET Identity model and override the defaults if needed.
              // For example, you can rename the ASP.NET Identity table names and more.
              // Add your customizations after calling base.OnModelCreating(builder);
          }
      }
  </code></pre>