Membership.DeleteUser() fails with RI Constraint

左心房为你撑大大i 提交于 2019-12-24 01:23:43

问题


I'm missing a concept here. I was assuming that Membership.DeleteUser() would expunge a user from my membership tables. My code:

// remove all but 'admin' from Membership
MembershipUserCollection users = Membership.GetAllUsers();

foreach ( MembershipUser user in users )
{
    if ( user.UserName != "admin" )
    {
        Membership.DeleteUser( user.UserName );
    }
}

DeleteUser() fails with exception:

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

In my Web.config:

<membership defaultProvider="MyMembershipProvider">
  <providers>
    <clear/>
    <add name="MyMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MembershipConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="MyApplication"/>
  </providers>
</membership>

I get it that there's a foreign key relationship between Membership.UserId -> Users.UserId but would have assumed that the whole point of DeleteUser() is to remove all the records for this user in the Membership, User and UsersInRoles tables, to name a few.

Of course, I could go straight into the Membership table and delete the appropriate record, but that's defeating the purpose of using the API. What am I doing wrong? What is the correct way to delete a user from the Membership tables?


回答1:


Well, Membership.DeleteUser(string) calls SqlMembershipProvider.DeleteUser(string, bool) with true, and that calls the stored proc aspnet_Users_DeleteUser. That sproc has code in it to delete the various extra data in other tables that are related.

You can check your sproc to see if it's been altered.

The only other thing I can think of is whether or not you created any other foreign key relationships to the Membership tables, such as to your own users table? If so, you would have to delete those records first. This is one reason it's not recommended to create such foreign key relationships.

Check the FK relationships on the aspnet_Membership table and see if there are any that shouldn't be there.




回答2:


I had the same problem. For me, it turned out to be because my database had the tables and stored procs for ASP membership but not the views. I installed the nine views, and then it worked. I figured it out by looking at the implementation of the SP aspnet_Users_DeleteUser. In it, you can see that for some weird reason, it looks to see if the views exist before deleting records from some of the tables. Since the view didn't exist in my DB, it wasn't deleting the records from aspnet_Membership first.



来源:https://stackoverflow.com/questions/12763229/membership-deleteuser-fails-with-ri-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!