EF7 nested Include not showing in Razor .net

2019-09-09 09:34发布

问题:

I have my models like this:

  1. Goup.cs
  2. GroupUser (pivot table)
  3. ApplicationUser (User) -> 4. Profile

And now I want to show the data in Profile on a details page when the User belongs to the group. I'm doing this like this:

private IEnumerable<GroupUser> GetUsers(int groupId)
    {
        IEnumerable<GroupUser> model = null;

        if(groupId == 0)
        {
            model = _kletsContext.GroupUser.OrderByDescending(o => o.GroupId).AsEnumerable();
        }
        else
        {
            model = _kletsContext.GroupUser.Where(g => g.GroupId == groupId).Include(p => p.User.Profile).OrderByDescending(o => o.GroupId).AsEnumerable();
        }

        return model;
    }

This works, if I just want to display the UserId, ... (so the data in the Pivot table) with this code:

@model IEnumerable<App.Models.GroupUser>
@if(Model != null && Model.Count() > 0)
{   
@foreach(var user in Model)
{
@user.UserId</h2>
}
}

But for some reason I can't display the data in the Included tables? Normally you would do something like this: @user.User.Profile.XXXX but then I get the error: System.NullReferenceException: Object reference not set to an instance of an object So this would mean the return is null, but there are users in the pivot table with a profile.

The models:

Group.cs:

namespace App.Models
{
public class Group : Item
{
    public Group() : base()
    {

    }

    [Key]
    public Int16 Id { get; set; } 
    public string Name { get; set; }
    public string Images { get; set; }

    /* Foreign Keys */
    public Nullable<Int16> RegionId { get; set; }

    public virtual Region Region { get; set; }
    public virtual ICollection<Lets> Lets { get; set; }
    public virtual ICollection<GroupUser> Users { get; set; }
}
}

ApplicationUser:

namespace App.Models.Identity
{
public class ApplicationUser : IdentityUser
{   
    public string Description { get; set; }
    public DateTime CreatedAt { get; set; }
    public Nullable<DateTime> UpdatedAt { get; set; }
    public Nullable<DateTime> DeletedAt { get; set; } 

    /* Virtual or Navigation Properties */
    public virtual Profile Profile { get; set; }
    public virtual ICollection<GroupUser> Groups { get; set; }    
    public virtual ICollection<Lets> Lets { get; set; }  
    public virtual ICollection<Category> Categories { get; set; } 
    public virtual ICollection<Region> Regions { get; set; }    
    public virtual ICollection<Status> Status { get; set; }   
    public virtual ICollection<Tag> Tags { get; set; }   
}
}

GroupUser:

namespace App.Models
{
public class GroupUser
{
    public GroupUser()
    {

    }

    public Nullable<Int16> GroupId { get; set; }
    public string UserId { get; set; }
    public virtual Group Group { get; set; }
    public virtual ApplicationUser User { get; set; }
}
}

Profile.cs:

namespace App.Models
{
public class Profile : Item
{
    public Profile() : base()
    {

    }

    [Key]
    public string UserId { get; set; } 
    public string FirstName { get; set; }
    public string SurName { get; set; }
    public string Email { get; set; }
    public string Gender { get; set; }
    public Int16 Age { get; set; }
    public string City { get; set; }
    public string Image { get; set; }
    public Int16 Credits { get; set; }
    public Int16 Postalcode { get; set; }
}
}

How can i display the nested data with razor?

回答1:

model = _kletsContext.GroupUser.Where(g => g.GroupId == groupId)
    .Include(gu => gu.User)
    .ThenInclude(u => u.Profile)
    .OrderByDescending(o => o.GroupId)
    .AsEnumerable();

Don't get freaked out when intellisense doesn't work for the ThenInclude, just type it, it will compile.



回答2:

try to include the user-reference

model = _kletsContext.GroupUser.Where(g => g.GroupId == groupId).Include(p => p.User).Include(p => p.User.Profile).OrderByDescending(o => o.GroupId).AsEnumerable();