2

I am trying to delete an already existing user from a database, which was created automatically when creating MVC application.
The database consists of tables:

AspNetUsers
AspNetUserRoles
AspNetUserLogins
AspNetUserClaims
AspNetRoles

In my code it looks like this:

var user = new ApplicationUser { UserName = model.email, Email = model.email };
var context = new ApplicationDbContext();
context.Users.Attach(user);
context.Users.Remove(user);
context.SaveChangesAsync();
return RedirectToAction("OperationSuccess", "Account");

I have also tried this:

var user = new ApplicationUser { UserName = model.email, Email = model.email };
var context = new ApplicationDbContext();
UserManager.DeleteAsync(user);

But it doesn't help at all. The application itselt does not break and does not show any errors, but the user is still in the database. How do I delete it?

Maciej Miśkiewicz
  • 412
  • 2
  • 8
  • 22
  • What is the IdentityResult? *var identityResult = await UserManager.DeleteAsync(user);* There should be information in the Errors property: https://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.identityresult(v=vs.108).aspx –  May 28 '17 at 19:19

3 Answers3

1

Try this code:

public async Task<IdentityResult> DeleteUser(string email)
{
    var user = UserManager.Users.FirstOrDefault(x => x.Email == email);
    if(user == null) return null;
    var result = await UserManager.DeleteAsync(user); //here result has two properties Errors and Succeeded.
    return result;
} 

Also, your code is not working because you are creating the object yourself and assigning only two properties yourself in spite of fetching the data from database.

Prateek Pandey
  • 833
  • 6
  • 19
0

Hi I think you have some versioning problem and its seems that you need to give one extra paramater to the DeleteAsync method.

Kindly refer the below link, since they had same kind of issue and resolved it.

https://stackoverflow.com/a/24594440/3397630

Hope it may give you some idea for your solution too.

Thanks

Karthik

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12
0

Hope below code will help you to fix your problem

[HttpPost]

    public async Task<ActionResult> Delete(string userId)
    {
        // Check for for both ID and exit if not found
        if (String.IsNullEmpty(userId))
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }  
        var user = UserManager.Users.SingleOrDefault(u => u.Id == Userid);

// Look for user in the UserStore

        // If not found, exit
        if (user == null)
        {
            return HttpNotFound();
        }



            var results = await UserManager.DeleteAsync(user); // Remove user from UserStore

            // If the statement is a success

            if (results.Succeeded)
            {
                // Redirect to Users page
                return RedirectToAction("Index", "Users");
            }
            else
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }       
        }
Jinto John
  • 365
  • 4
  • 22