如何获取 ADO.NET 实体模型中的外键?

发布于 2024-07-10 07:17:11 字数 889 浏览 11 评论 0原文

我有3个表(以及实体模型中的相应实体) 游戏: Id - 主键 ...其他栏目

玩家: Id-主键 ...其他栏目

GamePlayer(一个玩家可以参加很多游戏) 游戏ID --> 游戏中的外键 玩家ID --> 来自 Player 的外键 ...其他列

在我的代码中,我可以通过其他方式使用 gameId 和 playerId 。 使用这个我想知道玩家(playerId)是否正在参与特定游戏(gameId)。 所以我这样做: (entities 是我的上下文对象)

IQueryable query =Entity.GamePlayer.where(gp => ((gp.Game.Id == gameId) && (gp.Player.Id ==玩家ID))) 如果查询返回一行,那么我就知道玩家正在参与该游戏。

我阅读了多个有关实体引用的 MSDN 博客,但我很困惑。 看来 MSDN 建议首先我必须检查 EntityReference 对象的 IsLoaded,如果未加载,我必须加载该实体,只有这样我才应该在查询中使用它。

GamePlayer 确实有 GamePlayer.GameReference 和 GamePlayer.PlayerReference,但我无法检查引用是否已加载,因为我手中没有 GamePlayer 对象。 GamePlayer 表保存两个 1...* 关系,仅此而已。 我必须仅使用 GameId 和 PlayerId 查询 GamePlayer。 我在这里做错了什么?

我是否应该获取 Player(或 Game)对象(使用它们的 Id)并检查 GamePlayer 实体集合? Sql 就是这么简单。 如果这太天真了,抱歉,我很难将 sql 查询转换为实体查询。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦巷 2024-07-17 07:17:11

我认为你所做的很好。 据我所知,无论您在 lambda 表达式中使用什么,都不必显式加载。

我认为 MSDN 指的是如果您获取 GamePlayer 实体,然后访问关联实体,如下所示:

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

string gameName = gp.Game.name;

这会引发异常(您必须单独加载游戏或确保它像这样加载:entities.GamePlayer.Include("Game").First( g => g.id = 2))

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))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文