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

How to: Map Your Model Using Explicit Mapping

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.

As it was described in the previous topic, the FluentMetadataSource class will hold the entire configuration for your classes and the PrepareMapping method will be your entry point for working with the Telerik Data Access FluentMapping API. Also, it is important to remember that the FluentMetadataSource class and your entities must be located in the same project.

Open the EntitiesModelMetadataSource class. The PrepareMapping method allows you to specify which types to be enhanced by the Telerik Data Access Enhancer. You have to create a mapping configuration for each type that will be enhanced and to add that configuration object to the collection returned by the method. All other types will be skipped by the enhancer and will be considered as transient, i.e. they will not have a database representation.

For this example, the generic MappingConfiguration<T> class is used. You need to use that class when you want to map real CLR types. The following example demonstrates how to create explicit mapping configurations for the Category and Product classes. Both entities were defined in the How to: Create a Fluent Mapping Library topic.

The non-generic MappingConfiguration class is used for artificial type definitions. For more information, please check out the Mapping Artificial Types section.

using System.Collections.Generic;
using Telerik.OpenAccess.Metadata.Fluent;
namespace Data
{
   public class EntitiesModelMetadataSource : FluentMetadataSource
   {
       protected override IList<MappingConfiguration> PrepareMapping()
       {
           List<MappingConfiguration> configurations = new List<MappingConfiguration>();

           MappingConfiguration<Product> productConfiguration = new MappingConfiguration<Product>();
           productConfiguration.MapType( p => new
           {
               ProductId = p.Id,
               ProductName = p.Name,
               Discontinued = p.Discontinued,
               CategoryId = p.CategoryId
           } ).ToTable( "Products" );
           productConfiguration.HasProperty( p => p.Id ).IsIdentity();

           MappingConfiguration<Category> categoryConfiguration = new MappingConfiguration<Category>();
           categoryConfiguration.MapType( p => new
           {
               CategoryId = p.Id,
               CategoryName = p.Name,
           } ).ToTable( "Categories" );
           categoryConfiguration.HasProperty( p => p.Id ).IsIdentity();

           configurations.Add( productConfiguration );
           configurations.Add( categoryConfiguration );
           return configurations;
       }
   }
}
Imports Telerik.OpenAccess.Metadata.Fluent
Public Class EntitiesModelMetadataSource
    Inherits FluentMetadataSource
    Protected Overrides Function PrepareMapping() As IList(Of MappingConfiguration)
        Dim configurations As List(Of MappingConfiguration) = New List(Of MappingConfiguration)()

        Dim productConfiguration As New MappingConfiguration(Of Product)()
        productConfiguration.MapType(Function(p) New With
                                                 {Key .ProductId = p.Id,
                                                  Key .ProductName = p.Name,
                                                  Key .Discontinued = p.Discontinued,
                                                  Key .CategoryId = p.CategoryId}).
                                          ToTable("Products")
        productConfiguration.HasProperty(Function(p) p.Id).IsIdentity()
        productConfiguration.FieldNamingRules.AddPrefix = "_"

        Dim categoryConfiguration As New MappingConfiguration(Of Category)()
        categoryConfiguration.MapType(Function(p) New With
                                                  {Key .CategoryId = p.Id,
                                                   Key .CategoryName = p.Name}).
                                           ToTable("Categories")
        categoryConfiguration.HasProperty(Function(p) p.Id).IsIdentity()
        categoryConfiguration.FieldNamingRules.AddPrefix = "_"

        configurations.Add(productConfiguration)
        configurations.Add(categoryConfiguration)

        Return configurations
    End Function
End Class

In the previous example, new MappingConfiguration<T> objects were created for the Product and Category types.

Adding multiple mapping configurations for the same class in the PrepareMapping method will result in an InvalidOperationException. If you create more than one mapping configuration for the same class in the PrepareMapping method, Telerik Data Access will throw an InvalidOperationException during runtime.

The MapType method specifies the property to column mapping for members of the persistent type. Finally, the ToTable method specifies the table name for this mapping configuration.

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

You may note that the MapType method has two overloads. The first one, which is used in the last example, accepts a lambda expression that allows you to specify the property to column mapping explicitly. The second overload, specifies that all of the property mappings will be handled by Telerik Data Access. When you use default mapping, the FluentMetadataSource takes care of the property to column mapping. Or in other words, the FluentMetadataSource takes care for the column names. When you use explicit mapping, you are explicitly specifying the property to column mapping, i.e. you are explicitly specifying the column names. The explicit mapping configuration gives you more control over the column naming, as well as it allows you to create partially mapped models.

The IsIdentity method is used to specify which property of the object should be used as a primary key.

Further References