我试图在存储库中使用以下代码检索嵌套对象:
我在这里阅读了一些问题,他们都建议这应该返回单个值。我需要为每个gamesTime
添加gameDay
属性。
GamesDays模型:
public class GameDay
{
public string Id { get; set; }
public string GamesDay { get; set; }
public string LeagueSessionScheduleId { get; set; }
public ICollection<GameTime> GamesTimes { get; set; } = new Collection<GameTime>();
}
GameTime模型:
public class GameTime
{
public string Id { get; set; }
public string GameDayId { get; set; }
public DateTime GamesTime { get; set; }
}
还有什么要做的吗?
它对我来说适用于其他模型,例如:
public async Task<List<SportType>> GetAllAsync(CancellationToken ct = default)
{
return await this._dbContext.SportTypes.Include(sportType => sportType.Leagues).ThenInclude(league => league.Teams).ToListAsync(ct);
}
SportType
模型:
public class SportType
{
public string Id { get; set; }
public string Name { get; set; }
public IEnumerable<League> Leagues { get; set; }
}