Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating navigation properties of navigation properties

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;
        }
    }
like image 623
jag Avatar asked Oct 02 '13 06:10

jag


People also ask

What are navigational properties?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data. A navigation property definition includes the following: A name.

What is reference navigation property?

Reference navigation property: A navigation property that holds a reference to a single related entity. Inverse navigation property: When discussing a particular navigation property, this term refers to the navigation property on the other end of the relationship.

What is the use of navigation property in Entity Framework?

In Entity Framework you commonly use navigation properties to load entities that are related to the returned entity by the defined association. For more information, see Loading Related Objects.

What is navigation property in Linq?

Navigation properties allow a user to navigate from one entity to another, or from one entity to related entities through an association set. This topic provides examples in query expression syntax of how to navigate relationships through navigation properties in LINQ to Entities queries.


1 Answers

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

like image 61
Thewads Avatar answered Oct 24 '22 08:10

Thewads