尝试更改 EF4 中关系的多重性时出错
我正在尝试建立一个相对简单的游戏评论网站。游戏和评论之间应该是一对一的关系(每条评论针对一款游戏,并且一款游戏永远只有一条评论)。我的桌子非常简单。相关部分是:
评论表:
ReviewID - int,主键
文字 - 文字
GameID - int,来自游戏表的外键
游戏表:
GameID - int,主键
GameTitle - nvarchar(50)
EF4 不断将其映射为一对多关系,因为评论中存在外键。当我尝试手动将其更改为 1 对 1 时,出现以下错误:
错误 1 错误 113:多重性在关系“FK_Reviews_Games”中的角色“评论”中无效。由于从属角色属性不是关键属性,因此从属角色的重数上限必须为*。
我不确定错误试图说明什么,因为评论表中的外键 GameID 是 游戏表的主键。有什么想法吗?
I'm trying to build a relatively simple game review site. There should be a 1-to-1 relationship between games and reviews (each review is for one game, and there will only ever be one review for a game). My tables are pretty simple. The relevant parts are:
Reviews Table:
ReviewID - int, primary key
Text - text
GameID - int, foreign key from Games Table
Games Table:
GameID - int, primary key
GameTitle - nvarchar(50)
EF4 keeps mapping it as a 1-to-many relationnship given the existence of a foreign key in Reviews. When I try to change it to 1-to-1 manually, I get the following error:
Error 1 Error 113: Multiplicity is not valid in Role 'Reviews' in relationship 'FK_Reviews_Games'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.
I'm not sure what the error is trying to say as the foreign key GameID in the Reviews table is the primary key of the Games table. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定错误试图说明什么,因为 Reviews 表中的外键 GameID 是 Games 表的主键。
当然,因为它不是 Review 表的主键,并且为了 EF 在 Game 和 Review 实体之间建立 1:1 关联,它需要是。
所以,基本上你需要去掉 Review 表上的 ReviewID 并将 GameID 作为 Review 表的主键,然后 EF 会很乐意为你创建一个 1:1 的。
I'm not sure what the error is trying to say as the foreign key GameID in the Reviews table is the primary key of the Games table.
Of course because it is NOT the primary key of the Review table and in order for EF to make a 1:1 association between Game and Review entities, it needs to be.
So, basically you need to get rid of the ReviewID on Review table and make GameID to be the primary key of the Review table and then EF will happily create a 1:1 for you.