【问题标题】:Using EntityDataSoruce and nullable column. How do I set update the column in my db to null?使用 EntityDataSource 和可为空的列。如何将数据库中的更新列设置为空?
【发布时间】:2011-04-04 21:19:42
【问题描述】:

当绑定到任何控件(例如“FormView”)时,任何人都可以可靠地获取 EntityDataSource 以将可为空的列作为“null”保存到数据库中吗?

我尝试过使用几种不同的 UpdateParameters(SessionParameter、ControlParamater、Parameter 等)。我尝试将“ConvertEmptyStringToNull”设置为 true 并完全关闭该属性。没有任何效果。在我的“插入”上它工作正常(但显然要插入一条记录,如果没有价值,它必须插入“某物”。)

(我已确保在实体设计器中将该列设置为 nullable = true .....)

我已尝试将该列绑定到标签并将标签的文本值设置为“”。

在所有这些情况下,我都可以成功地将列设置为一个值,而不是 NULL。

【问题讨论】:

    标签: .net .net-4.0 entity-framework-4


    【解决方案1】:

    我也有类似的问题。

    我刚刚找到的唯一解决方法是在 ItemUpdated 事件中重新加载对象。检查我想设置为 null 的值是否为 null,如果是这种情况,请手动将值再次设置为 null。像这样:

    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            // Use the Exception property to determine whether an exception
            // occurred during the insert operation.
            if (e.Exception != null)
            {
                // handle the exception
            }
            else
            {
                //force the suppression of the foreign key relation
                using (var context = new ProxiEntities())
                {
                    //reload my object
                    var id = Convert.ToInt32(e.Keys["SessionID"]);
                    var session = (from o in context.Sessions
                                   where o.SessionID == id
                                   select o).FirstOrDefault();
    
                    try
                    {
                        //check if I wanted to set a value to null
                        if (e.NewValues["MagasinID"] == null)
                        {
                            //if yes, set it to null and save
                            session.MagasinID = null;
                        }
                        if (e.NewValues["LieuID"] == null)
                        {
                            session.LieuID = null;
                        }
                        context.SaveChanges();
                    }
                    catch (Exception)
                    {
                        //Add code to log the error.
                    }
                }
            }
        }
    

    这是我能找到解决这个问题的唯一方法。这似乎也是一个不寻常的问题。你是我唯一能找到的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 2020-11-21
      相关资源
      最近更新 更多