Custom RoleProvider: Can't insert record in UsersInRole Table

Posted by mahr.g.mohyuddin on Stack Overflow See other posts from Stack Overflow or by mahr.g.mohyuddin
Published on 2009-05-16T08:00:53Z Indexed on 2010/04/10 3:33 UTC
Read the original article Hit count: 555

Hi, I have implemented a LINQ to SQL based RoleProvider, when I assign role to a user following exception is thrown while AddUsersToRoles method is called. I have defined a composite primary key userid & roleId on this table, it still throwing this exception:

Can't perform Create, Update or Delete operations on 'Table(UsersInRole)' because it has no primary key.

My LinQ to SQL implementation of AddUsersToRoles method is as follows. It breaks at db.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 will be appreciated. Thanks.

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about ASP.NET