String Properties
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.
This topic demonstrates how to describe string properties in backend independent manner. The Fluent Mapping API exposes the following methods that allow you to map string properties in an abstract way:
- WithFixedLength - specifies that the string property should be mapped using fixed length. This will map the property to a CHAR(n) or NCHAR(n) column, depending on the encoding.
- WithVariableLength - specifies that the string property should be mapped using variable length. This will map the property to a VARCHAR(n) or NVARCHAR(n) column, depending on the encoding.
- WithInfiniteLength - specifies that the string property should be mapped using infinite length. This will map the property to a VARCHAR(MAX) or NVARCHAR(MAX) column, depending on the encoding.
- IsUnicode - specifies that the string property should be mapped to a unicode column.
- IsNotUnicode - specifies that the string property should be mapped to a non-unicode column.
Using a combination of methods will allow you to get all of the possible types that map a string.
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.ProductName ).WithFixedLength(15).
IsUnicode().ToColumn("ProductName");
productConfiguration.HasProperty( p => p.Description ).WithVariableLength( 25 ).
IsNotUnicode().ToColumn( "Description" );
productConfiguration.HasProperty( p => p.Comments ).WithInfiniteLength().
ToColumn("Comments");
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.ProductName).WithFixedLength(15). _
IsUnicode().ToColumn("ProductName")
productConfiguration.HasProperty(Function(p) p.Description).WithVariableLength(25). _
IsNotUnicode().ToColumn("Description")
productConfiguration.HasProperty(Function(p) p.Comments).WithInfiniteLength(). _
ToColumn("Comments")
productConfiguration.FieldNamingRules.AddPrefix = "_"
configurations.Add(productConfiguration)
Return configurations
End Function
End Class
The result database table will have the following structure:
Product Class
public class Product
{
public int ID
{
get;
set;
}
public string ProductName
{
get;
set;
}
public string Description
{
get;
set;
}
public string Comments
{
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 _productName As String
Public Property ProductName() As String
Get
Return _productName
End Get
Set(ByVal value As String)
_productName = 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
Private _comments As String
Public Property Comments() As String
Get
Return _description
End Get
Set(ByVal value As String)
_comments = value
End Set
End Property
End Class