如何在实体数据源中的一对多关系上包含外键?

发布于 2024-12-02 03:02:27 字数 2044 浏览 2 评论 0原文

我正在完成本教程: http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet-%E2%80%93-getting-started-part-3

大约在页面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-12-09 03:02:27

使用 Eval("Properties.DatePublished")PropertiesModel 建立一对多关系是不正确的,因为 PropertiesPropertiesModel 的集合 因此它没有名为 DatePublished 的属性。

Properties[0].DatePublished

将拥有该财产。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文