Cannot insert the value NULL into column 'RoleId' (mvc4 simple membership)

前端 未结 2 701
花落未央
花落未央 2021-01-28 11:26

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

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-28 12:20

    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?

提交回复
热议问题