I noticed that someone else has faced the same problem such as Cannot insert the value NULL into column 'UserId' but it should be caused by different reasons.
Th
1) Why are you using DbContext
to add Roles
to user
? There are some native Membership
methods to do it:
Roles.AddUsersToRole(string[] usernames, string rolename)
Roles.AddUserToRole(string username, string rolename) //**This one fits your purpose
Roles.AddUserToRoles(string username, string[] rolenames)
...
2) Value in key column can't be null
3) Int
can't be null
in C#. If you want to set null
value to int
, you should define it as nullable: int?