How do I delete a user with a role in ASP.NET MVC4 SimpleMembership?
asp.net, asp.net-mvc, asp.net-mvc-4, c#, simplemembership
Solution
I can't find that quickly why there doesn't seem to be a on delete cascade rule on the UsersInRoles table for when a User gets deleted, but maybe this answer helps:
void DeleteUserRoles(string username)
{
foreach (var role in Roles.GetRolesForUser(username))
Roles.RemoveUserFromRole(username, role);
}
Problem
Thanks for looking. Background On my current project, the client would like me to use ASP.NET MVC4's Simple membership. Normally I do not use the .NET membership classes so perhaps I am missing something simple, but I can't seem to delete a user through code as there is a foreign key constraint between the `webpages_UsersInRoles` table and the `UserProfile` table. I have tried using `Membership.DeleteUser(id, true);` since the setting of 'true' should cascade the delete, but even that fails if there is a role assigned to the user. I have tried going into the `mdb` file through the server explorer and setting the delete action on the FK's to `CASCADE` but the option for delete action is disabled. As a last-ditch effort, I created a separate entity class (.edmx) from the membership database in hopes that I could hack my way to a successful delete through C#, but the generation of the .edmx refuses to bring in the `webpages_UsersInRoles` table! I am using C#, .NET 4.5. Question Using C#. how do I delete a user from MVC4 SimpleMembership if that user has been assigned one or more roles?