Search code examples
entity-frameworkasp.net-coreasp.net-core-mvc

A key cannot be configured on 'ApplicationUser' because it is a derived type, but there is no key configuration on ApplicationUser


I'm trying to customize IdentityUser using ApplicationUser, I followed the steps in microsoft article, but when I run the application, I get this error on method base.OnModelCreating(modelBuilder);:

System.InvalidOperationException HResult=0x80131509 Message=A key cannot be configured on 'ApplicationUser' because it is a derived type. The key must be configured on the root type 'IdentityUser'. If you did not intend for 'IdentityUser' 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.
Source=Microsoft.EntityFrameworkCore StackTrace: at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.SetPrimaryKey(IReadOnlyList1 properties, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList1 properties, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList1 clrProperties, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder1.HasKey(Expression1 keyExpression) at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserContext5.<>c__DisplayClass20_0.b__0(EntityTypeBuilder1 b) at Microsoft.EntityFrameworkCore.ModelBuilder.Entity[TEntity](Action1 buildAction) at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserContext5.OnModelCreating(ModelBuilder builder) at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext8.OnModelCreating(ModelBuilder builder) at Assistente_de_Estagio.Data.ApplicationDbContext.OnModelCreating(ModelBuilder modelBuilder) in C:\Users\gui\source\repos\Assistente De Estágio\Assistente de Estagio\Data\ApplicationDbContext.cs:line 40
at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context) at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context) at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.b__1() at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)

Context code:

public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
    {
    }

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
    public virtual DbSet<Curso> Curso { get; set; }
    public virtual DbSet<Documento> Documento { get; set; }
    public virtual DbSet<OpcaoRequisito> OpcaoRequisito { get; set; }
    public virtual DbSet<ProgressoDoUsuario> ProgressoDoUsuario { get; set; }
    public virtual DbSet<Requisito> Requisito { get; set; }
    public virtual DbSet<RequisitoDeDocumento> RequisitoDeDocumento { get; set; }
    public virtual DbSet<RequisitoDeUsuario> RequisitoDeUsuario { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }

ApplicationUser.cs

public class ApplicationUser : IdentityUser
{

}

Solution

  • You need to replace all places in code where you use IdentityUser to ApplicationUser.

    For example in scaffolded Identity/Pages/Account/Register.cshtml.cs you need to do these changes:

    private readonly SignInManager<IdentityUser> _signInManager;
    private readonly UserManager<IdentityUser> _userManager;
    

    To

    private readonly SignInManager<ApplicationUser> _signInManager;
    private readonly UserManager<ApplicationUser> _userManager;
    

    I faced the same issue. After I replaced all usage IdentityUser to ApplicationUser in code the Error has gone.