0

I don't know if this is a duplicate, but I couldn't find any similar problems with solutions that helped in my case.

So I have an identity user that I want to add a role to based on which dropdown value the user chooses on signup page. I tested this part and it goes through just fine. But adding a role doesn't seem to work no matter what I try.

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
        returnUrl = returnUrl ?? Url.Content("~/");
        ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
        bool ownerRoleExists = await _roleManager.RoleExistsAsync("Owner");

        if (!ownerRoleExists)
        {
            var roleResult = await _roleManager.CreateAsync(new IdentityRole("Owner"));
        }

        bool patientRoleExists = await _roleManager.RoleExistsAsync("Patient");

        if (!patientRoleExists)
        {
            var roleResult = await _roleManager.CreateAsync(new IdentityRole("Patient"));
        }

        bool workerRoleExists = await _roleManager.RoleExistsAsync("Worker");

        if (!workerRoleExists)
        {
            var roleResult = await _roleManager.CreateAsync(new IdentityRole("Worker"));
        }

        if (ModelState.IsValid)
        {
            var user = new Person { UserName = Input.Email, Email = Input.Email, Name = Input.Name, LastName = Input.LastName, Address = Input.Address };
            var result = await _userManager.CreateAsync(user, Input.Password);

            if (result.Succeeded)
            {
                if (Input.PersonType.Equals("patient"))
                {
                    var patient = new Patient { PersonId = user.PersonId, VisitCount = 0, Number = Input.Number };
                    _context.Add(patient);
                    var patient_result = await _context.SaveChangesAsync();

                    if(patient_result > 0)
                    {
                        var result_role = await _userManager.AddToRoleAsync(user, "Patient");
                    }
                }
                else if(Input.PersonType.Equals("worker"))
                {
                    var worker = new Worker { PersonId = user.PersonId, WorkCode = Input.WorkCode };
                    _context.Add(worker);
                    var worker_result = await _context.SaveChangesAsync();

                    if(worker_result > 0)
                    {
                        var result_role = await _userManager.AddToRoleAsync(user, "Worker");
                    }
                }
                
                _logger.LogInformation("User created a new account with password.");
            }
        }
}

The error I get is:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_AspNetUserRoles_person_UserId". The conflict occurred in database "hospital-prod", table "dbo.person", column 'person_id'.
The statement has been terminated.

The values I get from user are correct, when I checked in the debug right before role is added so variable user should be correct.

Also user and patient/worker are added to the database successfully, so only the role is a problem.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
masterdodo
  • 31
  • 1
  • 6
  • I thing this should be tagged "Entity Framework" instead of "ASP.Net". If I understand [this answer](https://stackoverflow.com/a/21195605) correctly, you should add the user to `User` before adding it to `Person`. However, EF should be able to do everything in the right order by itself, if scaffolded correctly. – JHBonarius Jan 09 '21 at 10:25
  • @JHBonarius I think I didn't clarify it correctly. My `User` is `Person` aka. class `Person` inherites `IdentityUser` so there is no other class above it. And it's weird because it's all added to the database correctly, which is why I don't understand why the error is of foreign key constraint type, when the user is in the database before I try to add a role. – masterdodo Jan 09 '21 at 10:53
  • Can you check [this](https://stackoverflow.com/a/41283431) answer? – JHBonarius Jan 09 '21 at 12:01
  • Yes, tried it but there is no error. I appreciate the help, but somehow it just doesn't want to work I guess. And it doesn't have a problem inserting Patient or Worker, which also have fk contraint. It's weird. – masterdodo Jan 09 '21 at 12:20
  • @JHBonarius tnx for the help. Of course it was a stupid error at the end. :) – masterdodo Jan 09 '21 at 13:21

1 Answers1

0

Okay, so I figured out what the real problem was; and I'm going to briefly explain it, if someone else is in the same situation at some point.

The problem with adding a role in my case was that method AddToRoleAsync(User user, String role) accepts a User type variable not an ID, so it selects the ID by itself.

Well, I have a custom database where i already have a person_id column and the rules of IdentityUser don't allow you to remove any of the default columns and that includes the Id. So the problem was that that method automaticay selected a wrong id and I had to change the behaviour of ids' to get it to work/select the right one.

masterdodo
  • 31
  • 1
  • 6