开发者

Using EntityDataSoruce and nullable column. How do I set update the column in my db to null?

开发者 https://www.devze.com 2023-01-15 18:10 出处:网络
Can anyone reliably get the EntityDataSource to save a nullable column to the databse as a \"null\" when bound to any of the controls, such as \"FormView\"?

Can anyone reliably get the EntityDataSource to save a nullable column to the databse as a "null" when bound to any of the controls, such as "FormView"?

I have tried using s开发者_如何学Ceveral different UpdateParameters (SessionParameter, ControlParamater, Parameter, etc). I have tried setting "ConvertEmptyStringToNull" to true and leaving the property off entirely. Nothing works. On my "Inserts" it works fine (but obviously to insert a record, it has to insert "something" if there is no value.)

(I have made sure the column is set to nullable = true in the Entity Designer.....)

I have tried binding that column to a label and setting the text value of the label to "".

In all these cases, I can successfully set the column to a value, just not to NULL.


I have a similar problem.

The only workaround I just found was to reload the object in the ItemUpdated event. Check if the value I wanted to set to null is null and if it's the case set the value again to null manually. Like that:

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

This is the only way I could find a way to solve this. Also this seems to be an unusual problem. You are the only one I could find.

0

精彩评论

暂无评论...
验证码 换一张
取 消