将单独的属性链接到 Entity Framework / .NET MVC 中的同一个表
我花了几个小时试图找到这个问题的答案,但一无所获。
我对 .NET 和 MVC 完全陌生 - 我完成了 MvcMusicStore 教程,现在开始研究一个更复杂但类似的项目。我这么说只是为了解释我在这方面的经验和知识水平较低,希望任何回复都能考虑到这一点。
不管怎样,我遇到的问题是我有一个表“Fixtures”,它有两个属性“TeamId1”和“TeamId2”,它们都映射到另一个表“Team”。
我想要的是每个赛程都包含 Team1 和 Team2 的团队数据。到目前为止,我一直在使用 ObjectQuery 的 Include 方法来执行类似的操作,没有出现任何问题。
然而,我发现 .NET 或 MVC 或实体框架或其他什么,当我这样做时并不高兴。经过大量实验后,我认为只有当外键与其引用的表同名时它才会起作用。基本上我的属性必须具有名称“TeamId”,否则它将无法工作。如果我没有另一个也必须映射到该表的属性,这不会成为问题。显然,表中不能有两个同名的属性。所以我被困住了。
我只能假设可以使用“TeamId”以外的属性名称映射到表,但我找不到任何说明/上一篇文章/文档来解释如何执行此操作。
我考虑过分解我的固定装置数据并有一个固定装置表,然后是一个单独的表来保存球队 ID、分数等的可能性,但是考虑到每个固定装置永远不会有超过两支球队,这似乎有点矫枉过正。所以我称之为 B 计划。
如果可能的话,我不想回避这个问题 - 因为使用 EF 和 ObjectQuery 来解决这个问题似乎并不是不合理的。我还知道我可以手动创建查询,指定连接。不幸的是,我对 LINQ 的经验有限,到目前为止,我每次尝试时,一张表上的多个联接并没有成功。
因此,我真正需要知道的是,是否有一些设置或代码可以用来实现上述目的,而无需诉诸 LINQ 或重组我的 Fixtures 表。
编辑:
此图像显示相关型号。目前情况:这是可行的,但是查询显然只返回一个链接到“TeamId1”列的团队实例。注释的代码显示了我在模型中还需要的内容(就我之前有限的经验而言)。这样我的模型就有两个独立的团队链接到两个独立的列。
I have spent hours trying to find an answer to this, and have come up empty.
I am completely new to .NET and MVC - I completed the MvcMusicStore tutorial and have now started working on a more complex - but similar project. I only say this to explain my low level of experience and knowledge in this area, and hope that any responses can be considerate of this.
Anyway, the problem I am having is that I have one table "Fixtures" which has two attributes "TeamId1" and "TeamId2" these both map to another table "Team".
What I want is for each fixture to also include the Team data for both Team1 AND Team2. I have been using the Include method of ObjectQuery so far to perform similar operations with no problems.
However, I found that .NET or MVC or Entity Framework or whatever, wasn't happy when I did this. After MUCH experimentation, I figured that it will only work when the Foreign Key has the same name as the Table it refers to. Basically my attribute must have the name "TeamId" or it just won't work. This wouldn't be a problem if I didn't have another attribute which had to map to this table as well. I obviously can't have two attributes with the same name in a table. So I am stuck.
I can only assume that it is possible to map to a table using an attribute name other than "TeamId" but I can't find any instruction/previous post/documentation to explain how to do this.
I have considered the possibility of breaking my Fixture data up and having a Fixture table then a separate table to hold the team ids, scores etc, however this seems a bit of overkill given that each fixture will never have more than two teams. So I am calling this Plan B.
I'd rather not sidestep this problem, if possible - as it doesn't seem unreasonable for it to be possible using EF and ObjectQuery. I am also aware that I can create the queries manually, specifying the joins. Unfortunatley my experience of LINQ is limited and the multiple joins on one table hasn't worked out every time I've tried it so far.
So what I really need to know is if there is some setting or code I can use to achieve the above without resorting to LINQ or restructuring my Fixtures table.
Edit:
This image shows the relevant models. As it stands: this works, however queries obviously only return one Team Instance linked to the "TeamId1" column. The commented code shows what I need to also have in the model (as far as my limited previous experience has shown). So that my Model holds two separate teams linked to two separate columns.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在寻找的是一些自定义映射,而不是框架提供的“按惯例”映射。
如果您装饰并向您的灯具添加一些属性,它应该可以正常工作。
像这样:
I think what you're looking for is some custom mappings rather than the 'by convention' mappings that are supplied with the framework.
If you decorate and add some properties to your Fixture it should work fine.
Like this: