【问题标题】: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 中将用户 id 的类型从 string 更改为 int?
【发布时间】:2018-12-20 21:57:34
【问题描述】:

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

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

为了覆盖字符串/默认类型,我像这样创建了一个User 类

public class User : IdentityUser<int>
{
}

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

public class Role : IdentityRole<int>
{
}

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

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

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

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

InvalidOperationException:没有服务类型 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' 已注册。

这是我的 Startup.cs 类

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>>();

这是堆栈跟踪的屏幕截图

【问题讨论】:

  • @AhmedGhoniem 我觉得我在这里做同样的事情。唯一不同的是我没有覆盖IdentityUserRole、IdentityUserClaim、IdentityUserLogin,这实际上并不需要。我的AppIdentityDbContext 类扩展的IdentityDbContext&lt;TUser, TRole, TKey&gt; 继承了IdentityDbContext&lt;TUser, TRole, TKey, IdentityUserClaim&lt;TKey&gt;, IdentityUserRole&lt;TKey&gt;, IdentityUserLogin&lt;TKey&gt;, IdentityRoleClaim&lt;TKey&gt;, IdentityUserToken&lt;TKey&gt;&gt;,它基本上将int 传递给这些ysing TKey 泛型
  • 作为 IdentityUserRole 表中的示例,您将拥有 UserID、RoleId 列,IdentityUserRole 中 userid 的默认类型为 string,因此您必须将 userid 覆盖为 @ 987654349@

标签: c# asp.net-core asp.net-core-mvc asp.net-core-identity


【解决方案1】:

对AddDefaultIdentity&lt;TUser&gt;() 的调用将UserManager&lt;TUser&gt; 注册到依赖注入容器中。在您创建自己的自定义 TUser 类型之前,您会在这里使用 IdentityUser,它会注册 UserManager&lt;IdentityUser&gt;。

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

@inject UserManager<IdentityUser> UserManager

-变成-

@inject UserManager<User> UserManager

这将修复您显示的特定错误,但您还需要扫描项目的其余部分以查看是否还有其他对 UserManager&lt;IdentityUser&gt; 的引用也需要更改。

【讨论】:

    【解决方案2】:

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

    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获取有关架构自定义和迁移的更多详细信息

    【讨论】:

    • 我尝试了这个解决方案,当它生成迁移 ID 时仍然是字符串而不是 int。还有什么我需要做的。使用 vs2019 Net.Core.Identity 2.2
    【解决方案3】:

    你更新你的数据库了吗?

    如果这样做了,那么只需更改它

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

    到

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

    这是不必要的。

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

    删除它。

    【讨论】:

      猜你喜欢
      • 2019-05-13
      • 1970-01-01
      • 2014-01-31
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多