gpt4 book ai didi

c# - 更改asp.net core 2.2 IdentityUser 的Id类型

转载 作者:行者123 更新时间:2023-12-03 16:53:29 24 4
gpt4 key购买 nike

我是 dot-net core 2.x 的新手,所以......

我想将 asp.net core 2.2 IdentityUser 中的 Id 类型从 string 更改为 int。

我通过谷歌(以及 stackoverflow 搜索工具)找到的所有示例都为我提供了 asp.net core 2.0 的示例,当您搭建 Identity(2.2 未提供)时,它提供了一个 ApplicationUser。

所以,我不知所措..我尝试的第一件事(我寄予厚望)是:

services.AddDefaultIdentity<IdentityUser<int>>()
.AddRoles<IdentityRole>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();

但是,当我尝试 Add-Migration InitialCreate -Context ApplicationDbContext 时出现以下错误:

An error occurred while accessing the IWebHost on class 'Program'. Continuing without the application service provider. Error: GenericArguments[0], 'Microsoft.AspNetCore.Identity.IdentityUser`1[System.Int32]', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]' violates the constraint of type 'TUser'



想法?想法?我可以阅读的文档?

最佳答案

要将 IdentityUser key 类型从 string 更改为 int,您还需要更改 IdentityDbContextIdentityDbContext<IdentityUser<int>,IdentityRole<int>,int> .

  • 启动文件
    services.AddDefaultIdentity<IdentityUser<int>>()
    .AddDefaultUI(UIFramework.Bootstrap4)
    .AddEntityFrameworkStores<ApplicationDbContext>();
  • ApplicationDbContext
    public class ApplicationDbContext : IdentityDbContext<IdentityUser<int>,IdentityRole<int>,int>
    {
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    : base(options)
    {
    }
    }
  • 删除 Migrations文件夹并删除现有数据库
  • 运行命令添加和更新数据库
  • 关于c# - 更改asp.net core 2.2 IdentityUser 的Id类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53749631/

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