NULL values and defaults
Some Entity fields are optional and aren't set to a value in all cases,
which makes them undefined or null. This makes the fields
nullable. Nullable fields often have a 'default' value set in the
database; this is a value which is inserted by the database server when
a NULL
is inserted in such a column. These default values are defined in
the table definition itself.
NULL values read from the database
If a field is NULL in the database, the in-memory value will be null / Nothing. This means that the Current Value of the field object in the entity's Fields collection will be null / Nothing in this situation, not a default value, and the field's property will return null / Nothing as well.
Setting a field to NULL
To set a field to null, in a new entity, simply don't provide a value for the field. The INSERT query will not set the corresponding table field with a value, as the entity field wasn't changed (because you didn't supply a value for it). If you have set a default value for that table field, the database engine will automatically fill in the default value for that field in the database.
If you want to set a field of an existing entity to NULL
, you first
fetch the entity from the database and after that you set the field's
property to null / Nothing. When the entity is saved after that, the
UPDATE query will set the corresponding table field to NULL
.
Example:
OrderEntity order = new OrderEntity(10254);
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
adapter.FetchEntity(order);
order.ShippingDate = null;
adapter.SaveEntity(order);
}
Dim order As New OrderEntity(10254)
Using adapter As New DataAccessAdapter()
adapter.FetchEntity(order)
order.ShippingDate = Nothing
adapter.SaveEntity(order)
End Using
You can also use one of the other methods to update an entity, e.g. to avoid the initial fetch.
To test whether a field is null, simply read the field's current value and test whether it's null / Nothing.
// if the field is a nullable field:
var isNullSimple = order.ShippingDate == null;
// if the field isn't nullable, and you want to check whether the current
// value is set to null:
var isNullNotNullable = order.Fields.GetCurrentValue((int)OrderFields.ShippingDate)==null;