public class ApplicationUser : IdentityUser
{
public ICollection<Profile> Profiles { get; set; }
}
I have a collection of Profiles in ApplicationUser.
I created a custom ProfileService
, in method GetProfileDataAsync
var user = await _userManager.GetUserAsync(principal);
user.Profiles returns a null value. It must be somewhere to get the user data before adding to ClaimPrincipals.
How can I customize somewhere in Asp.Net Core Identity to load Profiles when getting User.
In the case of just property, for example, ContractName. It works.
How can I customize somewhere in Asp.Net Core Identity to load Profiles when getting User
Asp.Net Core Identity is abstract system which has no notion of loading related data, which is EF (Core) concept, hence cannot be configured at that level.
EF Core versions prior 5.0 also have no notion of "auto eager load" navigation property (at least not officially). So you must either configure lazy loading (with all associated drawbacks), or issue additional manual eager loading queries as shown in another answer.
EF Core 5.0 provides a way to configure navigation property to be automatically eager loaded using the new Navigation
and AutoInclude
fluent APIs:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>()
.Navigation(e => e.Profiles)
.AutoInclude();
}
In EF Core 3.x, although not officially announced and no fluent API available, the same can be configured with SetIsEagerLoaded metadata API:
modelBuilder.Entity<ApplicationUser>()
.Metadata.FindNavigation(nameof(ApplicationUser.Profiles))
.SetIsEagerLoaded(true);