如何实现多层“包含”在实体框架 3.5(VS 2008)中
我的数据库有两个表 - 问题和主题。为了实现多对多关系,有一个映射表,其结构如下:
Table TopicQuestionMapping
- int ID (主键)
- int QuestionID (问题表的外键)
- int TopicID (主题的外键 )表)
现在,在我的 EF 中,我得到了类似
ViewData.Model = DB.QuestionMaster.Include("TopicQuestionMapping").First(x => x.ID == id);
然后我尝试获取像
Model.TopicQuestionMapping.First().TopicMaster.Name
这样的主题(为了简化起见,我只考虑第一条记录)
查询填充TopicQuestionMapping(我得到的计数 = 1)。但 TopicMaster 为空。我可以让它工作吗?
就像表A引用表B。表B引用表C。我需要从表C获取数据。
My DB have two tables - Question and Topic. To implement many-to-many relation, there is a mapping table which has following structure:
Table TopicQuestionMapping
- int ID (Primary Key)
- int QuestionID (Foreign key to Question table)
- int TopicID (Foreign key to Topic table)
Now, in my EF I got something like
ViewData.Model = DB.QuestionMaster.Include("TopicQuestionMapping").First(x => x.ID == id);
and then I try to fetch topic like
Model.TopicQuestionMapping.First().TopicMaster.Name
(for simplification, I am just considering the first record)
The query populates the TopicQuestionMapping (I am getting count = 1). But the TopicMaster is null. Howe can I get it work?
It is something like Table A refer to Table B. Table B refer to Table C. I need to get data from Table C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Include
使用 .'s 来导航对象图。就像
.Include("TableA.TableB.TableC")
http://msdn.microsoft.com/en-us/library/bb896272.aspx
Include
uses .'s to navigate the object graph.So like
.Include("TableA.TableB.TableC")
http://msdn.microsoft.com/en-us/library/bb896272.aspx