实体框架中绑定自定义属性 [英] Binding custom property in Entity Framework

查看:142
本文介绍了实体框架中绑定自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在一个aspx窗体(FormView内)我想绑定一个DropDownList到员工集合:

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

不幸的是,当我启动该页面时,我收到一个错误:

  DataBinding:'System.Web.UI.WebControls.EntityDataSourceWrapper'不包含名称为Name的属性。 

任何想法我做错了什么?

解决方案

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


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?

解决方案

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.

这篇关于实体框架中绑定自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆