2
votes

is it possible to delete row from default table AspNetUsers ? if yes please explain.

i able to delete record which has Fk of AspNetUsers.

public ActionResult Delete(STUDENT st)
{
var studentuser = st.user;
st.user = null;
db.Entry(st).State = EntityState.Deleted;
db.SaveChanges();// delete from db successfully
var userid = User.Identity.GetUserId();
var user = _userManager.FindById(userid);
_userManager.DeleteAsync(user);
return RedirectToAction("Index", "Admin");
}

2

2 Answers

3
votes

This is a code snippet I've used previously to delete a user

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

Yeh it does. But at first you have to remove all existing roles from the user before deleting it.

The example: Roles.RemoveUserFromRoles("userName",Roles.GetRolesForUser("userName"));

There is a link to similar question:how-to-delete-a-simplemembership-user