代码之家  ›  专栏  ›  技术社区  ›  Junior

如何在Asp.NET Core 2.2中将用户id的类型从字符串更改为int?

  •  1
  • Junior  · 技术社区  · 7 年前

    现在,我决定改变主意 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);
    }
    

    我还尝试在Startsup文件中添加以下代码

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

    enter image description here

    3 回复  |  直到 6 年前
        1
  •  1
  •   Kirk Larkin    7 年前

    呼吁 AddDefaultIdentity<TUser>() 登记册 UserManager<TUser> TUser 类型,您将一直使用 IdentityUser UserManager<IdentityUser> .

    识别码 使用 User ,您现在可以注册了 UserManager<User> 而不是 . 问题中的错误消息显示项目中的部分视图正在尝试接收的实例 用户管理器<IdentityUser> 用户管理器<用户>

    @inject UserManager<IdentityUser> UserManager
    

    -变成-

    @inject UserManager<User> UserManager
    

    用户管理器<IdentityUser> 这也需要改变。

        2
  •  0
  •   Derviş Kayımbaşıoğlu    7 年前

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

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

    dotnet ef migrations add InitialCreate 
    

    Add-Migration InitialCreate
    

    用于更新数据库

    dotnet ef database update
    

    Update-Database
    

    Identity model customization in ASP.NET Core

        3
  •  0
  •   user10743690 user10743690    7 年前

    你更新数据库了吗?

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

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

    这是不必要的。

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

    把它拿走。