1

I'm creating fantasy football game and I'm using Entity Framework for the mapping of my database.

There is a table with all the possible football players called Players, a table that contains team information called Teams, a table that contains league information and a table keeps track of which player belongs to which team ( because one player can belong to many teams depending on which league they are in) called League Player.

I'm running into a problem however, when I try to get all the players from a team. When I make the query from my context, the players are showing up null, while if I look ID's and in the databases, all the information is there. Does anyone know what is causing this? Did I set up my Models incorrectly ?

Query

    public async Task<IActionResult> getTeamPlayers(){
        // Null values
        var y = await context.Teams.Include(s => s.Players)
            .SingleOrDefaultAsync(t => t.ManagerID == manager.ManagerID);
        // Also null values 
        var z = await context.Leagues.Include(l => l.Teams)
            .ThenInclude(t => t.Players)
            .AsNoTracking()
            .ToListAsync(); 

        return Json(y);
    }

Debug

enter image description here enter image description here

Models

public class Team{
    [Key]
    public int TeamID { get; set; }
    public int ManagerID { get; set; }
    public int LeagueID { get; set; }
    public string TeamName { get; set; }
    public virtual Manager Manager { get; set; }
    public virtual League League { get; set; }
    public virtual ICollection<LeaguePlayer> Players { get; set; }

}

public class Player {
    [Key]
    public int PlayerID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Postion { get; set; }
}

public class LeaguePlayer{
    [Key]
    public int LeaguePlayerID { get; set; }
    [ForeignKey("Player")]
    public int PlayerID { get; set; }
    [ForeignKey("Team")]
    public int TeamID { get; set; }
    [ForeignKey("League")]
    public int LeagueID { get; set; }
    public virtual Player Player { get; set; }
    public virtual Team Team { get; set; }
    public virtual League League { get; set; }

}
public class League{
    [Key]
    public int LeagueID { get; set; }
    public int CommissionerID { get; set; }
    public string LeagueName { get; set; }
    [ForeignKey("CommissionerID")]
    public virtual Manager Commisoner {get; set;} 
    public virtual ICollection<Team> Teams { get; set; }

}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
AJ_
  • 3,787
  • 10
  • 47
  • 82
  • There is something wrong in the way you build your model... that table `leaguePlayer` specificly. I'm going to try to show you a picture that explains – Antoine Pelletier Dec 02 '16 at 17:01
  • @AntoinePelletier thanks i appreciate it. – AJ_ Dec 02 '16 at 17:13
  • Do you want me to push furtur my answer ? cause if i understand correctly how your game work, a player is part of a `LEAGUE` only if he is part of `A TEAM` in the `LEAGUE` – Antoine Pelletier Dec 02 '16 at 18:34
  • Microsoft naming guidelines state that methods should begin with a capital letter (FYI Re: `getTeamPlayers()`), – Erik Philips Dec 02 '16 at 21:24

2 Answers2

3

Personnaly i think that this is what you want :

enter image description here

But here i see this :

enter image description here

Does a player belong to a team or a league ? if both it's going to be way more complex than you think, it's going to be a triple many to many

ok, finally after knowing exactly what you want, there is going to be 2 "many to many", and one "one to many"

enter image description here

AND ! YOU are going to manually validate that a player is not in 2 diferent teams in the same league.

So, make 2 junctions tables here, it's going to be hard to understand, but if you want "free agents player" in leagues that would not apear in other leagues, then it's the only way to go

Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
  • Yeah the first diagram is what i was trying to make. How did i screw it up ? – AJ_ Dec 02 '16 at 20:25
  • A League Player belongs to a Team. A Player doesn’t really belong to anyone. The idea is that there is a set amount of players. When a User creates a league, they can only have access to those set players. So player A can belong to Team A in League A, but not Team B in League A. However, Player A can belong to Team A in League A and Team A in League B. Understand ? – AJ_ Dec 02 '16 at 20:31
  • ha god, ok i get it, wait – Antoine Pelletier Dec 02 '16 at 21:02
  • So this will solve my null values ? Is the many to many my LeaguePlayer model ? – AJ_ Dec 02 '16 at 21:31
  • I looked over my models, compared it to your diagram and I’m pretty sure i do have the correct relationships. – AJ_ Dec 03 '16 at 04:11
0

Due to lazy loading (using the virtual key word), entity doesn’t load the data endless its already in use. If I called Leagues first the data would have loaded. By editing the query to include the data that needs to be used, the data shows up.

       var x = await context.Teams
            .Include(t => t.League)
            .Include(t => t.Players)
            .ThenInclude(p => p.Player)
            .ToListAsync(); 
AJ_
  • 3,787
  • 10
  • 47
  • 82
  • http://stackoverflow.com/questions/40729137/why-are-foreign-keys-in-ef-code-first-marked-as-virtual/40729675#40729675 see, if you remove vitrual, your entity will be eagerly loaded, and you won't need the .include – Antoine Pelletier Dec 06 '16 at 15:47
  • That was my answer to a previous question... lol i should have notice your virtual foreign key – Antoine Pelletier Dec 06 '16 at 15:48