How do I include a foreign key on a one-to-many relationship in an Entity Data Source?
I am working through this tutorial: http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet-%E2%80%93-getting-started-part-3
About 2/3 down the page, you will see this:
The OrderByExpression element specifies that the result set will be ordered by course title within department name. Notice how department name is specified: Department.Name. Because the association between the Course entity and the Department entity is one-to-one, the Department navigation property contains a Department entity. (If开发者_运维知识库 this were a one-to-many relationship, the property would contain a collection.) To get the department name, you must specify the Name property of the Department entity.
How do I setup a collection for my one-to-many relationship? I looked around on the net and couldn't find an answer. I get the following message when I run the page:
DataBinding: 'System.Data.Objects.DataClasses.EntityCollection`1[[QCPropertiesModel.Property, App_Code.s8yp4uy2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]' does not contain a property with the name 'DataPublished'.
This is my code:
<asp:EntityDataSource ID="PropertiesDataSource" runat="server"
ConnectionString="name=QCPlacesEntities"
DefaultContainerName="QCPlacesEntities" EnableFlattening="False"
EntitySetName="CustomerDetails"
include="Properties">
</asp:EntityDataSource>
<asp:QueryExtender ID="SearchQueryExtender" runat="server"
TargetControlID="PropertiesDataSource" >
</asp:QueryExtender>
<asp:GridView ID="SearchGridView" runat="server" AutoGenerateColumns="False"
DataKeyNames="CustomerID" DataSourceID="PropertiesDataSource" AllowPaging="true">
<Columns>
<asp:TemplateField HeaderText="Date Published">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Properties.DatePublished") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerID" HeaderText="ID"/>
<asp:BoundField DataField="CustomerLName" HeaderText="Last Name" />
<asp:BoundField DataField="CustomerFName" HeaderText="First Name" />
</Columns>
</asp:GridView>
You have one to many relationship with PropertiesModel
using Eval("Properties.DatePublished")
is incorrect because Properties
is a collection of PropertiesModel
so it doesn't have a property called DatePublished
.
Properties[0].DatePublished
will have that property.
精彩评论