gpt4 book ai didi

asp.net-core - 在 ASP.NET Core 3.0 中向 AspNetUserRoles 表添加自定义列 - ASP.NET Core IDENTITY

转载 作者:行者123 更新时间:2023-12-03 14:15:36 30 4
gpt4 key购买 nike

我通过迁移创建了表,以使用 ASP.NET Core 身份管理我的 ASP.NET Core 项目的安全性。

ASP.NET Core identity tables

我需要能够在 AspNetUserRoles 中插入一个附加字段表,以便以不同的方式管理已与 ADMIN 角色关联的用户。

例如,我有与美国国家相关联的 ADMIN 和与另一个巴西相关联的 ADMIN 或与 ITALY 相关联的 ADMIN 国家,但他们的实际角色始终是 ADMIN。

实际上,当我将用户与 ADMIN 角色相关联时,它只是要添加的另一个属性。

ASP.NET Core identity tables with custom column in AspNetUserRoles table

我试图扩展 IdentityUserRole以这种方式上课:

public class AspNetUserRoles : IdentityUserRole<string>
{
public string Nation { get; set; }
}

但是当我在迁移类中启动迁移脚本(代码优先)时,没有检测到任何变化!
PM> Add-Migration aspnetuserrolesupdate -Context ApplicationDbContext

这是结果:
public partial class aspnetuserrolesupdate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}

protected override void Down(MigrationBuilder migrationBuilder)
{
}
}

正如您从产品代码中看到的那样,没有发现任何更改......

在我的项目中,我已经在 AspNetUsers 中插入了字段。和 AspNetRoles表以这种方式成功,但是如何将第三列添加到 AspNetUserRoles table ?

谢谢你们

最佳答案

我是这样解决的:

我修改了注入(inject)的类,将它们全部添加以使 ApplicationUserRole 可用

public class ApplicationDbContext: // IdentityDbContext <ApplicationUser, ApplicationRole, string>
IdentityDbContext <
ApplicationUser, ApplicationRole, string,
IdentityUserClaim <string>, ApplicationUserRole, IdentityUserLogin <string>,
IdentityRoleClaim <string>, IdentityUserToken <string>>
{
public ApplicationDbContext (DbContextOptions <ApplicationDbContext> options)
: base (options)
{
}
}

关于asp.net-core - 在 ASP.NET Core 3.0 中向 AspNetUserRoles 表添加自定义列 - ASP.NET Core IDENTITY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60952643/

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