【问题标题】:FormView, inserting null programmatically in OnItemInserting or OnItemUpdating eventFormView,在 OnItemInserting 或 OnItemUpdating 事件中以编程方式插入 null
【发布时间】:2013-06-28 13:16:59
【问题描述】:

我正在使用 FormView 控件,并且我想通过 FormViewUpdateEventArgs 参数中的 NewValues 字典以编程方式在 OnItemInserting 和 OnItemUpdating 事件中插入空值。但是如果我使用

protected void FormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
    e.NewValues["EntityFrameworkEntityPropertyName"] = null;
}

此类修改被忽略,数据库列不更新并保持其旧值。 EntityFramework 实体属性为:Type Int32、Nullable True、StoreGeneratedPattern None。

如果我通过 Bind() 将 TextBox 控件直接绑定到属性,则可以很好地插入空值。此外,如果值与 null 不同,则一切正常。

如何插入空值?

【问题讨论】:

    标签: asp.net entity-framework formview


    【解决方案1】:

    试试类似的东西

      e.NewValues["EntityFrameworkEntityPropertyName"] = System.DBNull.Value;
    

    【讨论】:

    • 这引发异常 DBNull 无法转换为 Int32 nillable(通过实体框架)。
    【解决方案2】:

    好的,我找到了解决方案。 OnItemUpdating 事件中的 DetailsViewFormView 比较以前的值和新值,如果这些值发生更改,请更新它。当我将 null 放入 e.NewValues 时,而我的 TextBox 不受 Bound 函数的限制,e.OldValues["EntityFrameworkEntityPropertyName"] 不存在(意味着 null)并且在 NewValues 中也为 null,所以列没有更新。

    我只是将e.OldValues["EntityFrameworkEntityPropertyName"] 设置为任何东西(但类型相同),现在插入空值可以正常工作。

    protected void FormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        e.OldValues["EntityFrameworkEntityPropertyName"] = -1
        e.NewValues["EntityFrameworkEntityPropertyName"] = null;
    }
    

    我的文本框没有直接限制,因为我需要先处理它的值,然后才能将它显示给用户。在插入/更新时,我还需要一些值处理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-03
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多