我正在使用标准WebSecurity
功能创建一个管理角色 - 管理页面。
我收到此错误
无法插入外键值,因为对应的主键值不存在。[外键约束名 = fk_UserId]
在以下代码行:
if(!Roles.IsUserInRole(userNames[0], roleNames[0])){
Roles.AddUsersToRoles(userNames, roleNames);
}
我的理解是各个表应该与一userId
列链接。他们是。我正在使用 WebMatrix 2 和 Razor 框架进行开发。
您的 SQL Mastermind 有什么想法或解决方案吗?非常感谢!
下面是我如何处理的代码片段!
@{
var roleName = "";
string[] userNames = new string[1];
string[] roleNames = new string[1];
var db = Database.Open("ResearchA");
var selectQueryString = "SELECT UserId, username FROM [usernames]";
if(IsPost){
// Create new role
if(!Request["buttonCreateRole"].IsEmpty()){
roleName=Request["textRoleName"];
if(!Roles.RoleExists(roleName) && !roleName.IsEmpty()){
Roles.CreateRole(roleName);
}
}
// Delete role
if(!Request["buttonDeleteRole"].IsEmpty()){
roleName=Request["textRoleName"];
if(Roles.GetUsersInRole(roleName).Length == 0 && !roleName.IsEmpty()){
Roles.DeleteRole(roleName, true);
}
}
// Add user to role
if(!Request["buttonAddUserToRole"].IsEmpty()){
userNames[0] = Request["selectUserName"];
roleNames[0] = Request["selectRoleName"];
if(!Roles.IsUserInRole(userNames[0], roleNames[0])){
Roles.AddUsersToRoles(userNames, roleNames);
}
}
// Delete user from role
if(!Request["buttonDeleteUserFromRole"].IsEmpty()){
userNames[0] = Request["selectUserName"];
roleNames[0] = Request["selectRoleName"];
if(Roles.IsUserInRole(userNames[0], roleNames[0])){
Roles.RemoveUsersFromRoles(userNames, roleNames);
}
}
}
}