【发布时间】: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<TUser, TRole, TKey>继承了IdentityDbContext<TUser, TRole, TKey, IdentityUserClaim<TKey>, IdentityUserRole<TKey>, IdentityUserLogin<TKey>, IdentityRoleClaim<TKey>, IdentityUserToken<TKey>>,它基本上将int传递给这些ysingTKey泛型 -
作为 IdentityUserRole 表中的示例,您将拥有
UserID、RoleId列,IdentityUserRole中userid的默认类型为string,因此您必须将userid覆盖为 @ 987654349@
标签: c# asp.net-core asp.net-core-mvc asp.net-core-identity