微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – EntityFrameworkCore标识的继承问题

我最近决定在我正在ASP.NET Core MVC中开发的网站上实现ASP.NET身份功能.

让我们快速浏览一下主题中的表和类:

public class User : IdentityUser
{
    public string FirstName { get; set; }
    public stirng LastName { get; set; }
    public int CountryId { get; set; }

    public Country Country { get; set; }
}

public class Country 
{
    public int Id { get; set; }
    public string ISO { get; set; }
    public string Name { get; set; }
}

Country类有点不相关,但是如果你想要User类的引用.

通过此设置,我已经配置了我的数据库上下文:

public class DatabaseContext : IdentityDbContext<User>
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<IdentityUser>.ToTable("User");
        builder.Entity<IndentityRole>.ToTable("Roles");
        builder.Entity<IdentityUserRole<string>>.ToTable("UserRoles");
        builder.Entity<IdentityUserClaim<string>>.ToTable("UserClaims");
        builder.Entity<IdentityUserLogin<string>>.ToTable("UserLogins");
    }
}

我正在使用自定义表(重命名)并像这样映射它们.

问题

现在,每当我运行我的项目时,在我正在进行的第一次数据库调用时,我收到以下错误

The entity type ‘User’ should derive from ‘IdentityUser’ to reflect the hierarchy of the corresponding CLR types.

对我来说,这个错误说我的类User没有从IdentityUser接口继承,我不太清楚为什么会这样?显然我的用户类继承自IdentityUser?

解决方法

代替

builder.Entity<IdentityUser>.ToTable("User");

你需要把

builder.Entity<User>.ToTable("Users");

添加身份服务的Startup类中,您需要执行此操作

services.AddIdentity<User,IdentityRole>();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐