5
votes

How do I populate a navigation property with specific value?

I have 3 models, Game, UserTeam, User, defined below. I have a razor view which uses the model IEnumerable. This view loops over the Games, and within that loop, loops over the UserTeams. So far, so good.

Within the UserTeam loop, I want to access the User properties, but they are null. How do I populate the User navigation property for each UserTeam object? Do I need a constructor with a parameter in the UserTeam model?

Models

public class Game
{
    public Game()
    {
        UserTeams = new HashSet<UserTeam>();
    }

    public int Id { get; set; }
    public int CreatorId { get; set; }
    public string Name { get; set; }
    public int CurrentOrderPosition { get; set; }

    public virtual UserProfile Creator { get; set; }
    public virtual ICollection<UserTeam> UserTeams { get; set; }
}


 public class UserTeam
{
    public UserTeam()
    {
        User = new UserProfile();
    }

    public int Id { get; set; }
    public int UserId { get; set; }
    public int GameId { get; set; }
    public int OrderPosition { get; set; }

    public virtual UserProfile User { get; set; }
    public virtual Game Game { get; set; }
    public virtual IList<UserTeam_Player> UserTeam_Players { get; set; }

}

public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string test { get; set; }

    public UserProfile()
    {
        UserTeams = new HashSet<UserTeam>();
    }

    public virtual ICollection<UserTeam> UserTeams { get; set; }
    [ForeignKey("CreatorId")]
    public virtual ICollection<Game> Games { get; set; }
}

Loop in my Razor view (Model is IEnumerable)

@foreach (var item in Model) {
        @foreach (var userteam in item.UserTeams) {
                        @Html.ActionLink("Join game as"+userteam.User.UserName, "JoinGame", new { gameid = item.Id, userid=userteam.UserId })
        }
}

Method in my repository that returns the Games

public IEnumerable<Game> GetAllGames()
    {
        using (DataContext)
        {
            var gm = DataContext.Games.Include("UserTeams").ToList();
            return gm;
        }
    }
1
Thewads answer combined with @Slaumas comment solved the problem. Question remains though as to why instantiating the UserTeams in the Game constructor doesn't cause the same problem as instantiating the User in the UserTeam constructor - jag

1 Answers

6
votes

You would need to include this in your repository method. If you are using eager loading then it would be something like

var gm = DataContext.Games
                     .Include(x => x.UserTeams)
                     .Include(x => x.UserTeams.Select(y => y.User))
                     .ToList();

I have not done this without using LINQ for my queries, but I assume it would be something like:

var gm = DataContext.Games.Include("UserTeams.User").ToList();

Hopefully this helps you out