Data Access has been discontinued. Please refer to this page for more information.

String 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 string property, you need to use the StringPropertyConfiguration object. Besides the standard methods for primitive property configuration, the StringPropertyConfiguration class exposes a specific method named HasLength. It allows you to specify the length of this string property.

In the following example, the length of the FirstName property of the Person class is set to 5:

StringPropertyConfiguration stringPropertyConfig = 
    personConfiguration.HasProperty( p => p.FirstName );
stringPropertyConfig.HasLength( 5 );
Dim stringPropertyConfig As StringPropertyConfiguration = _
    personConfiguration.HasProperty(Function(p) p.FirstName)
stringPropertyConfig.HasLength(5)

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 - String 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 );

       StringPropertyConfiguration stringPropertyConfig = 
           personConfiguration.HasProperty( p => p.FirstName );
       stringPropertyConfig.HasLength( 5 );

       configurations.Add( personConfiguration );
       return configurations;
   }
}
Public Class FluentModelMetadataSource
    Inherits FluentMetadataSource
    Protected Overrides Function PrepareMapping() As _
        System.Collections.Generic.IList(Of 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 stringPropertyConfig As StringPropertyConfiguration = _
            personConfiguration.HasProperty(Function(p) p.FirstName)
        stringPropertyConfig.HasLength(5)

        configurations.Add(personConfiguration)
        Return configurations
    End Function
End Class