问题描述
|
我的数据库有两个表-Question和Topic。为了实现多对多关系,有一个映射表,其结构如下:
表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(我得到count = 1)。但是TopicMaster为空。我如何使它工作?
类似于表A引用表B。表B引用表C。我需要从表C获取数据。
解决方法
Include
使用。导航对象图。
就像.Include(\"TableA.TableB.TableC\")
http://msdn.microsoft.com/en-us/library/bb896272.aspx