0

I have two tables Round and Game. A round has many games, and I'd like to get all of the games of a round using a Web API.

Here are my classes (simplified for the sake of this question)

namespace DataLayer.Entities
{
    [Table("Games")]
    public class Game : IEntity
    {   
        public Guid Id { get; set; }
        public int PlayerOneId { get; set; }
        public int PlayerTwoId { get; set; }
        public Guid RoundId { get; set; }
        [ForeignKey("RoundId")]
        public Round Parent { get; set; }
    }
}

namespace DataLayer.Entities
    {
      [Table("Rounds")]
      public class Round
      {     
         public ICollection<Game> Games { get; set; }          
         public Guid Id { get; set; }
         public bool HasStarted { get; set; }
         public DateTime TimeStarted { get; set; }
         public int RoundNumber { get; set; }
       }
    }

I'd like to be able to pass in a round ID using Entity Framework and get all of the Games from that round.

1 Answer 1

1

If you do not have lazy loading enabled, include the games contained in the round. If lazy loading is enabled, no include is needed.

The Games can be accessed by their navigation property (assuming the relationship is set up correctly in the EF Model).

Guid roundId;    
var roundWithGames = dbContext.Rounds.Include(r => r.Games).SingleOrDefault(r => r.Id == roundId);
if (roundWithGames == null) { throw new ArgumentException(nameof(roundId)); }
var gamesOfRound = roundWithGames.Games;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.