I'm trying to use SimpleMembership with MySQL in a Code First project. This is the initializer I made:
WebSecurity.InitializeDatabaseConnection("MyConnectionStringName", "UserProfile", "UserId", "UserName", false);
if (!Roles.RoleExists("Employee"))
{
Roles.CreateRole("Employee");
}
if (!WebSecurity.UserExists("Kurt"))
{
WebSecurity.CreateUserAndAccount("Kurt", "test");
Roles.AddUserToRole("Kurt", "Employee");
}
The following exception appears by AddUserToRole
:
Cannot add or update a child row: a foreign key constraint fails (
ticket
.webpages_usersinroles
, CONSTRAINTfk_RoleId
FOREIGN KEY (RoleId
) REFERENCESwebpages_roles
(RoleId
))
The user is added to the table userprofile
(and webpages_membership
) and the role to webpages_roles
. webpages_usersinroles
is still empty. MySQL can't reference the role because of a mysterious reason. How do I fix this issue?
Thanks in advance!
First exec set foreign_key_checks=0;
execute your query
again set foreign_key_checks=1;
However, when using the API to programmatically add users and roles, you need to make sure your database already has the database schema required by the application services. Fortunately, again this is pretty easy to do. In the sample application you find code that looks like this:
http://imar.spaanjaars.com/563/using-entity-framework-code-first-and-aspnet-membership-together
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.