gpt4 book ai didi

c# - EntityFrameworkCore 身份的继承问题

转载 作者:行者123 更新时间:2023-11-30 12:56:20 24 4
gpt4 key购买 nike

我最近决定在我使用 ASP.NET Core MVC 开发的网站上实现 ASP.NET Identity 功能。

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

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 没有从接口(interface) IdentityUser 继承,我不太清楚为什么会这样?显然我的类 User 确实继承自 IdentityUser

最佳答案

代替

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

你需要放

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

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

services.AddIdentity<User, IdentityRole>();

关于c# - EntityFrameworkCore 身份的继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41915994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com