我在这里缺少一个概念。我假设这Membership.DeleteUser()
会将用户从我的会员表中删除。我的代码:
// remove all but 'admin' from Membership
MembershipUserCollection users = Membership.GetAllUsers();
foreach ( MembershipUser user in users )
{
if ( user.UserName != "admin" )
{
Membership.DeleteUser( user.UserName );
}
}
DeleteUser()
失败但有异常:
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 中:
<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()
是删除该用户在和表中的所有记录Membership
,仅举几例。User
UsersInRoles
当然,我可以直接进入Membership
表格并删除相应的记录,但这违背了使用 API 的目的。我究竟做错了什么?从成员资格表中删除用户的正确方法是什么?