views:

334

answers:

1

I have two Entity classes: Order and OrderItem. Order contains a navigation property OrderItemSet of type

System.Data.Objects.DataClasses.EntityCollection<OrderItem>

On an aspx page is a FormView bound to this EntityDataSource:

<asp:EntityDataSource ID="EntityDataSourceOrder" runat="server" 
    ConnectionString="name=EntitiesContext" 
    DefaultContainerName="EntitiesContext" 
    EntitySetName="Order" 
    Include="OrderItemSet"

    // stuff to define a query

</asp:EntityDataSource>

The FormView is bound to the DataSource and the ItemTemplate of this FormView contains a ListView which I try to bind to the OrderItemSet. It looks this way:

<asp:FormView ID="FormViewOrder" runat="server" DataKeyNames="OrderID" 
              DataSourceID="EntityDataSourceOrder" AllowPaging="True" >
    <ItemTemplate>
        ...

        <asp:ListView ID="ListViewOrderItems" runat="server" 
                      DataSource='<%# Eval("OrderItemSet")%>' >
            ...
        </asp:ListView>
    </ItemTemplate>
</asp:FormView>

When I run the application I get an exception pointing to the line DataSource='<%# Eval("OrderItemSet")%>' in markup and telling me:

DataBinding: System.Web.UI.WebControls.EntityDataSourceWrapper does not contain a property with name 'OrderItemSet'

What is wrong here?

(I've done the same with other navigation properties which are not lists but single object references, and that works.)

Thank you for help!

+1  A: 

It seems to me that you are trying to evaluate a collection from within a datasource, without first binding to that datasource.

Why don't you try binding directly to the datasource? E.g.

<asp:ListView ID="ListViewOrderItems" runat="server" 
            DataSourceID="EntityDataSourceOrder"
...
</asp:ListView>
codemonkeh
Hello Xian, thanks for your reply! Actually my DataSource is bound to a FormView. And the ListView I am talking about is part of the ItemTemplate of this FormView. So I don't want to bind the ListView to the "full" DataSource of an order but only to a specific navigation property (a list of OrderItems) of an order. I have tried to clarify this in my question above.
Slauma
Perhaps the databinding of the FormView is occurring too late. You could try hooking into the OnDataBound event of the FormView and then set the DataSource of ListViewOrderItems programmatically.
codemonkeh
Thanks Xian! That's what I am actually doing now and it works. Perhaps you are right and the databinding is too late, but then the error message of the exception is quite confusing. I'll close this question now.
Slauma