I'm using ASP.NET Core Identity along with Entity Framework Core on .NET 5, using a code first approach with a postgresql database.
I am trying to extend the identity classes like this
public class User : IdentityUser<int>
{
public ICollection<UserLogin> Logins { get; set; }
}
public class UserLogin : IdentityUserLogin<int>
{
public User User { get; set; }
}
public sealed class AppDbContext : IdentityDbContext<User, Role, int, UserClaim,UserRole, UserLogin, RoleClaim, UserToken>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
}
However, when I run migrations, the tables that it produces look like this -
Notice that an extra column UserId1 has been created.
I would expect it to recognize that the UserLogin.User navigation's Id should be correspond to the IdentityUserLogin.UserId property (according to the conventions laid out by MS here: https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key).
I have also tried overriding IdentityUserLogin.UserId but without luck:
public class UserLogin : IdentityUserLogin<int>
{
public User User { get; set; }
public override int UserId { get; set; }
}
Any help would be greatly appreciated. I know that I could probably do a workaround like specifying the mapping manually for these tables, but Id rather figure out how to use the migration tool along with my extended identity classes.

Your
UserLoginmodel has aUserIdproperty inherited fromIdentityUserLogin.According to the default
Identitydata model, there is already a one-to-many relationship betweenIdentityUserandIdentityUserLogin, and theUserIdproperty inIdentityUserLoginserves as the foreign key for this relationship.The
Identitydatabase is generated based on the default data model and any extension you make to those models. Therefore, theAspNetUserLoginstable already has aUserIdcolumn serving as a foreign key toAspNetUserstable.It is up to you whether or not you want navigation properties for the already existing relationships.
Now, you have added navigation properties, but you didn't tell EF that you want these navigations to base on the existing relationship. Therefore, EF is considering this as a new one-to-many relation and creating a new foreign key column
UserId1inAspNetUserLoginstable.In the
OnModelCreatingmethod just tell EF that your added navigations should base on the already existing foreign key -