J
J
JackBoner2014-11-30 17:20:49
ASP.NET
JackBoner, 2014-11-30 17:20:49

ASP.NET MVC 5. Identity. How to get data from a separate table?

How to unload additional data about the user through the UserManager, which is stored in a separate table, which is connected on a one-to-one basis with the main table (AspNetUsers)
Now for more details:
in the class ApplicationUser : IdentityUser
, the property
public UserProfile UserProfile { get; set; }
and here is the model itself:

public class UserProfile
    {
        [Key,ForeignKey("ApplicationUser")]
        public string ID { get; set; }
        public ApplicationUser ApplicationUser { get; set; }

        public DateTime? Birthday { get; set; }       
    }

When registering a new user, the following is done:
newUser.UserProfile = new UserProfile {Birthday=birthday,ID = newUser.Id};
UserManager.Update(newUser);

All this business is successfully carried out and saved in a DB.
But when I try to fetch user profile data via UserManager
ApplicationUser user = await UserManager.FindByNameAsync(userName)

user.UserProfile always = null
That is, FindByUsername retrieves only the main data, without those that are in separate tables.
How can I extract complete information through the UserManager, which includes data in related tables?
for example to have the same result
сontext.Users.Include(Users => Users.UserProfile).Single(u => u.UserName==User.Identity.Name);

---
And one more thing: I understand that in order to retrieve data from the database asynchronously, you need to pack each request into a method like Task GetSomethingAsync() ?
Isn't there anything easier?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
asdz, 2014-11-30
@asdz

Maybe you just disabled the loading of related objects in EF? Look at the LazyLoadingEnabled property of the context.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question