Decimal Properties
This topic demonstrates how to describe decimal properties in backend independent manner. The Fluent Mapping API exposes the following methods that allow you to map decimal properties in an abstract way:
- IsCurrency - specifies that this property is currency. This method will map a decimal property to Money in SQL Server and to NUMBER(19,4) in Oracle and backends that do not support the Money SQL Type.
- IsNumber - specifies that this property is a number. This method will map a decimal property to a number with custom scale and precision.
The following example demonstrates how to map the string properties of the Product class using the Backend Independent API.
public class FluentModelMetadataSource : FluentMetadataSource
{
protected override IList<MappingConfiguration> PrepareMapping()
{
List<MappingConfiguration> configurations = new List<MappingConfiguration>();
MappingConfiguration<Product> productConfiguration = new MappingConfiguration<Product>();
productConfiguration.MapType().ToTable( "Products" );
productConfiguration.HasProperty( x => x.ID ).IsIdentity( KeyGenerator.Autoinc );
productConfiguration.HasProperty( p => p.Price ).IsCurrency().ToColumn( "Price" );
productConfiguration.HasProperty( p => p.Rating ).IsNumber().ToColumn( "Rating" );
configurations.Add( productConfiguration );
return configurations;
}
}
Public Class FluentModelMetadataSource
Inherits FluentMetadataSource
Protected Overrides Function PrepareMapping() As IList(Of MappingConfiguration)
Dim configurations As New List(Of MappingConfiguration)()
Dim productConfiguration As New MappingConfiguration(Of Product)()
productConfiguration.MapType().ToTable("Products")
productConfiguration.HasProperty(Function(x) x.ID).IsIdentity(KeyGenerator.Autoinc)
productConfiguration.HasProperty(Function(p) p.Price).IsCurrency().ToColumn("Price")
productConfiguration.HasProperty(Function(p) p.Rating).IsNumber().ToColumn("Rating")
productConfiguration.FieldNamingRules.AddPrefix = "_"
configurations.Add(productConfiguration)
Return configurations
End Function
End Class
The result database table will have the following structure. Note that the Price property is mapped to a column of type money and the Rating property is mapped to a numeric column with custom scale and precision.
Product Class
public class Product
{
public int ID
{
get;
set;
}
public decimal Price
{
get;
set;
}
public decimal Rating
{
get;
set;
}
}
Public Class Product
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 _price As Decimal
Public Property Price() As Decimal
Get
Return _price
End Get
Set(ByVal value As Decimal)
_price = value
End Set
End Property
Private _rating As Decimal
Public Property Rating() As Decimal
Get
Return _rating
End Get
Set(ByVal value As Decimal)
_rating = value
End Set
End Property
End Class