Custom RoleProvider: unable to insert record into UsersInRole table
I have implemented LINQ to SQL based RoleProvider
when I assign a role to a user, the following except is generated when the method is called AddUsersToRoles
. I defined a composite primary key userid
and roleId
in this table, it still throws this exception:
Unable to create, update or delete operations on "Table (UsersInRole)" because it does not have a primary key.
My implementation of the LinQ to SQL method AddUsersToRoles
is as follows. It breaks down intodb.UsersInRoles.InsertOnSubmit(userInRole);
using (RussarmsDataContext db = new RussarmsDataContext())
{
List<UsersInRole> usersInRole = new List<UsersInRole>();
foreach (string username in usernames)
{
foreach (string rolename in rolenames)
{
UsersInRole userInRole = new UsersInRole();
object userId = ProvidersUtility.GetUserIdByUserName(username,applicationName);
object roleId = ProvidersUtility.GetRoleIdByRoleName(rolename,applicationName);
if (userId != null && roleId != null)
{
userInRole.UserId = (Guid)userId;
userInRole.RoleId = (Guid)roleId;
db.UsersInRoles.InsertOnSubmit(userInRole);
}
}
}
try
{
// db.UsersInRoles.InsertAllOnSubmit(usersInRole);
db.SubmitChanges();
}
catch (ChangeConflictException)
{
db.ChangeConflicts.ResolveAll(RefreshMode.OverwriteCurrentValues);
db.SubmitChanges();
}
}
Any help would be appreciated. Thanks.