Membership.DeleteUser()因RI约束而失败 [英] Membership.DeleteUser() fails with RI Constraint

查看:107
本文介绍了Membership.DeleteUser()因RI约束而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里缺少一个概念.我以为Membership.DeleteUser()将从我的成员资格表中删除用户.我的代码:

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()失败,发生以下异常:

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.

在我的Web.config中:

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>

我了解到Membership.UserId -> Users.UserId之间存在外键关系,但应该假设DeleteUser()的全部要点是删除该用户在MembershipUserUsersInRoles中的所有记录表,仅举几例.

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.

当然,我可以直接进入Membership表并删除相应的记录,但这违反了使用API​​的目的.我究竟做错了什么?从成员资格"表中删除用户的正确方法是什么?

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?

推荐答案

好吧,Membership.DeleteUser(string)用true调用SqlMembershipProvider.DeleteUser(string, bool),然后调用存储的proc aspnet_Users_DeleteUser.该sproc中包含代码,可删除其他相关表中的各种额外数据.

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.

我唯一想到的另一件事是您是否创建了与Membership表(例如您自己的users表)的任何其他外键关系?如果是这样,则必须先删除这些记录.这是不建议创建此类外键关系的原因之一.

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.

检查aspnet_Membership表上的FK关系,看看是否有不应该存在的关系.

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

这篇关于Membership.DeleteUser()因RI约束而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆