EF Core - 两个表中的 IdentityUser 映射

EF Core - IdentityUser mapping in two tables

我在我的 Identity Server 项目中实现了 Identity,但在尝试将 IdentityUser class 映射到我现有的数据库配置时遇到问题。

主要问题是 IdentityUser 属性在我的配置中被分成两个 table(主要是密码在另一个 table 中)。

我尝试相应地创建我的 classes:

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; }
}

我尝试了所有我能想到的配置,但无法将 IdentityUser 属性(如 PasswordHash 属性)映射到另一个 table。

我的数据库上下文,至于现在,如下:

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");
});

使用 ef core 查询时,我试图在 UserMapping class 中覆盖的属性链接到 User class 除了 AccessFailedCount (我不知道为什么)

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]

有什么办法可以映射吗?

希望我说得够清楚,谢谢你的帮助!

所以我真的做到了!

诀窍是忽略我想事先从基类覆盖的属性,如下所示:

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