views:

106

answers:

1

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>
+1  A: 

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&amp;pg=PA284&amp;lpg=PA284&amp;dq=entitydatasource+include&amp;source=bl&amp;ots=cKtfB1J8vC&amp;sig=C--WGKuU-9CNOQgDxdN0MpSMLt4&amp;hl=en&amp;ei=OidPTMnKB5P-ngeM1rinBw&amp;sa=X&amp;oi=book_result&amp;ct=result&amp;resnum=7&amp;ved=0CDAQ6AEwBg#v=onepage&amp;q=entitydatasource%20include&amp;f=false

NYSystemsAnalyst
Thank you, thank you, thank you! For the record, I added the property Include="Category,Supplier" in the EntityDataSource's declarative markup.
Scott Mitchell