Search code examples
sqlsql-serverasp.net-mvcasp.net-identity

Violation of PRIMARY KEY constraint 'PK_dbo.AspNetUserRoles'. Cannot insert duplicate key in object 'dbo.AspNetUserRoles'


I am making websites where I need roles such as admin, Doctor , patient ,Receptionist.

I want to store UserId and roleid in AspNetUserRoles table.It is working longest I dont add duplicate data . But once I add duplicate data it gives me error.

I want different users can have one roles within Admin, Doctor, Patient ,or Receptionist. But I cannot add role more than one time in table. Please can anyone give me suggestion and help with this issue ?

This is AspNetUserRoles table and error


Solution

  • You have RoleId as a Primary Key(PK). You cannot have duplicate values that are a primary key. RoleId needs to be a Foreign key(FK) that relates to a table that contains your roles.