4

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, CONSTRAINT fk_RoleId FOREIGN KEY (RoleId) REFERENCES webpages_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!

Spider man
  • 3,224
  • 4
  • 30
  • 42
Jens
  • 129
  • 1
  • 7

2 Answers2

0

First exec set foreign_key_checks=0;
execute your query
again set foreign_key_checks=1;

Krishna
  • 438
  • 5
  • 18
0

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

sln
  • 1