【发布时间】:2017-01-08 17:24:19
【问题描述】:
我正在尝试更改身份用户上默认字符串的 ID 类型。 尝试在配置服务方法中注册身份服务时出现异常。
这就是我在 Startup.ConfigureServices 方法中尝试做的事情。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<UserContext>(options =>
options.UseSqlServer(config.GetConnectionString("DefaultConnection")));
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<UserContext>();
//.AddDefaultTokenProviders();
services.AddDbContext<MusicContext>(options =>
options.UseSqlServer(config.GetConnectionString("DefaultConnection")));
services.Configure<IdentityOptions>(options =>
{
new IdentityOptionConfiguration(options);
});
services.AddMvc();
}
这是我继承 IdentityUser 的类。这是我试图覆盖字符串类型的地方。我希望它是一个 int 甚至是一个 guid。我还不确定哪个,但我现在要使用 int。
public class User : IdentityUser<int>
{
public int ID { get; set; }
}
这是我尝试使用的角色。大多数情况下,我可以接受默认角色。
public class Role : IdentityRole
{
}
现在我得到的异常如下......
System.Private.CoreLib.ni.dll 但未在用户代码中处理
附加信息:GenericArguments[0], 'MusicianProject.Services.User',在 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`4[TUser,TRole,TContext,TKey]' 违反了“TUser”类型的约束。
根据错误的措辞,我相信我试图更改类型很疯狂。我在这里错过了什么?
【问题讨论】:
-
示例 here 使用 Guid 进行 PK
-
@tmg:你应该为它发起一次近距离投票,所以它会被清楚地标记为重复,并且未来的用户会被指向它
-
@Tseng 你是对的
标签: c# asp.net-core ef-code-first asp.net-identity