Итак, я разрабатывал приложение WEB API с Angular на внешнем интерфейсе. У меня есть метод для поиска совпадений из базы данных:
[HttpGet("bySportByDateDifference/{sportID:int}/{dateDifferenceNumber:int}")]
public IEnumerable<Match> GetMatchesBySporByDateDifference([FromRoute] int sportID, [FromRoute] int dateDifferenceNumber)
{
IQueryable<Match> matches = Enumerable.Empty<Match>().AsQueryable();
switch (sportID)
{
case 1:
matches = _context.Matches
.Include(m => m.Sport)
.Where(m => m.Date.ToLocalTime().Date == DateTime.UtcNow.Date.AddDays(dateDifferenceNumber).Date
&& m.SportID == sportID);
break;
case 2:
matches = _context.Matches
.Include(m => m.BasketballMatchComponents)
.Where(m => m.Date.ToLocalTime().Date == DateTime.UtcNow.Date.AddDays(dateDifferenceNumber).Date
&& m.SportID == sportID);
break;
case 5:
matches = _context.Matches
.Include(m => m.IceHockeyMatchComponents)
.Where(m => m.Date.ToLocalTime().Date == DateTime.UtcNow.Date.AddDays(dateDifferenceNumber).Date
&& m.SportID == sportID);
break;
}
return matches;
}
Проблема, с которой я столкнулся, заключается в том, что свойства навигации имеют нулевой ответ.
Вот мой класс модели Match:
public class Match
{
/// <summary>
/// Match date.
/// </summary>
[Key]
public int ID { get; set; }
[Required(ErrorMessage = "Date has to be selected!")]
public DateTime Date { get; set; }
/// <summary>
/// Sport ID for the particular match.
/// </summary>
[ForeignKey("Sport")]
[Required(ErrorMessage = "Sport has to be selected!")]
public int SportID { get; set; }
/// <summary>
/// Competition ID for the particular match.
/// </summary>
[ForeignKey("Competition")]
[Required(ErrorMessage = "Competition has to be selected!")]
[Range(1, Int32.MaxValue, ErrorMessage = "Competition has to be selected!")]
public int CompetitionID { get; set; }
/// <summary>
/// ID of the home team.
/// </summary>
[ForeignKey("HomeTeam")]
[Required(ErrorMessage = "Home team has to be selected!")]
[Range(1, Int32.MaxValue, ErrorMessage = "Home team has to be selected!")]
public int HomeTeamID { get; set; }
/// <summary>
/// ID of the away team.
/// </summary>
[ForeignKey("AwayTeam")]
[Required(ErrorMessage = "Away team has to be selected!")]
[Range(1, Int32.MaxValue, ErrorMessage = "Away team has to be selected!")]
public int AwayTeamID { get; set; }
/// <summary>
/// Home team of the match.
/// </summary>
public virtual Team HomeTeam { get; set; }
/// <summary>
/// Away team of the match.
/// </summary>
public virtual Team AwayTeam { get; set; }
/// <summary>
/// Sport the match belongs to.
/// </summary>
public virtual Sport Sport { get; set; }
/// <summary>
/// Competition the match belongs to.
/// </summary>
public virtual Competition Competition { get; set; }
/// <summary>
/// Components of a football match.
/// </summary>
public virtual FootballMatchComponents FootballMatchComponents { get; set; }
/// <summary>
/// Components of a basketball match.
/// </summary>
public virtual BasketballMatchComponents BasketballMatchComponents { get; set; }
/// <summary>
/// Components of an ice hockey match.
/// </summary>
public virtual IceHockeyMatchComponents IceHockeyMatchComponents { get; set; }
}
Я пытался добавить .Include () к запросу, но это приводит к сетевой ошибке, когда приложение вызывает метод:
net :: ERR_HTTP2_PROTOCOL_ERROR 200
Я не уверен, что может быть причиной этого.
РЕДАКТИРОВАТЬ
Я нашел решение для этого.
https://docs.microsoft.com/en-us/ef/core/querying/related-data#related -данные -и-сериализация
Как упоминалось в статье, мне пришлось настроить Json. NET, чтобы игнорировать циклы, которые он находит в графе объектов.