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

Field Name Rules

One of the most important characteristics of the Fluent Mapping API is that when you use normal properties (not auto-implemented), by default the Fluent Mapping API expects the corresponding field to have the same name, however in camel case. In the case when your field name has a different name than the expected one, an error will occur when you build your project. In this case you have to specify custom naming rules. Using naming rules is extremely appropriate when you have some naming conventions as well. The naming rules are applicable only for standard C#/VB properties. The reason for this is that the Fluent Mapping API could not extract the name of the field name. For auto-implemented properties, the Fluent Mapping API will automatically find the actual names of the different fields.

Using the HasFieldName Method

The HasFieldName method allows you to explicitly specify the field name that the target property relates to. In the following example the Id property of the Person class is mapped to field _Id, and the FirstName property is mapped to field with name _FirstName.

personConfiguration.HasProperty(p => p.Id).HasFieldName("_Id");
personConfiguration.HasProperty(p => p.FirstName).HasFieldName("_FirstName");
personConfiguration.HasProperty(Function(p) p.Id).HasFieldName("_id")
personConfiguration.HasProperty(Function(p) p.FirstName).HasFieldName("_firstName")

Custom Naming Rules

When all field names follow the same convention, it is appropriate to define custom naming rules, instead of using the HasFieldName method for each property. By default, if no naming rules are specified by the developer, the Fluent Mapping API will construct the field names in camel case. Which means that the leading character of each word except the first one is capitalized. However, VB is case-insensitive and this is not allowed - you cannot have a variable with the same name as another class member. That's why for VB classes (with normal properties), you have to explicitly specify naming rules.

Telerik.OpenAccess.Metadata.NamingRules namingRules = new Telerik.OpenAccess.Metadata.NamingRules();
namingRules.CaseMode = Telerik.OpenAccess.Metadata.CaseChangeModes.PascalCase;
namingRules.AddPrefix = "_";
personConfiguration.FieldNamingRules = new Telerik.OpenAccess.Metadata.NamingRules();
Dim namingRules As New Telerik.OpenAccess.Metadata.NamingRules()
namingRules.CaseMode = Telerik.OpenAccess.Metadata.CaseChangeModes.PascalCase
namingRules.AddPrefix = "_"
personConfiguration.FieldNamingRules = New Telerik.OpenAccess.Metadata.NamingRules()

The NamingRules class gives you the following options:

  • AddPrefix - adds a prefix to the name of the item.
  • AddSuffix - adds a suffix to the name of the item.
  • CaseMode - specifies the case mode for the item name. The following case modes are available:

    • Unchanged - the string is not changed.
    • Capitalize - the first letter of each word in the string is capitalized.
    • CamelCase - the leading character of each word except the first one is capitalized.
    • PascalCase - the leading character of each word is capitalized.
    • Lower - all letters are changed to lower case.
    • Upper - all letters are changed to upper case.
  • PluralizationMode - specifies the pluralization mode of the item name.

  • PluralizationModeCollections - specifies the pluralization mode for the collection property name.
  • RemovePrefix - removes the specified prefix from the item name.
  • RemoveSuffix - removes the specified suffix from the item name.
  • RemoveUnderscore - removes underscore symbols from the item name.
  • SchemaNameAsPrefix - adds the schema name as a prefix.
  • UnderscoreAsWordDelimiter - uses underscore as a word delimiter.

Person Class

public class Person
{
   private int _Id;
   public int Id
   {
       get
       {
           return this._Id;
       }
       set
       {
           this._Id = value;
       }
   }

   private string _FirstName;
   public string FirstName
   {
       get
       {
           return this._FirstName;
       }
       set
       {
           this._FirstName = value;
       }
   }
}
Public Class Person
    Private _id As Integer
    Public Property Id() As Integer
        Get
            Return Me._id
        End Get
        Set(ByVal value As Integer)
            Me._id = value
        End Set
    End Property

    Private _firstName As String
    Public Property FirstName() As String
        Get
            Return Me._FirstName
        End Get
        Set(ByVal value As String)
            Me._FirstName = value
        End Set
    End Property
End Class

Sample FluentMetadataSource Implementation - Setting FieldNamingRules

public class FluentModelMetadataSource : FluentMetadataSource
{
   protected override IList<MappingConfiguration> PrepareMapping()
   {
       List<MappingConfiguration> configurations = new List<MappingConfiguration>();
       MappingConfiguration<Person> personConfiguration = new MappingConfiguration<Person>();
       personConfiguration.MapType().ToTable( "People" );

       personConfiguration.HasProperty( p => p.Id ).HasFieldName( "_Id" );
       personConfiguration.HasProperty( p => p.FirstName ).HasFieldName( "_FirstName" );

       //personConfiguration.FieldNamingRules.AddPrefix = "_";
       //personConfiguration.FieldNamingRules.CaseMode = CaseChangeModes.PascalCase;

       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().ToTable("People")

        personConfiguration.HasProperty(Function(p) p.Id).HasFieldName("_id")
        personConfiguration.HasProperty(Function(p) p.FirstName).HasFieldName("_firstName")

        'personConfiguration.FieldNamingRules.AddPrefix = "_"
        'personConfiguration.FieldNamingRules.CaseMode = CaseChangeModes.PascalCase

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