Home > Software design >  ASP.NET Core - How to insert ApplicationUser id in IdentityDbContext into Employee Model as UserId
ASP.NET Core - How to insert ApplicationUser id in IdentityDbContext into Employee Model as UserId

Time:12-03

In ASP.NET Core Web ASPI, I am implementing IndentityDbContext and Employee Model:

    public Employee FromEmployeeCreateDtoToEmployee(EmployeetCreateDto employeeCreateDto)
    {
        if (employeeCreateDto == null)
        {
            return null;
        }

        return new Employee
        {
            EmployeeName = employeeCreateDto.EmployeeName,
            AccountNumber = employeeCreateDto.AccountNumber,
            Password = "12345",
            ConfirmPassword = "12345",
            Email = employeeCreateDto.Email,
            UserName = employeeCreateDto.UserName,
        };
    }

Service:

    public async Task<Employee> Post(EmployeeCreateDto employeeCreateDto)
    {
        var mapper = new EntityMapper();
        var employee = mapper.FromEmployeeCreateDtoToEmployee(employeeCreateDto);

        try
        {
            await _unitOfWork.EmployeeRepository.Insert(employee);
            await _unitOfWork.SaveChangesAsync();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        try
        {
            ApplicationUser user = new ApplicationUser()
            {
                UserName = employee.UserName,
                PasswordHash = employee.Password
            };
            IdentityResult result = await _userManager.CreateAsync(user, employee.ConfirmPassword);
            if (result.Succeeded)
            {
                Task<IdentityResult> roleResult;
                //Check that there is an Employee role and create if not
                Task<bool> hasEmployeeRole = _roleManager.RoleExistsAsync(UserRoles.Employee);
                hasEmployeeRole.Wait();
                if (!hasEmployeeRole.Result)
                {
                    ApplicationRole roleCreate = new ApplicationRole();
                    roleCreate.Name = UserRoles.Employee;
                    roleResult = _roleManager.CreateAsync(roleCreate);
                    roleResult.Wait();
                }
                Task<IdentityResult> newUserRole = _userManager.AddToRoleAsync(user, UserRoles.Employee);
                newUserRole.Wait();
                try
                {
                    //Commit the transaction
                    await _context.SaveChangesAsync();
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        return employee;
    }

Employee Model has a field called UserId which is foreign to the Id from ApplicationUser. Immediately after inserting into:

ApplicationUser user = new ApplicationUser()

I want to call the:

await _unitOfWork.EmployeeRepository.Insert(employee);

and update the Employee Model UserId with the Id from ApplicationUser.

How do I achieve this?

Thanks

CodePudding user response:

Sorry for that and I deleted that piece. I hope you can get the application user id in below way and then update the Employee.

var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context)); 
var user = new ApplicationUser() { "Pass your Application user parameters"}; 
var result = manager.Create(user, "Your Password");  

if (result.Succeeded) 
{     
 string newId = user.Id; 
} 

CodePudding user response:

Did you try accessing it as below;

try
    {
        await _unitOfWork.EmployeeRepository.Insert(employee);
        await _unitOfWork.SaveChangesAsync();
        var employeeId = employee.id;

    }
  • Related