EF Core - IdentityUser mapping in two tables

250 Views Asked by At

I implemented Identity in my Identity Server project and I'm facing a problem trying to map IdentityUser class to my already existing DB configuration.

The main problem is that IdentityUser properties are split into two tables in my configuration (mainly password is in another table).

I tried to create my classes accordingly:

public partial class User : IdentityUser<int>
{
    public override int Id { get => base.Id; set => base.Id = value; }
    public override string Email { get => base.Email; set => base.Email = value; }
    public string Telephone { get; set; }
    public override string NormalizedUserName { get => base.NormalizedUserName; set => base.NormalizedUserName = value; }
    public override string UserName { get => base.UserName; set => base.UserName = value; }
}

public class UserMapping : User
{
    public override string PasswordHash { get => base.PasswordHash; set => base.PasswordHash = value; }
    public override int AccessFailedCount { get => base.AccessFailedCount; set => base.AccessFailedCount = value; }
    public override bool EmailConfirmed { get => base.EmailConfirmed; set => base.EmailConfirmed = value; }
}

I tried every configuration I could think of but couldn't manage to map IdentityUser properties like the PasswordHash property to another table.

My db context, as for now, is as follow :

modelBuilder.Entity<UserMapping>(entity =>
{
    entity.ToTable("myOtherTable");

    entity.Property(e => e.Id).HasColumnName("UserId");
    entity.Property(e => e.PasswordHash).HasColumnName("Password");
    entity.Property(e => e.AccessFailedCount).HasColumnName("PasswordFailuresSinceLastSuccess");
    entity.Property(e => e.EmailConfirmed).HasColumnName("IsConfirmed");

    entity.HasOne<User>()
        .WithOne()
        .HasForeignKey<User>(e => e.Id);
});

modelBuilder.Entity<User>(entity =>
{
    entity.ToTable("User");

    entity.Property(e => e.Id).HasColumnName("IdUser");
    entity.Property(e => e.UserName).HasColumnName("Username");
    entity.Property(e => e.NormalizedUserName).HasColumnName("UpperUsername");

    entity.HasKey(e => e.Id)
        .HasName("PK_dbo.User");
});

When querying with ef core, the properties that I tried to override in UserMapping class are linked to the User class except AccessFailedCount (I have no clue why)

SELECT [User].[IdUser], 
       [User].[Email], 
       [User].[IsConfirmed], 
       [User].[UpperUsername], 
       [User].[Password], 
       [User].[Telephone], 
       [User].[Username], 
       [UserMapping].[PasswordFailuresSinceLastSuccess], 
       [UserMapping].[ConfirmationToken], 
       [UserMapping].[CreateDate], 
       [UserMapping].[LastPasswordFailureDate], 
       [UserMapping].[PasswordChangedDate], 
       [UserMapping].[PasswordSalt], 
       [UserMapping].[PasswordVerificationToken], 
       [UserMapping].[PasswordVerificationTokenExpirationDate], 
FROM [User] AS [User]
LEFT JOIN [myOtherTable] AS [UserMapping] ON [User].[IdUser] = [UserMapping].[IdUser]
WHERE [User].[Email] = @__email_0
ORDER BY [User].[IdUser]

Is there any way to map it?

Hope I was clear enough, thank you for your help!

1

There are 1 best solutions below

0
On BEST ANSWER

So I actually managed to do it !

The trick was to ignore the properties I wanted to override from the base classe before hand, like so :

modelBuilder.Entity<User>().Ignore(c => c.AccessFailedCount)
                           .Ignore(c => c.PasswordHash)
                           .Ignore(c => c.EmailConfirmed)