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:
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.
The environment and code: using Visual Studio 2010, Entity Framework 4, ASP.NET web forms, and SQL Server 2008 Express.
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.
The part that works: here's the 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>
(Note: I've tried with EnableFlattening
on and off.)
This is linked to by a FormView
:
<asp:FormView ID="PartEditorFormView" runat="server"
DataSourceID="PartEntityDataSource"
DataKeyNames="PartId">
Note that I am forcing the FormView
into Edit
mode or Insert
mode depending on a query string parameters.
Without the DropDownList
, I am able to create and update records in the database.
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" />
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>
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: 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
I hope that is enough information!