EntityDataSource Null Update Pameters not getting marked Modified

1.9k views Asked by At

I am using an EntityDataSource with a FormView on VB.NET application. The FormView contains an AjaxControlToolKit TabContains with multiple tabs. Due to the fact that each tab is a naming container, Bind doesn't work properly for updating values (as discovered from reading other posts on stackoverflow). I instead have to declare UpdateParameters on my EntityDataSource. Example markup is as follows:

<asp:FormView ID="fv" runat="server" DataSourceID="eds" DataKeyNames="ID">
<EditItemTemplate>  
    <asp:TabContainer ID="tc" runat="server">
        <asp:TabPanel ID="tp" runat="server" HeaderText="Tab 1">
            <ContentTemplate>
                <asp:TextBox ID="tbName" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
            </ContentTemplate>
        </asp:TabPanel>
    </asp:TabContainer>
</EditItemTemplate> 
</asp:FormView>

<asp:EntityDataSource ID="eds" runat="server" ConnectionString="name=NDSEntities"
DefaultContainerName="NDSEntities" EnableFlattening="False" EntitySetName="Customers"
    Where="it.ID = @ID" EnableUpdate="true" EnableInsert="true">
    <WhereParameters>
        <asp:QueryStringParameter Name="ID" QueryStringField="ID" DbType="Guid" />
    </WhereParameters>
    <UpdateParameters>
        <asp:ControlParameter Name="Name" ControlID="fv$tc$tp$tbName" DbType="String" />
</UpdateParameters>
    <InsertParameters>
        <asp:ControlParameter Name="Name" ControlID="fv$tc$tp$tbName" DbType="String" />
</InsertParameters>
</EntityDataSource>

This works great, until a customer is edited and their name is set to nothing (assuming in this case, a null name is allowed). The Name UpdateParameter is set to Null but the ObjectStateEntry is not set to modified for Null properties, even if previously the Entity had a value specified. As long as the name is changed to something other than Null, everything is updated correctly.

I found a workaround by putting the following code in the Updating event of the EntityDataSource.

Dim ose As ObjectStateEntry = context.ObjectStateManager.GetObjectStateEntry(action)
For Each p As Parameter In eds.UpdateParameters
  ose.SetModifiedProperty(p.Name)
Next

This makes sure that each property in the UpdateParameters has its state set to modified. It works, but it seems like a hack and I can see it causing problems down the road. Is there anything else I could do?

1

There are 1 answers

1
J. Tihon On

Do you have an "Concurrency Mode" set for the entity in question? Depending on how you actually update the entity (I haven't used the EntityDataSource, but I'm guessing it internally uses the ObjectContext.Attach method), the code that creates the SQL statement, will try to update only those columns that are actually changed.

Consider the following:

void UpdatePersonEntity(int id, string firstName, string lastName)
{
    Person p = new Person { Id = id };
    this.Context.People.Attach(p);
    p.FirstName = firstName;
    p.LastName = lastName;

    this.Context.SaveChanges();
}

If firstName or lastName is null, the ObjectContext would assume that it's original value hasn't been touched. This might be something you should look into. I apologize if this isn't helpful, but it might push you in the right direction.