[EDIT by guardrex ... content moved here from #12372]
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-2.2#add-all-navigation-properties
After following this and attempting to execute the web application, an error is raised at base.OnModelCreating(modelBuilder);
System.InvalidOperationException
HResult=0x80131509
Message=A key cannot be configured on 'ApplicationUserClaim' because it is a derived type. The key must be configured on the root type 'IdentityUserClaim'. If you did not intend for 'IdentityUserClaim ' 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(IReadOnlyList
1 properties, ConfigurationSource configurationSource)
at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder1.HasKey(Expression
1 keyExpression)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserContext5.<>c.<OnModelCreating>b__20_1(EntityTypeBuilder
1 b)
at Microsoft.EntityFrameworkCore.ModelBuilder.Entity[TEntity](Action1 buildAction) at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserContext
5.OnModelCreating(ModelBuilder builder)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext`8.OnModelCreating(ModelBuilder builder)
at Identity.Data.IdentityDbContext.OnModelCreating(ModelBuilder modelBuilder) in C:\Users\devuser\SourceRepos\Identity\Data\IdentityDbContext.cs:line 24
The class it is complaining about is exactly as defined in the documentation. Also all other classes as per docs.
public class ApplicationUserClaim : IdentityUserClaim<string>
{
public virtual ApplicationUser User { get; set; }
}
<details>
<summary><strong>dotnet --info output</strong> or <strong>About VS info</strong></summary>
dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 2.2.103
Commit: 8edbc2570a
Runtime Environment:
OS Name: Windows
OS Version: 10.0.14393
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.103\
Host (useful for support):
Version: 2.2.1
Commit: 878dd11e62
.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.403 [C:\Program Files\dotnet\sdk]
2.1.500 [C:\Program Files\dotnet\sdk]
2.1.502 [C:\Program Files\dotnet\sdk]
2.1.503 [C:\Program Files\dotnet\sdk]
2.1.504 [C:\Program Files\dotnet\sdk]
2.1.505 [C:\Program Files\dotnet\sdk]
2.2.100 [C:\Program Files\dotnet\sdk]
2.2.103 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
</details>
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Hello @The-DevOps-Guy ... We just need one issue open for it. This is a little better for us to track because it has the topic metadata.
oh ... no worries ... I'll update it.
[EDIT] ok ..... moved it all to your comment. :+1:
[EDIT X2 :smile:] ... actually ... I'll just move it to the OP.
Hi, I have created a example project with this issue for you to reproduce.
To see, all you need to do is run the following command in the Package Manager Window.
Add-Migration Init -Context ApplicationDbContext
I am using VS 2017 Enterprise v.15.9.11
Solution file : https://drive.google.com/file/d/1z2MHCdZ8zQOS6lRuX7ZOR07pc2J7REn9/view?usp=sharing
Not sure if this is related, but I noticed the Microsoft.AspNetCore.App 2.2.0 includes...
Which in turn includes...
But the stand alone package Microsoft.AspNetCore.Identity.EntityFrameworkCore v2.2.0 includes...
Not sure why this is.
I hope this helps anyone else who finds this issue. I have fixed this. The issue was actually within the ApplicationUser class. While this might indicate the error raised is not reporting the underlying issue, the problem was the declarations for the navigation properties were typed with the Identity... rather than
the Application... derived types.
Before
public virtual ICollection<IdentityUserClaim<string>> Claims { get; set; }
public virtual ICollection<IdentityUserLogin<string>> Logins { get; set; }
public virtual ICollection<IdentityUserToken<string>> Tokens { get; set; }
After (fixed)
public virtual ICollection<ApplicationUserClaim> Claims { get; set; }
public virtual ICollection<ApplicationUserLogin> Logins { get; set; }
public virtual ICollection<ApplicationUserToken> Tokens { get; set; }
I have almost the same problem, but in my case, the collections are correct. Core 2.1, EF Core 2.1.1
System.InvalidOperationException: A key cannot be configured on 'ApplicationUserRole' 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.
Why is it, 5 minutes after posting 'it doesn't work', I get it working. Turns out I didn't have the correct DbContext, specifically, I hadn't specified ApplicationUserRole:
class ApplicationDbContext : IdentityDbContext
, ApplicationUserRole, IdentityUserLogin , IdentityRoleClaim , IdentityUserToken >
The :pray: server gods :pray: play with us mere mortals!
Most helpful comment
I hope this helps anyone else who finds this issue. I have fixed this. The issue was actually within the ApplicationUser class. While this might indicate the error raised is not reporting the underlying issue, the problem was the declarations for the navigation properties were typed with the Identity... rather than
the Application... derived types.
Before
public virtual ICollection<IdentityUserClaim<string>> Claims { get; set; }
public virtual ICollection<IdentityUserLogin<string>> Logins { get; set; }
public virtual ICollection<IdentityUserToken<string>> Tokens { get; set; }
After (fixed)
public virtual ICollection<ApplicationUserClaim> Claims { get; set; }
public virtual ICollection<ApplicationUserLogin> Logins { get; set; }
public virtual ICollection<ApplicationUserToken> Tokens { get; set; }