asp.net - How do I get the EntityDataSource to allow me to access child entities? -


I have a very simple mockup that I am creating a customer using the Northwind database I have a three Edmx file is: Product, Category, and Supplier.

I am trying to create a page that has a gridview that shows the name of the category and the supplier name. To SQL with LINQ I can return the LinqDataSource Control Product Unit and after that there can be a TemplateField in GridView like:

   & Lt; / ItemTemplate & gt;  

However, it seems that EntityDataSource is not playing well so it seems that it will not load lazy category data. I am a very simple EDS:

  & lt; ASP: EntityDataSource id = "dsProducts" runat = "server" ConnectionString = "name = NorthwindEntities" DefaultContainerName = "NorthwindEntities" EnableFlattening = "false" EntitySetName = "Product" & gt; & Lt; / ASP: EntityDataSource & gt;  

But Gridview does not show the name of the category. If I am creating a RoadAdbound event handler for GridView and I am binding the product unit in line, then I think the property category property does not return anything. For example, if I work:

  Protected sub gvProducts_RowDataBound (ByVal handles this form as, byVal e System.Web.UI.WebControls.GridViewRowEventArgs) gvProducts.RowDataBound then e.Row.RowType = DataControlRowType.DataRow then P NorthwindModel. Product = e.Row.DataItem dim catName = p.Category.CategoryName end then End Sub  Slow 

I get an NullReferenceException when trying to p.Category. .CategoryName

However, I work for the edmx loading lazy b / c If I type code in the Page_Load event handler like:

  Retardant New In the form of NorthwindModel. NorthwindEntities dim p = context.Products.Take (1). Single ()  

I can get the error without category p.category.CategoryName.

Is it? There are a few voodoo I need to do to obtain the support for EntityDataSource related organizations to retrieve

thanks

Solution: Include property I specify EntityDa TaSource, not including the unit objects to be included. Specifically, I updated the announcement markup of my EntityDataSource control in the following:

  & asp: EntityDataSource id = "dsProducts" runat = "server" ConnectionString = "name = NorthwindEntities" DefaultContainerName = Include "NorthwindEntities" EnableFlattening = "False" EntitySetName = "Product" = "Category, Supplier" & gt; & Lt; / ASP: EntityDataSource & gt; You need to include an entity data source's property in order to receive the related entities.  

Take a look at some pages from the "Programming Entity Framework" book:


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -