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

How to: Create a Fluent Mapping Library

Telerik Data Access provides a Fluent Mapping API, also known as code-first, for defining data models using only code. The Fluent Mapping API gives developers complete control over the model mapping configuration, and schema management. This topic demonstrates how to get started by creating a new class library for your model using the Telerik Data Access NuGet Packages for Code First Development.

In this tutorial, you will also define two sample classes that will be used in the next topics. Read more

Using the Telerik Data Access NuGet Packages for Code First Development

Telerik Data Access provides NuGet packages that make it easy to get started building a model using Fluent Mapping API.

The first thing you need to do is to create a new Class Library Project. Once the project is created it is time to add the NuGet package by using the Package Manager Console (Tools > Library Package Manager > Package Manager Console):

  • Install-Package Telerik.DataAccess.Fluent or
  • Install-Package Telerik.DataAccess.Sample

The Telerik.DataAccess.Fluent package sets up the project for code first development with Telerik Data Access. It will add code to the project file which integrates the project with the Telerik Data Access enhancer, and it will add all required assembly references. Other than that, the project will look the same as it did before installing the package.

The Telerik.DataAccess.Sample package adds additional classes to the project which help you to get the basic code first project skeleton set up. It will also create an example entity, and an example mapping configuration for that entity.

The next step is to define sample entities that will be used in the next topics.

Defining Sample Classes

Add two classes named Category and Product in the sample project.

Open it and replace the code in the Category class with the following:

using System.Collections.Generic;
namespace Data
{
   public class Category
   {
       public Category()
       {
           this.Products = new List<Product>();
       }
       public int Id
       {
           get;
           set;
       }
       public string Name
       {
           get;
           set;
       }
       public IList<Product> Products
       {
           get;
           set;
       }
   }
}
Public Class Category
    Public Sub New()
        Me.Products = New List(Of Product)()
    End Sub

    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 _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _products As IList(Of Product)
    Public Property Products() As IList(Of Product)
        Get
            Return _products
        End Get
        Set(ByVal value As IList(Of Product))
            _products = value
        End Set
    End Property
End Class

Now for the Product class. Open it and replace the code in that class with the following:

namespace Data
{
   public class Product
   {
       public int Id
       {
           get;
           set;
       }

       public string Name
       {
           get;
           set;
       }

       public bool Discontinued
       {
           get;
           set;
       }

       public Category Category
       {
           get;
           set;
       }

       public int CategoryId
       {
           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 _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Private _discontinued As Boolean
    Public Property Discontinued() As Boolean
        Get
            Return _discontinued
        End Get
        Set(ByVal value As Boolean)
            _discontinued = value
        End Set
    End Property

    Private _category As Category
    Public Property Category() As Category
        Get
            Return _category
        End Get
        Set(ByVal value As Category)
            _category = value
        End Set
    End Property

    Private _categoryId As Integer
    Public Property CategoryId() As Integer
        Get
            Return _categoryId
        End Get
        Set(ByVal value As Integer)
            _categoryId = value
        End Set
    End Property
End Class

At this point you are pretty much done with your data project. You haven't defined any relationships or configuration items, but you will do that shortly in the same project. In the next two topics, you will see how to configure your model by using Explicit Mapping and Default Mapping.

You can create and migrate your database with the help of the UpdateSchema method in the How to: Create/Migrate Your Database topic.