在Entity Framework中绑定自定义属性

时间:2010-12-22 02:45:03

标签: webforms entity-framework-4

我的EF模型中有一个员工实体。然后我在项目中添加了一个类来添加自定义属性

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

在aspx表单上(在FormView中),我想将DropDownList绑定到employee集合:

                <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>

不幸的是,当我启动页面时,出现错误:

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

任何想法我做错了什么?

3 个答案:

答案 0 :(得分:1)

经过多次搜索后,我发现EntityDataSource不支持分部类中的自定义属性。它只返回模型中的实体。

答案 1 :(得分:1)

根据article

  

问题是我们使用的是EntityDataSourceWrapper而不是我们的实际实体。解决方案?停止使用包装器!   禁用展平,如下所示:

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

有关展平的更多信息是here

答案 2 :(得分:0)

您能否验证两个部分Employee类是否在同一名称空间中?

相关问题