DateTime Property Configuration
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
When you want to configure a DateTime property, you need to use the DateTimePropertyConfiguration object. Besides the standard methods for primitive property configuration, the DateTimePropertyConfiguration class exposes a specific method named IsCalculatedOn. It allows you to specify if the property should be updated upon create or update operations.
The following code-snippet demonstrates you how to configure the HireDate property of the Person class to be backend calculated, i.e. during insert or update this field gets the actual date and time. Additionally, the following behaviors are possible:
- None - the DateTime field value is not changed during insert or update.
- Insert - during insert of a new object containing this DateTime field, the field gets the actual date and time.
- Update - during update of an existing object containing this DateTime field, the field gets the actual date and time.
- InsertAndUpdate - during insert and update of an object containing this DateTime field, the field gets the actual date and time.
DateTimePropertyConfiguration dateTimePropertyConfig = personConfiguration.
HasProperty( p => p.HireDate );
dateTimePropertyConfig.IsCalculatedOn( DateTimeAutosetMode.InsertAndUpdate );
Dim propertyConfiguration As DateTimePropertyConfiguration = personConfiguration. _
HasProperty(Function(p) p.HireDate)
propertyConfiguration.IsCalculatedOn(Telerik.OpenAccess.Metadata.DateTimeAutosetMode.InsertAndUpdate)
Person Class
public class Person
{
public int Id {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public DateTime HireDate {get;set;}
}
Public Class Person
Private _id As Integer
Public Property Id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Private _firstName As String
Public Property FirstName() As String
Get
Return _firstName
End Get
Set(ByVal value As String)
_firstName = value
End Set
End Property
Private _lastName As String
Public Property LastName() As String
Get
Return _lastName
End Get
Set(ByVal value As String)
_lastName = value
End Set
End Property
Private _hireDate As DateTime
Public Property HireDate() As DateTime
Get
Return _hireDate
End Get
Set(ByVal value As DateTime)
_hireDate = value
End Set
End Property
End Class
Sample FluentMetadataSource Implementation - DateTime Configuration
public class FluentModelMetadataSource : FluentMetadataSource
{
protected override IList<MappingConfiguration> PrepareMapping()
{
List<MappingConfiguration> configurations = new List<MappingConfiguration>();
MappingConfiguration<Person> personConfiguration = new MappingConfiguration<Person>();
personConfiguration.MapType( p => new
{
Id = p.Id,
FirstName = p.FirstName,
LastName = p.LastName,
HireDate = p.HireDate
} ).ToTable( "People" );
personConfiguration.HasProperty( p => p.Id ).IsIdentity( KeyGenerator.Autoinc );
DateTimePropertyConfiguration dateTimePropertyConfig = personConfiguration.
HasProperty( p => p.HireDate );
dateTimePropertyConfig.IsCalculatedOn( DateTimeAutosetMode.InsertAndUpdate );
configurations.Add( personConfiguration );
return configurations;
}
}
Public Class FluentModelMetadataSource
Inherits FluentMetadataSource
Protected Overrides Function PrepareMapping() As _
System.Collections.Generic.IList(Of Telerik.OpenAccess.Metadata.Fluent.MappingConfiguration)
Dim configurations As List(Of MappingConfiguration) = New List(Of MappingConfiguration)()
Dim personConfiguration As New MappingConfiguration(Of Person)()
personConfiguration.MapType(Function(p) New With {Key .Id = p.Id,
Key .FirstName = p.FirstName,
Key .LastName = p.LastName,
Key .HireDate = p.HireDate}).ToTable("People")
personConfiguration.HasProperty(Function(p) p.Id).IsIdentity(KeyGenerator.Autoinc)
personConfiguration.FieldNamingRules.AddPrefix = "_"
Dim propertyConfiguration As DateTimePropertyConfiguration = _
personConfiguration.HasProperty(Function(p) p.HireDate)
propertyConfiguration. _
IsCalculatedOn(Telerik.OpenAccess.Metadata.DateTimeAutosetMode.InsertAndUpdate)
configurations.Add(personConfiguration)
Return configurations
End Function
End Class