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

How to: Translate to Persistent Types

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.

This topic demonstrates how to translate a DbDataReader object to persistent types.

To run the samples in this topic, you need to use\import the System.Data, System.Data.Common and Telerik.OpenAccess.Data.Common namespaces.

Suppose, you have a domain model representing the Cars table from the SofiaCarRental database. The following example demonstrates how to materialize the DbDataReader instance to a collection of Car objects:

private static void TranslateDbDataReaderToPersistentTypes()
{
   // 1. Create a new instance of the OpenAccessContext.
   using ( EntitiesModel dbContext = new EntitiesModel() )
   {
       using ( IDbConnection connection = dbContext.Connection )
       {
           // 2. Create a new command.
           using ( IDbCommand command = connection.CreateCommand() )
           {
               // 3. Set the command text.
               command.CommandText = "Select * From Cars where Make = @CarMake";

               // 4. Create and initialize a new OAParameter.
               IDbDataParameter parameter = command.CreateParameter();
               parameter.ParameterName = "@CarMake";
               parameter.Value = "Honda";
               command.Parameters.Add( parameter );

               // 5. Execute command and translate data reader.
               using ( IDataReader reader = command.ExecuteReader() )
               {
                   IEnumerable<Car> cars = dbContext.Translate<Car>( reader as DbDataReader );
                   foreach ( Car car in cars )
                   {
                       Console.WriteLine( "Car Model: {0}", car.Model );
                       Console.WriteLine( "-----------------------------------------" );
                   }
               }
           }
       }
   }
}
Private Sub TranslateDbDataReaderToPersistentTypes()
    ' 1. Create a new instance of the OpenAccessContext.
    Using dbContext As New EntitiesModel()
        Using connection As IDbConnection = dbContext.Connection
            ' 2. Create a new command.
            Using command As IDbCommand = connection.CreateCommand()
                ' 3. Set the command text.
                command.CommandText = "Select * From Cars where Make = @CarMake"

                ' 4. Create and initialize a new OAParameter.
                Dim parameter As IDbDataParameter = command.CreateParameter()
                parameter.ParameterName = "@CarMake"
                parameter.Value = "Honda"
                command.Parameters.Add(parameter)

                ' 5. Execute command and translate data reader.
                Using reader As IDataReader = command.ExecuteReader()
                    Dim cars As IEnumerable(Of Car) = dbContext.Translate(Of Car)(TryCast(reader, DbDataReader))
                    For Each _car As Car In cars
                        Console.WriteLine("Car Model: {0}", _car.Model)
                        Console.WriteLine("-----------------------------------------")
                    Next _car
                End Using
            End Using
        End Using
    End Using
End Sub

Optionally, if the execution of the command is expected to take long time, you can set an appropriate value for the CommandTimeout property of command. By default, it is the one specified in Runtime Configuration. If the command exceeds the timeout it will be terminated and SQLException will be thrown.

In the previous code-snippet, you started off by creating a string with the SQL select statement. This statement contains one parameter: @CarMake. This is a placeholder that will be replaced by values when the statement is executed. Next, you create a single parameter that bind the placeholder name to specific values. For the select statement, you bind the value "Honda" to the @CarMake placeholder. To execute the SQL statement, you pass the string with the SQL statement to the CommandText property of the OACommand instance, and add the single OAParameter to the OAParameterCollection exposed by the command. Next you execute the command by calling the ExecuteReader method to get a data reader. With the reader in hand, you use the generic Translate method on the OpenAccessContext to materialize instances of the Car entity from the reader. In this example, it is assumed that the Car entity is part of your domain model.

Materialized persistent types are automatically attached to the context.

Materialized persistent types are automatically attached to the context. Namely, if you modify the materialized Car objects and call the SaveChanges method of the context, then all changes will be committed to the database.