加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – EntityFrameworkCore标识的继承问题

发布时间:2020-12-15 21:04:57 所属栏目:百科 来源:网络整理
导读:我最近决定在我正在ASP.NET Core MVC中开发的网站上实现ASP.NET身份功能. 让我们快速浏览一下主题中的表和类: public class User : IdentityUser{ public string FirstName { get; set; } public stirng LastName { get; set; } public int CountryId { get
我最近决定在我正在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>();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读