2

I have an employee entity in my EF model. I then added a class to the project to add a custom property

public partial class Employee
{
    public string Name
    {
        get { return string.Format("{0} {1}", this.FirstName, this.LastName); }
    }
}

On a aspx form (inside a FormView), I want to bind a DropDownList to the employee collection:

                <asp:Label runat="server" AssociatedControlID="ddlManagerId"
                    Text="ManagerId" />
                <asp:DropDownList ID="ddlManagerId" runat="server" 
                    DataSourceID="edsManagerId" 
                    DataValueField="Id" 
                    DataTextField="Name" 
                    AppendDataBoundItems="true"
                    SelectedValue='<%# Bind("ManagerId") %>'>
                    <asp:ListItem Text="-- Select --" Value="0" />
                </asp:DropDownList>
                <asp:EntityDataSource ID="edsManagerId" runat="server" 
                    ConnectionString="name=Entities" 
                    DefaultContainerName="Entities" 
                    EntitySetName="Employees" 
                    EntityTypeFilter="Employee"
                    EnableFlattening="true">
                </asp:EntityDataSource>

Unfortunately, when I fire up the page, I get an error:

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

Any ideas what I'm doing wrong?

Alfero Chingono
  • 2,663
  • 3
  • 33
  • 54

3 Answers3

1

As per this article:

The issue is that we’re using the EntityDataSourceWrapper and not our actual entity. The solution? Stop using the wrapper! Disable flattening, like this:

<asp:EntityDataSource ... EnableFlattening="False" ... </asp:EntityDataSource>

More information on Flattening is here.

PeterX
  • 2,713
  • 3
  • 32
  • 42
1

After much searching I discovered that that the EntityDataSource does not support custom properties in the partial classes. It only returns the entity that is in the model.

Alfero Chingono
  • 2,663
  • 3
  • 33
  • 54
  • I just found this answer here `EnableFlattening="False"` [solution](http://blog.tentaclesoftware.com/archive/2012/09/06/databinding-system-web-ui-webcontrols-entitydatasourcewrapper-does-not-contain-a.aspx) / [more info on Flattening](http://msdn.microsoft.com/en-us/library/ee404746(v=vs.110).aspx) – PeterX Jan 06 '15 at 02:19
0

Could you verify that both your partial Employee classes are in the same namespace?

Steven K.
  • 2,097
  • 16
  • 15