如何在 Asp.NET Core 2.2 中将用户 ID 的类型从 string 更改为 int?

How to change the type of the user's id from string to int in Asp.NET Core 2.2?

我在 Asp.Net Core 2.2 的顶部有一个使用 C# 编写的应用程序。该应用程序是在 Core 2.1 的基础上编写的,但后来升级到 Asp.net Core 2.2。

现在,我决定将 User 模型的 Id 类型从字符串更改为整数。

为了覆盖 string/default 类型,我创建了一个 User class 像这样

public class User : IdentityUser<int>
{
}

然后我创建了一个 Role class 像这样

public class Role : IdentityRole<int>
{
}

最后,我像这样更新了 ApplicationDbContext class

public class ApplicationDbContext : IdentityDbContext<User, Role, int>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
}

然后我删除了所有以 AspNet 开头的数据库 table 和 _Migrations table.

现在,每次我 运行 我的应用程序都会收到以下错误

InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' has been registered.

这是我的Startup.csclass

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));


        services.AddDefaultIdentity<User>().AddEntityFrameworkStores<ApplicationDbContext>()
          .AddDefaultTokenProviders();

    services.AddAuthentication()
            .AddFacebook(facebookOptions =>
    {
        facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
        facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
    });

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

我也尝试在我的启动文件中添加以下代码

services.AddScoped<UserManager<User>>();

这是堆栈跟踪的屏幕截图

您需要添加迁移和更新数据库来创建模式:

  1. 在代码中定义或更新数据模型。 完成
  2. 添加迁移以将此模型转换为可应用于数据库的更改。
  3. 检查迁移是否正确代表了您的意图。
  4. 应用迁移更新数据库以与模型同步。
  5. 重复步骤 1 到 4 以进一步优化模型并保持数据库同步。

用于创建迁移

dotnet ef migrations add InitialCreate 

Add-Migration InitialCreate

用于更新数据库

dotnet ef database update

Update-Database

您可以从 Identity model customization in ASP.NET Core

获取有关模式自定义和迁移的更多详细信息

你更新数据库了吗?

如果这样做了,那么就改变这个

services.AddDefaultIdentity<User>().AddEntityFrameworkStores<ApplicationDbContext>()
      .AddDefaultTokenProviders();

services.AddIdentity<User, Role>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultUI()
                .AddDefaultTokenProviders();

这是不必要的。

services.AddScoped<UserManager<User>>();

只需删除它。

AddDefaultIdentity<TUser>() 的调用向依赖注入容器注册 UserManager<TUser>。在您创建自己的自定义 TUser 类型之前,您会在此处使用 IdentityUser,这会注册 UserManager<IdentityUser>.

从使用 IdentityUser 更改为使用 User,您现在注册 UserManager<User> 而不是 UserManager<IdentityUser>。您问题中的错误消息显示您项目中的部分视图正在尝试从 DI 容器接收 UserManager<IdentityUser> 的实例,但由于您的更改,它应该请求 UserManager<User> 的实例。这意味着在部分视图中需要进行以下更改:

@inject UserManager<IdentityUser> UserManager

-变成-

@inject UserManager<User> UserManager

这将修复您显示的特定错误,但您还需要浏览项目的其余部分,看看是否还有任何其他对 UserManager<IdentityUser> 的引用也需要更改.