2010-09-13 16 views
0

任何人都可以可靠地获得EntityDataSource绑定到任何控件(如“FormView”)时将可空列保存为数据库的“空”?使用EntityDataSoruce和可为空的列。我如何设置更新我的数据库中的列为空?

我试过使用几个不同的UpdateParameters(SessionParameter,ControlParamater,Parameter等)。我已经尝试将“ConvertEmptyStringToNull”设置为true并完全关闭该属性。什么都没有在我的“插入”上它工作正常(但显然插入一条记录,如果没有值,它必须插入“something”。)

(我已经确定在实体中将列设置为nullable = true设计师.....)

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

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

回答

1

我有类似的问题。

我刚刚发现的唯一解决方法是重新加载ItemUpdated事件中的对象。检查我想要设置为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. 
       } 
      } 
     } 
    } 

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

相关问题