如何在实体数据源中的一对多关系上包含外键?
大约在页面的 2/3 处,您将看到以下内容:
OrderByExpression 元素指定结果集将按部门名称内的课程标题排序。请注意如何指定部门名称:Department.Name。由于 Course 实体和 Department 实体之间的关联是一对一的,因此 Department 导航属性包含 Department 实体。 (如果这是一对多关系,则该属性将包含一个集合。)要获取部门名称,您必须指定部门实体的 Name 属性。
如何为一对多关系设置集合?我在网上查了一圈,找不到答案。当我运行该页面时,我收到以下消息:
DataBinding: 'System.Data.Objects.DataClasses.EntityCollection`1[[QCPropertiesModel.Property, App_Code.s8yp4uy2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]' 不包含具有以下属性的属性命名为“DataPublished”。
这是我的代码:
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
Eval("Properties.DatePublished")
与PropertiesModel
建立一对多关系是不正确的,因为Properties
是PropertiesModel 的集合
因此它没有名为DatePublished
的属性。将拥有该财产。
You have one to many relationship with
PropertiesModel
usingEval("Properties.DatePublished")
is incorrect becauseProperties
is a collection ofPropertiesModel
so it doesn't have a property calledDatePublished
.will have that property.