C# ASP.NET核心2-实体类型';IdentityUserRole<;int>';使用单个键属性定义,但向';DbSet.Find';方法
尝试将新用户添加到角色时出现异常: ArgumentException:实体类型“IdentityUserRole”是使用单个键属性定义的,但向“DbSet.Find”方法传递了2个值 这是扔在下面的线C# ASP.NET核心2-实体类型';IdentityUserRole<;int>';使用单个键属性定义,但向';DbSet.Find';方法,c#,asp.net-core-2.0,C#,Asp.net Core 2.0,尝试将新用户添加到角色时出现异常: ArgumentException:实体类型“IdentityUserRole”是使用单个键属性定义的,但向“DbSet.Find”方法传递了2个值 这是扔在下面的线 userManager.AddToRoleAsync(user, "Admin"); 这是Startup类中的方法: private async Task CreateRoles(IServiceProvider serviceProvider) { //adding custom r
userManager.AddToRoleAsync(user, "Admin");
这是Startup类中的方法:
private async Task CreateRoles(IServiceProvider serviceProvider) {
//adding custom roles
var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole<int>> > ();
var userManager = serviceProvider.GetRequiredService<UserManager<UserEntity>>();
string[] roleNames = { "Admin", "Manager", "User" };
IdentityResult roleResult;
foreach (var roleName in roleNames) {
//creating the roles and seeding them to the database
var roleExist = await roleManager.RoleExistsAsync(roleName);
if (!roleExist) {
roleResult = await roleManager.CreateAsync(
new IdentityRole<int> {
Name = roleName,
NormalizedName = roleName.ToUpper()
});
}
}
//creating a super user who could maintain the web app
var poweruser = new UserEntity {
UserName = Configuration["AdminUserName"],
Email = Configuration["AdminUserEmail"],
Password = Configuration["AdminUserPassword"],
ResidendceId = int.Parse(Configuration["AdminUserCountryId"])
};
string UserPassword = Configuration["AdminUserPassword"];
UserEntity user = await userManager.FindByEmailAsync(Configuration["AdminUserEmail"]);
if (user == null) {
var createPowerUser = await userManager.CreateAsync(poweruser);
user = poweruser;
}
var adminRoleList = await userManager.GetUsersInRoleAsync("Admin");
if (user != null && !adminRoleList.Any(u => u.Email == user.Email)) {
//here we tie the new user to the "Admin" role
await userManager.AddToRoleAsync(user, "Admin");
}
}
专用异步任务CreateRoles(IServiceProvider服务提供者){
//添加自定义角色
var rolemager=serviceProvider.GetRequiredService();
var userManager=serviceProvider.GetRequiredService();
字符串[]roleNames={“Admin”、“Manager”、“User”};
识别结果角色结果;
foreach(roleName中的变量roleName){
//创建角色并将其植入数据库
var roleExist=wait rolemager.RoleExistsAsync(roleName);
如果(!roleExist){
roleResult=等待roleManager.CreateAsync(
新身份证{
Name=roleName,
NormalizedName=roleName.ToUpper()
});
}
}
//创建可以维护web应用程序的超级用户
var poweruser=新用户实体{
用户名=配置[“AdminUserName”],
电子邮件=配置[“AdminUserEmail”],
密码=配置[“AdminUserPassword”],
ResidendceId=int.Parse(配置[“AdminUserCountryId”])
};
字符串UserPassword=配置[“AdminUserPassword”];
UserEntity user=await userManager.findbyemailsync(配置[“AdminUserEmail”]);
if(user==null){
var createPowerUser=await userManager.CreateAsync(poweruser);
用户=超级用户;
}
var adminRoleList=await userManager.GetUsersInRoleAsync(“Admin”);
if(user!=null&&!adminRoleList.Any(u=>u.Email==user.Email)){
//这里我们将新用户绑定到“Admin”角色
等待userManager.AddToRoleAsync(用户,“Admin”);
}
}
有什么想法吗?
感谢您在DbContext类中添加以下内容
builder.Entity<IdentityUserRole<int>>(b =>
{
b.HasKey(i => new {i.UserId, i.RoleId});
});
builder.Entity(b=>
{
b、 HasKey(i=>new{i.UserId,i.RoleId});
});
这应该会让DbFind找到它正在寻找的两个键。如果您使用的是
int
,我相信您需要传递int
,而不是用户对象:AddToRoleAsync(user.Id,“Admin”)