是否可以从默认表AspNetUsers中删除行?如果是,请解释。
我能删除具有AspNetUsers Fk的记录。
公共ActionResult删除(STUDENT st)
{
var studentuser = st.user;
st.user = null;
db.Entry(st).State = EntityState.Deleted;
db.SaveChanges(); //从db中成功删除
var userid = User.Identity.GetUserId();
var user = _userManager.FindById(userid);
_userManager.DeleteAsync(用户);
return RedirectToAction(" Index"," Admin");
}
答案 0 :(得分:3)
这是我之前用来删除用户的代码段
// Find the user
var user = await _userManager.FindByIdAsync(id);
var logins = user.Logins;
// Delete every login, if he has
foreach (var login in logins.ToList())
{
await _userManager.RemoveLoginAsync(login.UserId, new UserLoginInfo(login.LoginProvider, login.ProviderKey));
}
// Delete every role, if he has
var rolesForUser = await _userManager.GetRolesAsync(id);
if (rolesForUser.Count() > 0)
{
foreach (var item in rolesForUser.ToList())
{
// item should be the name of the role
var result = await _userManager.RemoveFromRoleAsync(user.Id, item);
}
}
// Delete the user itself
_await _userManager.DeleteAsync(user);
答案 1 :(得分:2)
示例:Roles.RemoveUserFromRoles("userName",Roles.GetRolesForUser("userName"));
指向类似问题的链接:how-to-delete-a-simplemembership-user