EntityDataSource 空更新参数未标记为已修改

发布于 2024-10-18 21:12:20 字数 1996 浏览 5 评论 0原文

我在 VB.NET 应用程序上使用带有 FormView 的 EntityDataSource。 FormView 包含一个带有多个选项卡的 AjaxControlToolKit TabContains。由于每个选项卡都是一个命名容器,Bind 无法正常更新值(正如阅读 stackoverflow 上的其他帖子所发现的那样)。相反,我必须在 EntityDataSource 上声明 UpdateParameters。示例标记如下:

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

这非常有效,直到编辑客户并将其名称设置为空(假设在本例中允许使用空名称)。 Name UpdateParameter 设置为 Null,但 ObjectStateEntry 未设置为针对 Null 属性进行修改,即使之前实体已指定值。只要将名称更改为 Null 以外的其他名称,一切都会正确更新。

我找到了一种解决方法,将以下代码放入 EntityDataSource 的 Updating 事件中。

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

这可确保 UpdateParameters 中的每个属性都将其状态设置为已修改。它有效,但它看起来像是一个黑客,我可以看到它会在未来引起问题。我还有什么可以做的吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停滞 2024-10-25 21:12:20

您是否为相关实体设置了“并发模式”?根据您实际更新实体的方式(我没有使用 EntityDataSource,但我猜测它内部使用了 ObjectContext.Attach 方法),创建 SQL 语句的代码将尝试仅更新那些实际更新的列改变了。

请考虑以下情况:

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();
}

如果firstName 或lastName 为null,则ObjectContext 将假定它的原始值尚未被修改。这可能是您应该研究的事情。如果这没有帮助,我深表歉意,但它可能会将您推向正确的方向。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文