2

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":

An exception of type 'System.InvalidOperationException' occurred in MySql.Web.dll but was not handled in user code

Additional information: UserTableName configuration was not initialized.

The user is added to the table "userprofile" (and "webpages_membership") and the role to "webpages_roles". "webpages_usersinroles" is empty.

The providers in Web.config (for SimpleMembership with MySql):

    <membership defaultProvider="MySqlSimpleMembershipProvider">
        <providers>
            <clear />
            <add name="MySqlSimpleMembershipProvider" type="MySql.Web.Security.MySqlSimpleMembershipProvider,MySql.Web,Version=6.9.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" applicationName="/" description="MySQLdefaultapplication" connectionStringName="MyConnectionStringName" userTableName="UserProfile" userIdColumn="UserId" userNameColumn="UserName" autoGenerateTables="True" />
        </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="MySqlSimpleRoleProvider">
        <providers>
            <clear />
            <add name="MySqlSimpleRoleProvider" type="MySql.Web.Security.MySqlSimpleRoleProvider, MySql.Web, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="MyConnectionStringName" applicationName="/" />
        </providers>
    </roleManager>

Does anyone know which configuration isn't right here? It's strange because the other things (creating users and roles) are working fine without exceptions.

Thanks in advance!

Update

I have added the attributes from the membershipprovider to the rolemanager.

    <roleManager defaultProvider="MySqlSimpleRoleProvider">
        <providers>
            <clear />
            <add name="MySqlSimpleRoleProvider" type="MySql.Web.Security.MySqlSimpleRoleProvider, MySql.Web, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="SamandarService_DB" applicationName="SamandarService" userTableName="UserProfile" userIdColumn="UserId" userNameColumn="Name" />
        </providers>
    </roleManager>

Another exception shows up now:

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 role is correctly added to the roles table, but MySql can't reference it because of a mysterious reason.

How do I fix this issue?

Community
  • 1
  • 1
Jens
  • 129
  • 1
  • 7

0 Answers0