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

How to: Consume Fluent Mapping Model

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.

You need a project that will consume your data project. For the sake of simplicity, add to your solution a new Console Application and name it Client. You might be asking why you created a separated project for your classes. The answer is simply because you want to compile your classes into a separate and distinct assembly from the UI project.

Add the following references to the Client project:

  • Telerik.OpenAccess.dll
  • Telerik.OpenAccess.35.Extensions.dll
  • Data.dll - reference to the data project

The last step is to ensure that the Client project "has" the ability to know about the domain model, i.e. to have an access to the database connection string used by the rlinq file. What you normally do when you have an existing config file in the client project is to copy the connection string from the App.config in the library project and paste it in the <connectionStrings/> section in the client config file.
If you don't have an existing config file in the client project, you simply could copy and paste the entire config file. The reason behind this step is that the Client project is your executable project. When you run the application, all settings (configurations) are taken from that project. Respectively, the OpenAccessContext will try to retrieve the connection string from the config file in the executable project. If such doesn't exist, the initialization of the OpenAccessContext will fail.

So, these are the three important pieces you need in order to consume your domain model:

  • Reference to the library project.
  • References to Telerik.OpenAccess.dll and Telerik.OpenAccess.35.Extensions.dll.
  • Copy and paste the connection string from the library project to the consuming project.

Additional step you need to consider is making the client (consuming) project a start up project. Right-click the client (consuming) project in Solution Explorer and select Set as Startup Project.

Migrating Your Database

The following code-snippet calls the UpdateSchema method of the context to migrate the backend database to the latest model state.

using ( EntitiesModel dbContext = new EntitiesModel() )
{
   dbContext.UpdateSchema();
}
Using dbContext As New EntitiesModel()
    dbContext.UpdateSchema()
End Using

Performing CRUD Operations

You can use the context class for CRUD operations as well.

using ( EntitiesModel dbContext = new EntitiesModel() )
{
   dbContext.UpdateSchema();
   Product p = new Product
                   {
                       Id = 1,
                       Name = "MyProduct"
                   };

   Category c = new Category
                       {
                           Id = 1,
                           Name = "MyCategory"
                       };

   p.Category = c;
   dbContext.Add( p );
   dbContext.Add( c );

   dbContext.SaveChanges();

   foreach ( Product item in dbContext.Products )
   {
       Console.WriteLine( item.Name );
   }
}
Using dbContext As New EntitiesModel()
    dbContext.UpdateSchema()

    Dim p As Product = New Product With
                        {.Id = 1,
                        .Name = "MyProduct"}

    Dim c As Category = New Category With
                        {.Id = 1,
                         .Name = "MyCategory"}

    p.Category = c
    dbContext.Add(p)
    dbContext.Add(c)

    dbContext.SaveChanges()

    For Each item As Product In dbContext.Products
        Console.WriteLine(item.Name)
    Next item
End Using