如何获取 EntityDataSource 以允许我访问子实体?

发布于 2024-09-11 07:16:32 字数 1946 浏览 5 评论 0原文

我正在为使用 Northwind 数据库的客户制作一个非常简单的模型。我有一个包含三个实体的 EDMX 文件:产品、类别和供应商。

我正在尝试创建一个具有 GridView 的页面,用于显示产品,包括类别名称和供应商名称。使用 LINQ to SQL,我可以让 LinqDataSource 控件返回 Products 实体,然后可以在 GridView 中拥有 TemplateField,如下所示:

<ItemTemplate>
    <%# Eval("Category.CategoryName") %>
</ItemTemplate>

然而,EntityDataSource 似乎表现得不太好。就好像它不会延迟加载类别数据一样。我有一个非常简单的 EDS:

<asp:EntityDataSource ID="dsProducts" runat="server" 
    ConnectionString="name=NorthwindEntities" 
    DefaultContainerName="NorthwindEntities" EnableFlattening="False" 
    EntitySetName="Products">
</asp:EntityDataSource>

但是 GridView 不显示类别名称。如果我为 GridView 创建 RowDataBound 事件处理程序并将 Product 实体绑定到该行,我会看到产品的 Category 属性返回 Nothing。例如,如果我这样做:

Protected Sub gvProducts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProducts.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim p As NorthwindModel.Product = e.Row.DataItem
        Dim catName = p.Category.CategoryName
    End If
End Sub

当尝试执行 p.Category.CategoryName 时,我会收到 NullReferenceException。

但是,我知道如果我在 Page_Load 事件处理程序中编写如下代码,则延迟加载适用于 EDMX b/c:

    Dim context As New NorthwindModel.NorthwindEntities
    Dim p = context.Products.Take(1).Single()

我可以通过 p.Category.CategoryName 获取类别名称,不会出现错误。

我需要做一些巫术才能让 EntityDataSource 包含对检索相关实体的支持吗?

谢谢

解决方案: 我指定了 EntityDataSource 的 Include 属性,并注明要包含的实体对象。具体来说,我将 EntityDataSource 控件的声明性标记更新为:

<asp:EntityDataSource ID="dsProducts" runat="server" 
    ConnectionString="name=NorthwindEntities" 
    DefaultContainerName="NorthwindEntities" EnableFlattening="False" 
    EntitySetName="Products" Include="Category,Supplier">
</asp:EntityDataSource>

I have a very simple mockup I'm making for a client that uses the Northwind database. I have an EDMX file with three entities: Products, Categories, and Suppliers.

I'm trying to create a page that has a GridView that shows the products, including the category name and supplier name. With LINQ to SQL I can have the LinqDataSource control return the Products entity and then can have a TemplateField in the GridView like so:

<ItemTemplate>
    <%# Eval("Category.CategoryName") %>
</ItemTemplate>

However, it seems the EntityDataSource is not playing so nicely. It's as if it won't lazy load the category data. I have a very simple EDS:

<asp:EntityDataSource ID="dsProducts" runat="server" 
    ConnectionString="name=NorthwindEntities" 
    DefaultContainerName="NorthwindEntities" EnableFlattening="False" 
    EntitySetName="Products">
</asp:EntityDataSource>

But the GridView does not show the category name. If I create a RowDataBound event handler for the GridView and get the Product entity being bound to the row, I see that the product's Category property returns Nothing. For example, if I do:

Protected Sub gvProducts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvProducts.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim p As NorthwindModel.Product = e.Row.DataItem
        Dim catName = p.Category.CategoryName
    End If
End Sub

I get a NullReferenceException when trying to do p.Category.CategoryName.

However, I know lazy loading works for the EDMX b/c if I write code in the Page_Load event handler like:

    Dim context As New NorthwindModel.NorthwindEntities
    Dim p = context.Products.Take(1).Single()

I can get the Category name via p.Category.CategoryName without error.

Is there some voodoo I need to do to get the EntityDataSource to include support for retrieving related entities?

Thanks

SOLUTION:
I specified the Include property of the EntityDataSource, noting the entity objects to include. Specifically, I updated my EntityDataSource control's declarative markup to:

<asp:EntityDataSource ID="dsProducts" runat="server" 
    ConnectionString="name=NorthwindEntities" 
    DefaultContainerName="NorthwindEntities" EnableFlattening="False" 
    EntitySetName="Products" Include="Category,Supplier">
</asp:EntityDataSource>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无畏 2024-09-18 07:16:32

You need to use the Include property of the entity data source to get related entities. Then, change the markup to query into these entities.

Take a look a a couple of pages from the book "Programming Entity Framework": http://books.google.com/books?id=wp8V0vBebnoC&pg=PA284&lpg=PA284&dq=entitydatasource+include&source=bl&ots=cKtfB1J8vC&sig=C--WGKuU-9CNOQgDxdN0MpSMLt4&hl=en&ei=OidPTMnKB5P-ngeM1rinBw&sa=X&oi=book_result&ct=result&resnum=7&ved=0CDAQ6AEwBg#v=onepage&q=entitydatasource%20include&f=false

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