Search code examples
c#asp.netentity-frameworkasp.net-identityasp.net-core-2.0

asp.net core 2.0 Entity Framework how to access user profile


I am new to EF and trying to use it with .net core 2.0. I may be on the wrong track here and would appreciate any help. I have created a new web application in VS 2017 selected individual user account. I am now trying to build on this further by adding a user profile table to store various bits of information. The trouble I am having is then reading this as part of the ApplicationUser.

So far I have done the following.

In ApplicationUser I have created -

public class ApplicationUser : IdentityUser
{
    public virtual ICollection<UserProfileData> ProfileData { get; set; }
}

Then in UserProfileData table, I have -

public class UserProfileData
{
    public int  Id { get; set; }
    public string UserId { get; set; }
    public string Health { get; set; }
    public virtual ApplicationUser UserProfile { get; set; }
}

Is the correct way to join the tables and how can I then access the health field in the account controller, Edit and Create views?

I currently have this in the Register method.

var user = new ApplicationUser { UserName = model.Email, Email = model.Email 
            ,test_img ,Health = model.Health};  //I know this is wrong.

Any help would be great even if its just to say I am going about this in the wrong way.

I hope this was clear.

Thanks


Solution

  • You can just extend the ApplicationUser with any data you want to store for the user's profile.

    public class ApplicationUser : IdentityUser
    {
        public string Health { get; set; }
    
        public string Status { get; set; }
    }
    

    Right now in your code you have created a one-to-many relationship, which means a user can have multiple profiles (which is probably not what you want).

    I suggest reading this article and in particular this one to learn more about EF relationships.