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

How to: Materialize Non-Persistent Types

This topic demonstrates how to materialize non-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.

Sometimes the shape of the result returned by the query may not map to any of the available persistent types in your model. In this case, you would need to define a custom CLR type that can hold the result. The requirements for this CLR type are:

  • It should provide a parameterless constructor, so that Telerik Data Access can create instances of that type.
  • The property names should match the column names that are returned by the query. The case is ignored. In case a corresponding property for a column is not found the value of this column is ignored.
  • The property types should be compatible with the column types.

In the following example, the Categories and Cars table from the SofiaCarRental database will be used.

private static void MaterializeNonPersistentTypes()
{
   // 1. Create a new instance of the OpenAccessContext.
   using ( FluentModel dbContext = new FluentModel() )
   {
       // 2. Set the command text.
       const string SqlQuery = "select car.CarID, car.Make, car.Model, category.CategoryName " +
                               "from Cars car " +
                               "join Categories category on car.CategoryID = category.CategoryID " +
                               "where car.Make = @CarMake";

       // 3. Create and initialize a new OAParameter.
       DbParameter parameter = new OAParameter()
       {
           ParameterName = "@CarMake",
           Value = "Honda"
       };

       // 4. Execute the sql command and materialize the result.
       IEnumerable<CarsDetails> carDetails = dbContext.ExecuteQuery<CarsDetails>( SqlQuery, 
            parameter );
   }
}
Private Sub MaterializeNonPersistentTypes()
 ' 1. Create a new instance of the OpenAccessContext.
 Using dbContext As New FluentModel()
  ' 2. Set the command text.
  Const SqlQuery As String = "select car.CarID, car.Make, car.Model, category.CategoryName " & _
   "from Cars car " &
   "join Categories category on car.CategoryID = category.CategoryID " &
   "where car.Make = @CarMake"

  ' 3. Create and initialize a new OAParameter.
  Dim parameter As DbParameter = New OAParameter With {.ParameterName = "@CarMake", .Value = "Honda"}

  ' 4. Execute the sql command and materialize the result.
  Dim carDetails As IEnumerable(Of CarsDetails) = dbContext.ExecuteQuery(Of CarsDetails)(SqlQuery, parameter)
 End Using
End Sub