c# - 是否可以从默认表 AspNetUsers 中删除行?如果是,请解释一下

标签 c# asp.net asp.net-mvc entity-framework ef-code-first

是否可以从默认表 AspNetUsers 中删除行?如果是,请解释一下。

我能够删除具有 AspNetUsers Fk 的记录。

公共(public) ActionResult 删除(学生 st)
{
var Studentuser = st.user;
st.user = null;
db.Entry(st).State = EntityState.Deleted;
db.SaveChanges();//从数据库中删除成功
var userid = User.Identity.GetUserId();
var user = _userManager.FindById(userid);
_userManager.DeleteAsync(用户);
return RedirectToAction("Index", "Admin");
}

最佳答案

这是我之前用来删除用户的代码片段

// 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);

关于c# - 是否可以从默认表 AspNetUsers 中删除行?如果是,请解释一下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33014357/

相关文章:

c# - 如何覆盖 Autofac 子范围中的 Serilog 记录器?

html - 奇怪的 "line"出现在 GridView 中——是 CSS 还是边框还是什么?

c# - 使用 ASPNETDB.MDF 添加到 c# asp.net 中的表

javascript - 通过jquery获取更新值的Id

c# - 在 Winform 中复制 LocalDB 给我一个 'File being used by another process'

c# - Json.net `JsonConstructor` 构造函数参数名

asp.net - Aspnet MVC 中的相对路径和绝对路径有什么区别?

asp.net - 使用 SSL 在服务器上部署 ASP.Net MVC 5 应用程序

c# - 带有 firefox 3.6 的 ajax modalpopupextender 显示在页面底部

c# - ASP.Net 中非 ANSI 字符的 QueryString 编码