|
Article relates to
|
Telerik OpenAccess ORM
|
|
Created by
|
Damyan Bogoev
|
|
Last modified
|
December 22, 2009
|
|
Last modified by
|
Damyan Bogoev
|
DESCRIPTION
Setting default value to a DateTime variable is very useful due to the difference between the DateTime representation in the .NET and the MSSQL. The .NET DateTime value type represents dates and times with values ranging from 12:00:00 midnight, January 1, 0001 through 11:59:59 P.M., December 31, 9999. The DateTime data in the MSSQL is represented from January 1, 1753 through December 31, 9999. When no value is provided for a DateTime field of a persistent object, OpenAccess automatically uses the empty DateTime constructor to set a default value to that field before persisting it on the database server. When a new DateTime variable is created in the .NET environment via the default DateTime constructor it is assigned with the following value "1/1/0001 12:00:00 AM". When trying to push this value to the MSSQL server, an exception will be thrown because of the MSSQL DateTime handling that was mentioned above.
SOLUTION
Telerik OpenAccess ORM provides a mechanism for setting default value of datetime fields. For instance, a developer wants to store audit information in the database and instead of setting the audit date field in the code he/she would like to do that automatically.
Setting datetime field’s default value using the wizard
Run the Forward Mapping Wizard from the Telerik -> OpenAccess menu in Visual Studio. Locate and expand the class node. Click on the field node inside the expanded class node. On the right side, under the Field section, you could select the desired setting.
- Set field to current date during create
- Set field to current date during update

Setting field’s default value whose type is different than datetime
The Telerik OpenAccess ORM provides mechanism for setting default value only of the datetime fields. If you want to set default value to a field whose type is different than datetime you should set it inside the class constructor.
Please
Sign In
to rate this article.