I have a very simple layout that I am making for a client using the Northwind database. I have an EDMX file with three objects: Products, Categories and Suppliers.
I am trying to create a page with a GridView that shows products, including the category name and vendor name. With LINQ to SQL, I can force the LinqDataSource control to return a Products object, and then it can have a TemplateField in the GridView, for example:
<ItemTemplate>
<%# Eval("Category.CategoryName") %>
</ItemTemplate>
However, it seems that EntityDataSource does not play so beautifully. As if he would not be too lazy to load 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>
GridView . RowDataBound GridView Product , , Category 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
NullReferenceException p.Category.CategoryName.
, , EDMX b/c , Page_Load, :
Dim context As New NorthwindModel.NorthwindEntities
Dim p = context.Products.Take(1).Single()
p.Category.CategoryName .
- voodoo, , EntityDataSource ?
:
Include EntityDataSource, , . , EntityDataSource:
<asp:EntityDataSource ID="dsProducts" runat="server"
ConnectionString="name=NorthwindEntities"
DefaultContainerName="NorthwindEntities" EnableFlattening="False"
EntitySetName="Products" Include="Category,Supplier">
</asp:EntityDataSource>