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
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; }
}