© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
6 replies
Mawral

Identity/EF collection returns null

Hey, so I'm new to using Entity Framework. I tried to extend the Identity User model with a one-to-many relationship, like so:
//Model/AppUser.cs
public class AppUser : IdentityUser
{
    public ICollection<Profile> Profiles { get; }
}

//Model/Profile.cs
public class Profile
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string AppUserId { get; set; }
    public AppUser AppUser { get; set; }
}
//Model/AppUser.cs
public class AppUser : IdentityUser
{
    public ICollection<Profile> Profiles { get; }
}

//Model/Profile.cs
public class Profile
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string AppUserId { get; set; }
    public AppUser AppUser { get; set; }
}

From here, I want to make a razor page that shows a logged in user's 'Profiles' as a list, but going about it the most obvious way gives me a null value. Is there a step I'm missing for getting collection data, or some other error?
//some razor page's cshtml
public async Task<IActionResult> OnGetAsync()
{
    StatusMessage = "";
    var user = await _userManager.GetUserAsync(User);
    if (user == null) {return NotFound($"User ID Not Found");}

    Username = user.UserName; //works as expected
    Profiles = user.Profiles; //null value
    return Page();
}
//some razor page's cshtml
public async Task<IActionResult> OnGetAsync()
{
    StatusMessage = "";
    var user = await _userManager.GetUserAsync(User);
    if (user == null) {return NotFound($"User ID Not Found");}

    Username = user.UserName; //works as expected
    Profiles = user.Profiles; //null value
    return Page();
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

InvalidOperationException - EF Instance - Identity Resolution
C#CC# / help
4y ago
Reset identity colums EF Core error.
C#CC# / help
16mo ago
❔ EF Core nullability vs API nullability behavior
C#CC# / help
4y ago
❔ .NET MAUI GetSystemService returns null
C#CC# / help
3y ago