Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom identity user and extending profile MVC

I am trying to extend the identity user data in separate table but its not populating.

  public class MyUserInfo
  {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

  }

  public class AppUser : IdentityUser<Int32, AppUserLogin, AppUserRole, AppUserClaim>
  {

    public MyUserInfo MyUserInfo { get; set; }
 }

//Fetching the user data

var userStore = new UserStore<AppUser, AppRole, int, AppUserLogin, AppUserRole, AppUserClaim>(db);
var userManager = new UserManager<AppUser, int>(userStore);

var user = userManager.FindById(1);

user.MyUserInfo showing the null. But in db, we have the respective user data.

I saw many post where without customization seems working but for me I modified id stored as integer and changed the table name.

like image 901
Parwej Avatar asked May 04 '15 12:05

Parwej


1 Answers

The stock Identity UserManager does not automatically pull UserInfo (or any other table Entity with foreign key Navigation)

You have extended the Identity Schema, but you have not extended the UserManager.

None of the UserManager 's methods for getting user (e.g. findbyid(), FindByIdAsync()) retrieve "child" entities by Entity Framework Navigation.

Take a look at the GitHub FindByIdAsync

    public virtual Task<TUser> FindByIdAsync(string userId, CancellationToken cancellationToken = default(CancellationToken))
    {
        cancellationToken.ThrowIfCancellationRequested();
        ThrowIfDisposed();
        var id = ConvertIdFromString(userId);
        return Users.FirstOrDefaultAsync(u => u.Id.Equals(id), cancellationToken);
    }

The method only returns the User Entity. There is no functionality to check for an "child" Navigation tables, let alone pull them.


You can extend the UserManager by implementing such a method.

Otherwise, you will have to manually pull the child Entity after retrieving the UserId. This is how I did it:

            var UserId = User.Identity.GetUserId<int>();

            try
            {
                user = await UserManager.FindByIdAsync(UserId );
            }
            catch (Exception e)
            {
                return BadRequest(e.Message);
            }

            var userInfo = AuthContext.UserInfo.
                   FirstOrDefault(u => u.Id == user.UserInfo_Id);

Note: I also had to make sure that my ApplicationUser had a foreign key for my UserInfo Entity:

public class ApplicationUser : IdentityUser
{
    [ForeignKey("UserInfo")]
    public int UserInfo_Id { get; set; }

    public virtual UserInfo UserInfo { get; set; }

and in my model builder

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ApplicationUser>()
          .HasRequired<UserInfo>(profile => profile.UserInfo);


        base.OnModelCreating(modelBuilder);
    }
like image 75
Dave Alperovich Avatar answered Sep 22 '22 18:09

Dave Alperovich