Jul 6 2009

ASP.NET Membership Woes

The problem:

I kept on getting foreign key constraint errors when trying to use ASP.NET (2.0) DeleteUser method:


All my research on the internet pointed to it being a permissions problem. Like many of the posts I found, this worked fine in my local development database, but in production it would yield this error:

The DELETE statement conflicted with the REFERENCE  constraint
"FK__aspnet_Me__UserI__15502E78". The conflict occurred in  database
"dbname", table "dbo.aspnet_Membership", column 'UserId'.
The  statement has been terminated.

After hours of experimentation with user permissions in the database and research on the problem, the support team at the hosting company I was working with found the solution of setting the references foreign key constraints to cascade upon deletion. I went into SQL Server Management Studio and updated the FKs that were referenced in the errors to cascade upon deletion and voila! Problem solved. Good job CrystalTech support and especially Geoff.

Update: I looked into my local database (that has been working properly) and saw that the foreign keys are not set to cascade deletions, so it still may be some strange permissions issue. Regardless, this seems like an acceptable workaround and it did get those deletions to work (and the usernames freed up).