Primitive Property Configuration
This topic demonstrates the most common method you will need to use when configuring value type properties.
- ToColumn
- IsNullable
- IsNotNullable
- IsVersion
- IsIdentity
- HasPrecision
- HasScale
- AsTransient
- WithDataAccessKind
- WithLoadBehavior
ToColumn
The ToColumn method specifies the column name that this property is mapped to.
The ToColumn method has a second overload that allows you to map a class to more than one table in the database. Read more
In the following example, the LastName property of the Person class is mapped to column with name Surname.
personConfiguration.HasProperty( c => c.LastName ).ToColumn( "Surname" );
personConfiguration.HasProperty(Function(c) c.LastName).ToColumn("Surname")
IsNullable
The IsNullable method specifies that the property is nullable. In the following example, the HireDate property of the Person class is configured as nullable.
personConfiguration.HasProperty( c => c.HireDate ).IsNullable();
personConfiguration.HasProperty(Function(c) c.HireDate).IsNullable()
IsNotNullable
The IsNotNullable method specifies that the property is not nullable. In the following example, the FirstName property of the Person class is configured as nullable.
personConfiguration.HasProperty( c => c.FirstName ).IsNotNullable();
personConfiguration.HasProperty(Function(c) c.FirstName).IsNotNullable()
IsVersion
The IsVersion method specifies that the property contains information about versioning. In the following example, the Timestamp property of the Person class is configured to contain information about versioning.
personConfiguration.HasProperty( c => c.Timestamp ).IsVersion();
personConfiguration.HasProperty(Function(c) c.Timestamp).IsVersion()
IsIdentity
The IsIdentity method specifies that the property is part of the identity key. For more information, please refer to Defining Identity.
HasPrecision
The HasPrecision method specifies the precision of the target property. In the following example, the precision for the Bonus property of the Person class is set to 10.
personConfiguration.HasProperty( c => c.Bonus ).HasPrecision( 3 ).HasScale( 10 );
personConfiguration.HasProperty(Function(c) c.Bonus).HasPrecision(3).HasScale(10)
HasScale
The HasScale method specifies the precision of the target property. In the following example, the scale for the Bonus property of the Person class is set to 3.
personConfiguration.HasProperty( c => c.Bonus ).HasPrecision( 10 ).HasScale( 3 );
personConfiguration.HasProperty(Function(c) c.Bonus).HasPrecision(10).HasScale(3)
AsTransient
The AsTransient method marks this property as transient. Transient properties don't have a corresponding database representation. In the following example, the Description property of the Person class is marked as transient.
personConfiguration.HasProperty(c => c.Description).AsTransient();
personConfiguration.HasProperty(Function(c) c.Description).AsTransient()
WithDataAccessKind
The WithDataAccessKind method specifies the data access kind of the property. For more information, please refer to Setting DataAccessKind.
WithLoadBehavior
The WithLoadBehavior method specifies the load behavior. It could be:
- Default - the default behavior. The runtime will decide whether to load it or not based on the type of the property.
- Lazy - the property will be lazily loaded.
- Eager - the property will be eagerly loaded.
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 Int64 Timestamp { get; set; }
public decimal Bonus { get; set; }
public string Description { 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
Private _timestamp As Int64
Public Property Timestamp() As Int64
Get
Return _timestamp
End Get
Set(value As Int64)
_timestamp = value
End Set
End Property
Private _bonus As Decimal
Public Property Bonus() As Decimal
Get
Return _bonus
End Get
Set(value As Decimal)
_bonus = value
End Set
End Property
Private _description As String
Public Property Description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
End Class
Sample FluentMetadataSource Implementation - Primitive Property 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,
Timestamp = p.Timestamp,
Bonus = p.Bonus,
Remarks = p.Description
} ).ToTable( "People" );
personConfiguration.HasProperty( p => p.Id ).IsIdentity( KeyGenerator.Autoinc );
personConfiguration.HasProperty( c => c.LastName ).ToColumn( "Surname" );
personConfiguration.HasProperty( c => c.HireDate ).IsNullable();
personConfiguration.HasProperty( c => c.FirstName ).IsNotNullable();
personConfiguration.HasProperty( c => c.Timestamp ).IsVersion();
personConfiguration.HasProperty( c => c.Bonus ).HasPrecision( 10 ).HasScale( 3 );
personConfiguration.HasProperty(c => c.Description).AsTransient();
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,
Key .Timestamp = p.Timestamp,
Key .Bonus = p.Bonus}).ToTable("People")
personConfiguration.HasProperty(Function(p) p.Id).IsIdentity(KeyGenerator.Autoinc)
personConfiguration.FieldNamingRules.AddPrefix = "_"
personConfiguration.HasProperty(Function(c) c.LastName).ToColumn("Surname")
personConfiguration.HasProperty(Function(c) c.HireDate).IsNullable()
personConfiguration.HasProperty(Function(c) c.FirstName).IsNotNullable()
personConfiguration.HasProperty(Function(c) c.Timestamp).IsVersion()
personConfiguration.HasProperty(Function(c) c.Bonus).HasPrecision(10).HasScale(3)
personConfiguration.HasProperty(Function(c) c.Description).AsTransient()
configurations.Add(personConfiguration)
Return configurations
End Function
End Class