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

Using Default Mapping Functionality

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 use default mapping, the FluentMetadataSource takes care of the property to column mapping. On the other side, when you perform explicit mapping configuration you are explicitly specifying the property to column mapping. The explicit mapping gives you more control over the column naming, as well as it allows you to create partially mapped models. When you want to use the default mapping functionality the only thing you should do is to use the overload of the MapType method without parameters.

This tutorial teaches you how to use default mapping functionality, as follows:

Use Default Mapping

When you want to use the default mapping functionality the only thing you should do is to use the overload of the MapType method without parameters. The following example shows you how to configure the Person class.

MappingConfiguration<Person> personConfiguraiton = new MappingConfiguration<Person>();
personConfiguraiton.MapType().ToTable("People");
Dim personConfiguraiton As New MappingConfiguration(Of Person)()
personConfiguraiton.MapType().ToTable("People")

If you execute the DDL script for the previous example a new table named People with primary key Id will be created in the database. The table will have the following structure:

Fluent Mapping API allows you to map a class to more than one table in the database. Read more

Adding multiple mapping configurations for the same class in the PrepareMapping method will result in an InvalidOperationException.

Setting DataAccessKind

You could specify the DataAccessKind for the class, by using the WithDataAccessKind method. The DataAccessKind specifies the type of access for the entity. It could be:

  • Default - specifies that the default value for the data access kind (read/write) will be used by the runtime.
  • ReadWrite - the type allows full access to the data. All CRUD modifications are allowed.
  • InsertOnly - the type allows only reading and inserting operations. No modifications (update or delete) are allowed.
  • ReadOnly - the type is read-only. No CUD modifications (create, update or delete) are allowed.
MappingConfiguration<Person> personConfiguration = new MappingConfiguration<Person>();
personConfiguraiton.MapType().WithDataAccessKind(Telerik.OpenAccess.DataAccessKind.ReadOnly).ToTable("People");
Dim personConfiguration As New MappingConfiguration(Of Person)()
personConfiguration.MapType().WithDataAccessKind(Telerik.OpenAccess.DataAccessKind.ReadOnly).ToTable("People")

Setting ConcurencyControl and Version Member

You could specify the ConcurencyControl for the persistent, by using the WithConcurencyControl method. The version member could be specified by using the HasVersion method of the MappingConfiguration object. The HasVersion method specifies that the class will use an internal member for version control. The method returns a property configuration object that allows you to make an additional configuration for the internal version member. For example, you could specify the name of the column that the version member is mapped to. Usually the data type for the version column is int, and the version number is incremented on every update.

MappingConfiguration<Person> personConfiguration = new MappingConfiguration<Person>();
personConfiguration.MapType().
    WithConcurencyControl(Telerik.OpenAccess.OptimisticConcurrencyControlStrategy.Version).
    ToTable("People");
personConfiguration .HasVersion().ToColumn("Count");
Dim personConfiguration As New MappingConfiguration(Of Person)()
personConfiguration.MapType(). _
    WithConcurencyControl(Telerik.OpenAccess.OptimisticConcurrencyControlStrategy.Version). _
    ToTable("People")
personConfiguration .HasVersion().ToColumn("Count")

Setting CacheStrategy

You could specify the CacheStrategy for the persistent, by using the WithCacheStrategy method.

MappingConfiguration<Person> personConfiguration = new MappingConfiguration<Person>();
personConfiguration.MapType().WithCacheStrategy(CacheStrategy.Yes).ToTable("People");
Dim personConfiguration As New MappingConfiguration(Of Person)()
personConfiguration.MapType().WithCacheStrategy(CacheStrategy.Yes).ToTable("People")

Additional Property Attributes

When you use the default mapping functionality, you have the option to make an additional post-configuration of the model. For example, the following code-snippet demonstrates how to explicitly set the column name for the FirstName property of the Person class and how to configure the Id property as a primary key.

MappingConfiguration<Person> personConfiguration = new MappingConfiguration<Person>();
personConfiguration.MapType().ToTable("People");
personConfiguration.HasProperty(p => p.Id).IsIdentity();
personConfiguration.HasProperty(p => p.FirstName).ToColumn("FirstName");
Dim personConfiguration As New MappingConfiguration(Of Person)()
personConfiguraiton.FieldNamingRules.AddPrefix = "_"
personConfiguration.MapType().ToTable("People")
personConfiguration.HasProperty(Function(p) p.Id).IsIdentity()
personConfiguration.HasProperty(Function(p) p.FirstName).ToColumn("FirstName")

Person Class

public class Person
{
   public int Id {get;set;}
   public string FirstName {get;set;}
   public string LastName {get;set;}
   public string Address {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 _address As String
    Public Property Address() As String
        Get
            Return _address
        End Get
        Set(ByVal value As String)
            _address = value
        End Set
    End Property
End Class