asp.net web api2 - Entity Framework Web API - Get related entities using Foreign Key value -
i have 2 tables round
, game
. round has many games, , i'd of games of round using web api.
here classes (simplified sake of 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 able pass in round id using entity framework , of games round.
if not have lazy loading enabled, include
games contained in round. if lazy loading enabled, no include
needed.
the games
can accessed navigation property (assuming relationship set correctly in 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;
Comments
Post a Comment