我创建了一个teams web api控制器,并try 调用GET方法来获取数据库中所有团队的json结果.但是当我打电话的时候,我只是让第一队回到json中,但是当我在return语句上设置一个断点时,它包含了所有的254支球队以及所有的比赛.
以下是我正在处理的两个模型:
public class Team
{
public string Id { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public string Mascot { get; set; }
public string Conference { get; set; }
public int NationalRank { get; set; }
public List<Game> Games { get; set; }
}
public class Game
{
public string Id { get; set; }
public string Opponent { get; set; }
public string OpponentLogo { get; set; }
public string GameDate { get; set; }
public string GameTime { get; set; }
public string TvNetwork { get; set; }
public string TeamId { get; set; }
public Team Team { get; set; }
}
When I do this:
[HttpGet]
public async Task<List<Team>> Get()
{
var teams = await _context.Teams.ToListAsync();
return teams;
}
I get all 254 teams but Game property is null because EF Core does not support lazy loading. So what I really want to do is add the .Include() like this:
[HttpGet]
public async Task<List<Team>> Get()
{
var teams = await _context.Teams.Include(t => t.Games).ToListAsync();
return teams;
}
This returns the first team with the first game but nothing else. Here is the json:
[
{
"id": "007b4f09-d4da-4040-be3a-8e45fc0a572b",
"name": "New Mexico",
"icon": "lobos.jpg",
"mascot": "New Mexico Lobos",
"conference": "MW - Mountain",
"nationalRank": null,
"games": [
{
"id": "1198e6b1-e8ab-48ab-a63f-e86421126361",
"opponent": "vs Air Force*",
"opponentLogo": "falcons.jpg",
"gameDate": "Sat, Oct 15",
"gameTime": "TBD ",
"tvNetwork": null,
"teamId": "007b4f09-d4da-4040-be3a-8e45fc0a572b"
}
]
}
]
When I set a break point on the return statement it shows that there are 254 teams and every team has their games populated correctly...but the json result does not reflect. Here is an image:
I have tried doing this both synchronously and asynchronously but getting the same result. Do you know why I am only getting one result back in the json but at the breakpoint it has all of the results?