Etaos
Etaos

Reputation: 927

Entity framework core: Retrieve data to view in Many to many relationship

I have issue with reading related data in my Entity framework core in ASP.NET Core 2.2.

I have two entities Team and Player and because I deal with many to many relationship (team can have many players and players can have multiple teams), I have junction entity PlayerAssignments.

In players Index view, I want to show all players and in information about them - first name, last name and the teams they are assigned to, but I don't know how and I am trying to solve this for about two days now.

public class Player
{
    public int id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; } 
    public ICollection<PlayerAssignments> PlayerAssignments { get; set; }
}
public class Team
{
    public int id { get; set; }
    public string name { get; set; }
    public string city { get; set; } 
    public ICollection<PlayerAssignments> PlayerAssignments { get; set; }
}
public class PlayerAssignment
{
    public int PlayerID { get; set; }
    public int TeamID { get; set; }
    public Player Player { get; set; }
    public Team Team { get; set; }
}

everything similarly in dbcontext.

In my index method of PlayerController.cs:

var player = await _context.Player
                .Include(one => one.PlayerAssignment)
                    .ThenInclude(team => team.Team)
                        .ThenInclude(teamName => teamName.name)
                .ToListAsync();

            if(player == null)
            {
                return NotFound();
            }
            return View(player);
        }

My index view looks like this:

<table class="table">
    <thead>
        <tr>
            <th>
                First name
            </th>
            <th>
                Last name
            </th>
            <th>
                Player's teams
            </th>
         </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.first_name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.last_name)
                </td>
                <td>
                    foreach(var assignedTeam in item.PlayerAssignments)                                                            
                    {
                        @* I just cant figure out code here *@
                    }
                </td>
            </tr>
    </tbody>
<table>

I need to show all players with their info and the teams they are assigned to. But no matter what I tried, I never managed to write out the teams the players are assigned to (they are filled in database).

Upvotes: 2

Views: 4656

Answers (1)

Tiago Silva
Tiago Silva

Reputation: 2349

First of all your second thenInclude is redundant because that variable already is being included in the Team Object so change to this:

var player = await _context.Player
  .Include(one => one.PlayerAssignment)
  .ThenInclude(team => team.Team)
  .ToListAsync();
    
if(player == null)
{
  return NotFound();
}
return View(player);

And now all you have to do is display that Team 'Name' variable in the foreach loop which contains all the player team assigments like so:

<td>
  foreach(var assignedTeam in item.PlayerAssignments)                                                            
  {
    @assignedTeam.Team.name
  }
</td>

Upvotes: 3

Related Questions