title: Entity Framework: Cannot be configured on 'xxx' class because it is a derived type
body: <p>I got this error when try add "AppUserRoles" table by myself to access UserRoles from User, for example:</p>
  
  <pre><code>from u in _userManager.Users.Where(x =&gt; x.UserRoles "contain" "some codition here")
  </code></pre>
  
  <p>And I got this error:</p>
  
  <blockquote>
    <p>A key cannot be configured on 'AppUserRoles' because it is a derived type. The key must be configured on the root type 'IdentityUserRole'. If you did not intend for 'IdentityUserRole' to be included in the model, ensure that it is not included in a DbSet property on your context, referenced in a configuration call to ModelBuilder, or referenced from a navigation property on a type that is included in the model.</p>
  </blockquote>
  
  <p>This is my previous code, It is run ok:</p>
  
  <pre><code>builder.Entity&lt;IdentityUserRole&lt;Guid&gt;&gt;().ToTable("AppUserRoles")
  .HasKey(x =&gt; new { x.RoleId, x.UserId });
  </code></pre>
  
  <p>--->And I change to this:</p>
  
  <p>My user class</p>
  
  <pre><code>[Table("AppUsers")]
  public class AppUser : IdentityUser&lt;Guid&gt;, IDateTracking, ISwitchable
  {
      public virtual ICollection&lt;AppUserRoles&gt; UserRoles { get; set; }
  }
  </code></pre>
  
  <p>My role class</p>
  
  <pre><code>[Table("AppRoles")]
  public class AppRole : IdentityRole&lt;Guid&gt;
  {
      public virtual ICollection&lt;AppUserRoles&gt; UserRoles { get; set; }
  }
  </code></pre>
  
  <p>My appUserRole class:</p>
  
  <pre><code>[Table("AppUserRoles")]
  public class AppUserRoles : IdentityUserRole&lt;Guid&gt;
  {
  
      public virtual AppUser User { get; set; }
      public virtual AppRole Role { get; set; }
  }
  </code></pre>
  
  <p>My DbContextClass</p>
  
  <pre><code>public class AppDbContext : IdentityDbContext&lt;AppUser, AppRole, Guid&gt;
  {
      public AppDbContext(DbContextOptions options) : base(options)
      {
      }
      public DbSet&lt;AppUser&gt; AppUsers { get; set; }
      public DbSet&lt;AppRole&gt; AppRoles { get; set; }
      public DbSet&lt;AppUserRoles&gt; AppUserRoles { get; set; }
  
      protected override void OnModelCreating(ModelBuilder builder)
      {
          #region Identity Config
  
          builder.Entity&lt;IdentityUserClaim&lt;Guid&gt;&gt;().ToTable("AppUserClaims").HasKey(x =&gt; x.Id);
  
          builder.Entity&lt;IdentityUserLogin&lt;Guid&gt;&gt;().ToTable("AppUserLogins").HasKey(x =&gt; x.UserId);
  
          builder.Entity&lt;AppUserRoles&gt;(userRole =&gt;
          {
              userRole.HasKey(ur =&gt; new { ur.UserId, ur.RoleId });
  
              userRole.HasOne(ur =&gt; ur.Role)
                  .WithMany(r =&gt; r.UserRoles)
                  .HasForeignKey(ur =&gt; ur.RoleId)
                  .IsRequired();
  
              userRole.HasOne(ur =&gt; ur.User)
                  .WithMany(r =&gt; r.UserRoles)
                  .HasForeignKey(ur =&gt; ur.UserId)
                  .IsRequired();
          });
  
          builder.Entity&lt;IdentityUserToken&lt;Guid&gt;&gt;().ToTable("AppUserTokens")
             .HasKey(x =&gt; new { x.UserId });
  
          #endregion Identity Config
  
      }
  
      public override int SaveChanges()
      {
          var modified = ChangeTracker.Entries().Where(e =&gt; e.State == EntityState.Modified || e.State == EntityState.Added);
  
          foreach (EntityEntry item in modified)
          {
              var changedOrAddedItem = item.Entity as IDateTracking;
              if (changedOrAddedItem != null)
              {
                  if (item.State == EntityState.Added)
                  {
                      changedOrAddedItem.DateCreated = DateTime.Now;
                  }
                  changedOrAddedItem.DateModified = DateTime.Now;
              }
          }
          return base.SaveChanges();
      }
  }
  
  public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory&lt;AppDbContext&gt;
  {
      public AppDbContext CreateDbContext(string[] args)
      {
          IConfiguration configuration = new ConfigurationBuilder()
              .SetBasePath(Directory.GetCurrentDirectory())
              .AddJsonFile("appsettings.json").Build();
          var builder = new DbContextOptionsBuilder&lt;AppDbContext&gt;();
          var connectionString = configuration.GetConnectionString("DefaultConnection");
          builder.UseSqlServer(connectionString);
          return new AppDbContext(builder.Options);
      }
  }
  </code></pre>
  
  <p>This is my startup file:</p>
  
  <pre><code>services.AddDbContext&lt;AppDbContext&gt;(options =&gt;
  
  options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
              o =&gt; o.MigrationsAssembly("YayoiApp.Data.EF")), 
              ServiceLifetime.Scoped);
  </code></pre>
  
  <p>Please give me some advise, I already  have researched to much, but not found a solution. Thanks.</p>