I study Entity Framework Core and start by using the first code approach when reading this guide https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html , with the exception of different models.
In EF6, it had lazy loading, and I was able to easily pull related objects, but in EF Core it does not work. I was wondering how to do this, or if there is work to make it work.
Here is an example of how my two models look:
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; }
}
I want to get all the games for the team, but at the moment this is zero.
Web Api, TeamsController, GET , Games, .
:
[HttpGet]
public async Task<List<Team>> Get()
{
return await _context.Teams.Include(t => t.Games).ToListAsync();
}
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"
}
]
}
]
:
[HttpGet]
public async Task<List<Team>> Get()
{
return await _context.Teams.ToListAsync();
}
, Games null.
, . ?