Search code examples
asp.net-coreidentityasp.net-core-identity

How do i get all column values of aspnetusers table right after registering in asp.net core


i am facing this issue where i have an identity column in aspnetuser table and i need to get the value of this db generated identity column value right after registration i am using these lines of code

  var user = new ApplicationUser { UserName = Input.UserName, Email = Input.Email };
                    var result = await _userManager.CreateAsync(user, Input.Password); 

 public class ApplicationUser : IdentityUser
    {
        [PersonalData]
        [NotMapped]
        public int? U_ID { get; set; }
    } 

i have tried below lines but some of them returns null and some only returns the data which is either entered by user like username and password and email and fields generated by identity like normalized email etc but i am unable to get U_ID column it remains null

 ApplicationUser usersss = await _userManager.GetUserAsync(HttpContext.User);
                        //string message = "Hello " + user.UserName;


                        var usernames = HttpContext.User.Identity.Name;

                        var userEmail = User.FindFirstValue(ClaimTypes.Name); // will give the user's Email

                        var user2 = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);
                        var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
                        HttpContext.User = await _signInManager.CreateUserPrincipalAsync(user);
                        ApplicationUser userjsss = await _userManager.GetUserAsync(HttpContext.User);

                        var userIds = _userManager.GetUserId(HttpContext.User);


                        var userv = await _userManager.FindByEmailAsync(Input.Email);

it is very crucial to get this U_ID column value and store in an inmemory claim as this will be needed throughout the app unless user logs out


Solution

  • i found the answer i was using [NotMapped] attribute which was not mapping efcore model and hence i was getting null in U_ID column all i had to do was to remove [NotMapped] and use [DatabaseGenerated(DatabaseGeneratedOption.Identity)] instead

    public class ApplicationUser : IdentityUser
        {
            [PersonalData]
            //[NotMapped]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int? U_ID { get; set; }
        }