Handling Database Columns with Default Value
This article will show you how to handle database columns with specified default value when using Fluent Mapping.
When creating a Fluent Model from an existing database which has specified default values for some of the columns of its tables, Telerik Data Access will automatically detect this and add the HasDefaultValue extension method to the mapping configuration of the respective properties.
The HasDefaultValue method will not result in generation of SQL script for columns with default value in a Model First scenario.
For example consider a scenario where a database has a table named Users. This table has a column Age with a default value of 22.
When you create a Fluent Model from this database, the mapping configuration for the Age property of the persistent class User would look as the following code snippet. Notice the HasDefaultValue method towards the end of the configuration.
configuration.HasProperty(x => x.Age)
.HasFieldName("_age")
.WithDataAccessKind(DataAccessKind.ReadWrite)
.ToColumn("Age")
.IsNotNullable()
.HasColumnType("int")
.HasPrecision(0)
.HasScale(0)
.HasDefaultValue();
configuration.HasProperty(Function(x) x.Age).
HasFieldName("_age").
WithDataAccessKind(DataAccessKind.ReadWrite).
ToColumn("Age").
IsNotNullable().
HasColumnType("int").
HasPrecision(0).
HasScale(0).
HasDefaultValue()
Before being able to make use of the defined default value, you will need to make the Age property of the User class nullable. This needs to be done even if the Age column does not support null values. With such configuration, when adding to the database User objects which have their Age property set to null or not initialized at all, the default value of the Age column will be used.
private int? _age;
public virtual int? Age
{
get
{
return this._age;
}
set
{
this._age = value;
}
}
Private _age As Integer?
Public Overridable Property Age As Integer?
Get
Return Me._age
End Get
Set(ByVal value As Integer?)
Me._age = value
End Set
End Property
The default value of a column is respected only when adding a new object to the database. When updating the value passed for the respective property is always pushed to the database.
In case the property is not nullable, even if it is not explicitly initialized it will contain the default value for its CLR type. As a result this value will be pushed to the database and the specified default value for the column will not be respected.
Setting Null values to Nullable Columns with Specified Default Value
The approach described above is easy to implement and use but it has one drawback - it does not allow you to set null value to the Age column, even if the column is nullable. There is an easy solution to this problem. The following steps need to be performed:
- Remove the HasDefaultValue extension method from the mapping of the Age property.
- Step 1 will allow you to set null values to the Age column, but you will be unable to use its default value. To fix this create a parameterless constructor for the User class which initializes the Age property with the same value as the default one defined in the database.
public User()
{
this.Age = 22;
}
Public Sub New()
Me.Age = 22
End Sub