'IdentityResult DeleteAsync does not recognize that I deleted a row

I have this delete method that deletes a Client, which has a foreign key 'ApplicationUserId' mapped to my ApplicationUser. It should let me delete the Application user because I already deleted the client which would stop a foreign key problem.

    [MinSecurityLevel(SecurityLevelFlag.Admin)]
    public IActionResult Delete(long id)
    {
        Client domainObject = baseSvc.ClientRepo.GetById(id);
        if (domainObject != null)
        {
            baseSvc.ClientRepo.Delete(domainObject);

            //Get the application user's id. Delete the client, and then delete the corresponding applicaton user.
            ApplicationUser applicationUser = userManager.FindByIdAsync(domainObject.ApplicationUser.Id).Result;
                   
            if (applicationUser != null)
            {
                IdentityResult result = userManager.DeleteAsync(applicationUser).Result;
                
                if (!result.Succeeded)
                {
                    BannerText = Message.SomethingWentWrong;
                }
            }               
        }
        return RedirectToAction(nameof(Index), new { area = nameof(AdminPortal) });
    }

However, once the IdentityResult part of the code is reached I get the error

SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_M2O_Client_ApplicationUser_Id". The conflict occurred in database "Table_Dev", table "dbo.Client", column 'ApplicationUserId'.

Looking at the database through SSMS, I noticed that the deletion of the Client row doesn't actually happen until this function is finished running. Is there a way I can delete the Client without having to have a 2nd method?

EDIT: I've learned that the delete call doesn't actually happen right at that method call but sometime later. I Flushed the session of the client repo, so that it goes ahead and deletes the client, but then I get a timeout error when adding the new user via CreateAsync.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source