Когда пользователь попадает в локацию, он получает вопрос.Таким образом, у меня есть класс для «Вопросы» и класс для «Местоположения».Однако когда я получаю местоположение, параметр Question всегда имеет значение null.Похоже, это проблема всего проекта, поскольку та же самая проблема повторяется где-то еще (здесь «Игра» имеет список «Команд», но команды всегда пусты).
Объекты создаются при инициализации базы данных:
public static void Initialize(DBContext context)
{
context.Database.EnsureCreated();
if (!context.Games.Any())
{
var teams = new List<Team>();
var team1 = new Team()
{
TeamName = "Kwizmasterz",
TotalPoints = 0,
TotalBoobyTraps = 2
};
var team2 = new Team()
{
TeamName = "Xesennettet",
TotalPoints = 0,
TotalBoobyTraps = 2
};
teams.Add(team1);
teams.Add(team2);
var game = new Game()
{
GameCode = "X35H0",
team = teams
};
context.Games.Add(game);
context.SaveChanges();
}
if (!context.Locations.Any())
{
var que = new Question()
{
QuestionText = "How much is 2 + 2?",
Answer = "4",
IsSolved = false,
Points = 1000000
};
var loc = new Location()
{
LocationName = "LocationName",
Latitude = 50.2299036,
Longitude = 5.4163052,
Question = que,
IsBoobyTrapped = false
};
context.Locations.Add(loc);
context.SaveChanges();
}
}
Класс местоположения:
public class Location
{
public int LocationID { get; set; }
public string LocationName { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public Question Question { get; set; }
public bool IsBoobyTrapped { get; set; }
public int VictorTeamID { get; set; } = -1;
}
Класс вопроса:
public class Question
{
public int QuestionID { get; set; }
public int QuestionType { get; set; } // 1 = Question - Answer
public string QuestionText { get; set; }
public int Points { get; set; }
public bool IsSolved { get; set; }
public string Answer { get; set; }
}
Класс контроллера:
[Route("api/v1")]
public class GameController : Controller
{
private readonly DBContext context;
public GameController(DBContext context)
{
this.context = context;
}
public IActionResult Index()
{
return View();
}
[Route("location")]
[HttpPost]
public IActionResult postGame([FromBody] Location newLocation)
{
newLocation.LocationID = context.Games.Count();
context.Locations.Add(newLocation);
return Created("", newLocation);
}
[Route("location")]
[HttpGet]
public List<Location> getLocations()
{
return context.Locations.ToList();
}
[Route("location/{id}")]
[HttpGet]
public Location getLocation(int id)
{
int _id = id - 1;
List<Location> loc = context.Locations.ToList();
if (loc[_id] != null)
return loc[_id];
else
return null;
}
[Route("game")]
[HttpPost]
public IActionResult postGame([FromBody] Game newGame)
{
newGame.GameID = context.Games.Count();
context.Games.Add(newGame);
return Created("", newGame);
}
[Route("game")]
[HttpGet]
public List<Game> getGames()
{
return context.Games.ToList();
}
[Route("game/{id}")]
[HttpGet]
public Game getGame(int id)
{
List<Game> game = context.Games.ToList();
if (game[id] != null)
return game[id];
else
return null;
}
}