title: How do I get the EntityDataSource to allow me to access child entities?
body: <p>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.</p>
  
  <p>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:</p>
  
  <p></p>
  
  <pre><code>&lt;ItemTemplate&gt;
      &lt;%# Eval("Category.CategoryName") %&gt;
  &lt;/ItemTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p>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:</p>
  
  <pre><code>&lt;asp:EntityDataSource ID="dsProducts" runat="server" 
      ConnectionString="name=NorthwindEntities" 
      DefaultContainerName="NorthwindEntities" EnableFlattening="False" 
      EntitySetName="Products"&gt;
  &lt;/asp:EntityDataSource&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I get a NullReferenceException when trying to do p.Category.CategoryName.</p>
  
  <p>However, I know lazy loading works for the EDMX b/c if I write code in the Page_Load event handler like:</p>
  
  <pre><code>    Dim context As New NorthwindModel.NorthwindEntities
      Dim p = context.Products.Take(1).Single()
  </code></pre>
  
  <p>I can get the Category name via p.Category.CategoryName without error.</p>
  
  <p>Is there some voodoo I need to do to get the EntityDataSource to include support for retrieving related entities?</p>
  
  <p>Thanks</p>
  
  <p><strong>SOLUTION:</strong>
  I specified the Include property of the EntityDataSource, noting the entity objects to include. Specifically, I updated my EntityDataSource control's declarative markup to:</p>
  
  <pre><code>&lt;asp:EntityDataSource ID="dsProducts" runat="server" 
      ConnectionString="name=NorthwindEntities" 
      DefaultContainerName="NorthwindEntities" EnableFlattening="False" 
      EntitySetName="Products" Include="Category,Supplier"&gt;
  &lt;/asp:EntityDataSource&gt;
  </code></pre>