如何解决当基类型为 IdentittyUser 时如何映射鉴别器列?
我自定义了 IdentityUser
并将其命名为 User
。现在将 User
扩展为 Doctor
和 Secretary
。
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 会创建一个 Discriminator 作为 shadow 属性,但我想将此 Discriminator 映射到 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'.
为什么会出现这个错误?这是我走的好方法吗?
你知道如何建立不同类型的用户吗?
解决方法
我刚刚改变了
MyDbContext : IdentityDbContext {...}
到
MyDbContext : IdentityDbContext<User> {...}
问题解决了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。