具有默认空值的数据绑定组合框 [英] Data-binding combobox with default null value

查看:53
本文介绍了具有默认空值的数据绑定组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个系统,其中有两个实体名称字段

字段有一个名为外来的属性,它是对实体的引用。

我正在尝试绑定一个ComboBox SelectedItem属性为字段实体的外部属性。一切正常,纠正值(外键)被写入数据库。

我不能做什么(我现在已经搜索了几个小时)是如何让用户不要选择一个对于外部属性,ComboBox中的值(通过在其中添加 DBNull 值)让EF在数据库中将该字段设置为NULL。我已经尝试过使用NullValue,DataSourceNullValue,玩绑定的Format和Parse事件,但没有成功。

我最终遇到两种情况:没有搞乱绑定中的Parse事件,我不能离开组合框。 EF类字段的setter事件甚至不会触发。

第二种情况,使用绑定上的Parse事件,我可以看到是否选择了DBNull值,然后将值设置为null。这种"有效"。但数据不会写入数据库。如果为该字段分配了值,则不会将其清除。此外,组合中显示的值不会保持为空值并恢复为之前的值。


任何建议?
问候

解决方案

Hello Rodrigo,

实体框架实际上不使用DBNull,而是使用null(或VB中的Nothing)来表示实体属性中的空值。为此,属性的类型必须是可空的(即,仅仅是诸如stirng或Nullable< T>类型的引用类型)。总而言之,如果不从近距离观察问题就很难提供帮助。请随意向microsoft dot com发送此问题的副本给diego dot vega。搜索结果,谢谢,结果,迭

Hi,

I have a system where, among other entities, there are two entities names table and field.

Field has a property named foreign which is a reference to a table entity.

I'm trying to bind a ComboBox SelectedItem property to this foreign property of the field entity. Everything works fine and the corrects values (foreign keys) are written to the database.

What I can't do (and I've searched for some hours now) is how to allow the user not to select a value in the ComboBox (by adding a DBNull value in it) for the foreign property and let the EF do his job setting the field as NULL in the database.
I've already tried using NullValue, DataSourceNullValue, playing with the binding Format and Parse events, but no sucess.

I end up in two situations: without messing with the Parse event in the binding, I can't leave the combobox. The setter event for the EF class field does not even trigger.

The second situation, using the Parse event on the binding, I can see if the DBNull value was selected and then set the value to null. This "works" but data is not written in the database. If there was a value assigned to the field, it is not cleared. Also the displayed value in the combo does not remain the null value and is restored to the previous value.


Any suggestions?
Regards

解决方案

Hello Rodrigo,

Entity Framework really doesn't use DBNull, but null (or Nothing in VB) to denote a null value in an property of an entity. For that, the type of the property has to be nullable (i.e. simply a reference type like stirng, or a Nullable<T> type). All that said, it is kind of difficult to help without looking at the problem from closer. Feel free to send me a repro of this issue to diego dot vega at microsoft dot com.

Thanks,
Diego


这篇关于具有默认空值的数据绑定组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆