问题
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; }
}
回答1:
Personnaly i think that this is what you want :
But here i see this :
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"
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
回答2:
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();
来源:https://stackoverflow.com/questions/40937111/data-displaying-null-types