如何在EF4 EntityDataSource中进入下一级关联?

时间:2011-03-03 16:37:48

标签: asp.net entity-framework-4 entitydatasource

我已经使用EF4 EntityDataSources成功地进行了DataBinding,但是这是我难倒的情况:

表“CurrentComplaintsByUsers”具有外键到“投诉”表。因此,在EF中,我可以访问CurrentComplaintByUser记录,并导航到其父投诉,如下所示:

var ccU = dataContext.CurrentComplaintsByUsers.First();
var parentComplaint = ccU.Complaint;

当我基于CurrentComplaintsByUsers绑定一个EntityDataSource绑定一个gridview时,我想从“投诉”对象转到一个名为“Status”的投诉协会。这是我的投诉表中的StatusID,所以在代码中我可以像这样投诉状态,包括状态属性:

string statusLabel = parentComplaint.Status.LabelText;

FAILURE: 当我尝试将此全部烘焙到EntityDataSource时,我无法从我的起始项“CurrentComplaintsByUsers”转到“状态”。这是我的EDS:

<asp:EntityDataSource ID="edsCurrentCases" runat="server" 
     ConnectionString="name=EOCaseDataModel" DefaultContainerName="EOCaseDataModel" 
     EnableFlattening="False" EntitySetName="CurrentComplaintsByUsers" 
     Include="Complaint, Status">
</asp:EntityDataSource>

我的错误是:

System.InvalidOperationException
"A specified Include path is not valid. 
The EntityType 'EOCaseApp.CurrentComplaintsByUser' does not declare a navigation property with the name 'Status'."

我正试图在Gridview中找到它的作用:

<%# ((CurrentComplaintsByUser)Container.DataItem).Complaint.Status.LabelText %>

所以,仅使用声明标记,如何从EDS获取相关对象关联? (我可以在代码中完成,只是在我可以的时候尝试使用EDS)

其他注意事项:如果我从EDS中的“Include”中删除“Status”,那么当我在Gridview中点击..Complaint.Status.LabelText时,我会得到一个NULL ERROR,几乎是你所期望的。 / p>

1 个答案:

答案 0 :(得分:1)

尝试将EntityDataSource中的Include="Complaint, Status"更改为Include="Complaint, Complaint.Status"