Question

I have 3 tables (and corresponding entities in the entity model) Game: Id - primay key ... other columns

Player: Id - primary key ... other columns

GamePlayer (a player can participate in many games) GameId --> foreign key from Game PlayerId --> foreign key from Player ... other columns

In my code, I have gameId and playerId available to me thru' other means. Using this I want to know if the player (playerId) is participating in a particular game (gameId). So I'm doing this: (entities is my context object)

IQueryable query = entities.GamePlayer.where(gp => ((gp.Game.Id == gameId) && (gp.Player.Id == playerId))) If the query returns a row, then I know that player is participating in that game.

I read multiple MSDN blogs on entity references and I'm confused. It appears that MSDN recommends that first I have to check the EntityReference object for IsLoaded and if not loaded, I have to load the entity and ONLY then I should use that in the query.

GamePlayer does have GamePlayer.GameReference and GamePlayer.PlayerReference, but I cannot check if reference is loaded because I dont have a GamePlayer object in hand. GamePlayer table holds the two 1...* relationships and thats about it. I have to query GamePlayer only using GameId and PlayerId. What am I doing wrong here?

Should I instead get the Player (or Game) object (using their Ids) and check the GamePlayer entity collection instead? Sql was so simple. If this is so naive, sorry, I'm having a tough time translating my sql query to entity queries.

Was it helpful?

Solution

I think what you're doing is fine. As far as I know, whatever you use in the lambda-expression doesn't have to be loaded explicitly.

I think what MSDN is referring to is if you would get a GamePlayer entity and then access an associated entity like this:

GamePlayer gp = entities.GamePlayer.First( g=> g.id = 2);

string gameName = gp.Game.name;

That would throw an exception (you would have to either load the game separately or make sure it is loaded like this: entities.GamePlayer.Include("Game").First( g => g.id = 2))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top