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.

0


a source to share


1 answer


LINQ to SQL doesn't support many of many ... there must be two foreign key columns in the join table PLUS 1 primary key column with an attribute IDENTITY

.



0


a source







All Articles