【问题标题】:How to Map Discriminator Column when base type is IdentittyUser?基本类型为 IdentityUser 时如何映射鉴别器列?
【发布时间】:2021-05-05 01:52:09
【问题描述】:

我自定义了IdentityUser,并将其命名为User。现在将User 扩展为DoctorSecretary

using Microsoft.AspNetCore.Identity;
//...

public class User : IdentityUser
{
    public string UserType {get; set;}
    //some props
}

public class Doctor : User
{
    //some props
}

public class Secretary : User
{
    //some props
}

//...

我知道 EF Core 将创建一个鉴别器作为影子属性,但我想将此鉴别器映射到 User.UserType 属性。所以这是我在 DbContext 上的配置:

builder.Entity<User>()
            .HasDiscriminator(u=>u.UserType)
            .HasValue<User>("user")
            .HasValue<Doctor>("doctor")
            .HasValue<Secretary>("secretary");

添加迁移时出现此错误:

The CLR property 'UserType' cannot be added to entity type 'IdentityUser' because it is declared on the CLR type 'User'.

为什么会发生这个错误?这是我的好方法吗?

你知道有什么合适的方法来培养不同类型的用户吗?

【问题讨论】:

  • 实体类型 错误消息中的“IdentityUser”表示有问题,因为IdentityUser 不能是实体。确保将自定义的User 类作为TUser 通用参数传递给基本身份相关类,包括数据库上下文,例如YourDbContext : IdentityDbContext&lt;User&gt;
  • @IvanStoev 你是对的。这是我的上下文MyDbContext : IdentityDbContext,我将其更改为MyDbContext : IdentityDbContext&lt;User&gt;,问题解决了。
  • @IvanStoev 留下您的评论作为回应,以便我将其作为适当的回应。

标签: c# asp.net-core entity-framework-core asp.net-core-identity


【解决方案1】:

我只是变了

MyDbContext : IdentityDbContext {...}

MyDbContext : IdentityDbContext<User> {...}

问题解决了。

【讨论】:

    猜你喜欢
    • 2018-01-18
    • 2013-05-20
    • 2017-07-31
    • 2015-11-04
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多