具有EntityDataSource和DropDownList的FormView在实体上找不到“A属性名称”{0}“...” [英] FormView with EntityDataSource and DropDownList gives "A property names '{0}' was not found on the entity ..."

查看:214
本文介绍了具有EntityDataSource和DropDownList的FormView在实体上找不到“A属性名称”{0}“...”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 EntityDataSource FormView c>它有一个< asp:DropDownList> 链接到另一个表,页面加载正确的设置,但更新我收到此错误消息:

The problem: when I try to do an update using an EntityDataSource and a FormView that has an <asp:DropDownList> linking to another table, the page loads fine with the proper setting, but on update I get this error message:


在插入,更新或删除操作期间,实体上找不到名为Vendor.VendorId的属性。检查以确保指定为绑定表达式的属性可用于数据源。

环境和代码:使用Visual Studio 2010,实体框架4,ASP.NET Web表单和SQL Server 2008 Express。

The environment and code: using Visual Studio 2010, Entity Framework 4, ASP.NET web forms, and SQL Server 2008 Express.

我有两个表;一个部分表和一个供应商表。 Part 表有一个 VendorId 列,链接到供应商表,它被允许为 NULL 。它由Entity Framework包装。

I have two tables; a Part table and a Vendor table. The Part table has a VendorId column which links to the Vendor table, and it is allowed to be NULL. It is wrapped by Entity Framework.

可以使用的部分 EntityDataSource

<asp:EntityDataSource ID="PartEntityDataSource" runat="server" 
    ConnectionString="name=PartDBEntities" 
    DefaultContainerName="PartDBEntities" EnableFlattening="True" 
    EntitySetName="Parts" 
    EntityTypeFilter="Part" Where="it.PartId = @PartId" 
    EnableUpdate="True" EnableInsert="True" Include="Vendor">
    <WhereParameters>
        <asp:QueryStringParameter Type="Int64" DefaultValue="null" Name="PartId" 
            QueryStringField="partid" />
        </WhereParameters>
</asp:EntityDataSource>

(注意:我尝试过 EnableFlattening 开/关。)

(Note: I've tried with EnableFlattening on and off.)

这是通过一个 FormView 链接:

<asp:FormView ID="PartEditorFormView" runat="server"
    DataSourceID="PartEntityDataSource"
    DataKeyNames="PartId">

请注意,我强制将 FormView 编辑模式或根据查询字符串参数插入模式。

Note that I am forcing the FormView into Edit mode or Insert mode depending on a query string parameters.

没有 DropDownList ,我可以在数据库中创建和更新记录。

Without the DropDownList, I am able to create and update records in the database.

该部分不起作用:现在为 DropDownList 。首先有一个新的 EntityDataSource

The part that doesn't work: now for the DropDownList. First there is a new EntityDataSource:

<asp:EntityDataSource ID="VendorEntityDataSource" runat="server"
    ConnectionString="name=PartDBEntities"
    DefaultContainerName="PartDBEntities" EnableFlattening="False"
    EntitySetName="Vendors" />

然后$ $中的 DropDownList c $ c> EditTemplate ,使用在模板中转换空值字段

And then the DropDownList in the EditTemplate, using Converting Null in Template Fields:

<asp:DropDownList ID="EditVendorDDL" runat="server" 
    DataSourceID="VendorEntityDataSource" 
    DataTextField='CompanyName' 
    DataValueField='VendorId'
    AppendDataBoundItems="True"
    SelectedValue='<%# Bind("Vendor.VendorId") %>'>
        <asp:ListItem Selected="True" Value="">(none)</asp:ListItem>
</asp:DropDownList>

如上所述,初始页面显示正常; DropDownList 中有正确的条目,正确的供应商设置为正确的值。只需点击更新按钮就会导致上述错误。

As I said above, the initial page displays just fine; the DropDownList has the right entries in it and the correct vendor is set to the correct value. Just clicking on the Update button causes the above error.

以下是堆栈跟踪的相关部分:

Here is the relevant part of the stack trace:


[InvalidOperationException:在插入,更新或删除操作期间,实体上找不到名为Vendor.VendorId的属性。

检查以确保指定为绑定表达式的属性是
可用于数据源。]

System.Web.UI.WebControls.EntityDataSourceView.ConvertProperties(IDictionary
values,PropertyDescriptorCollection propertyDescriptors,
ParameterCollection referenceParameters,Dictionary`2 convertedValues)
+263 < br>
System.Web.UI.WebControls.EntityDataSourceView.ExecuteUpdate(IDictionary
键,IDictionary值,IDictionary oldValues)+256

System.Web.UI.DataSourceView.Update( IDictionary键,IDictionary值,IDictionary oldValues,DataSourceViewOperationCallback回调)+95

[InvalidOperationException: A property named 'Vendor.VendorId' was not found on the entity during an insert, update, or delete operation.
Check to ensure that properties specified as binding expressions are available to the data source.]
System.Web.UI.WebControls.EntityDataSourceView.ConvertProperties(IDictionary values, PropertyDescriptorCollection propertyDescriptors, ParameterCollection referenceParameters, Dictionary`2 convertedValues) +263
System.Web.UI.WebControls.EntityDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +256
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +95

希望这是足够的信息!

解决方案

不应该从PartEntityDataSource的角度看待选择的值,因为VendorEntityDataSource没有绑定整体数据绑定控件,所以你需要使用<% #Bind(VendorID)%>

Shouldn't the selectedvalue though be from the PartEntityDataSource perspective, because the VendorEntityDataSource isn't binding the overall data bound control, you need to use <%# Bind("VendorID") %> instead.

HTH。

这篇关于具有EntityDataSource和DropDownList的FormView在实体上找不到“A属性名称”{0}“...”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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