CreateNewRound() method is accessed by multiple threads at runtime. Let's say the _game.CurrentRound = 99 and two threads access the method at the same time and they both initialize the currentRoundId as 100 and both threads add two entities with the same roundId. But that is wrong and I don't want that to happen since rounds should be unique and different. How can I fix this so that thread one adds an entity with round 100 and the other with round 101.
public void CreateNewRound() { var game = _cache.GetGameById(_session.gameId); var currentRoundId = game.CurrentRound + 1; var response = SomeAPI.SomeCall(); if (response.responseCode == (int)responseCodes.Success) { _dbContext.GameState.Add(new GameState() { RoundId = CurrentRoundId }); _dbContext.SaveChanges(); } }