How to: Materialize Persistent Type
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 materialize 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 Car objects:
private static void MaterializePersistentTypes()
{
// 1. Create a new instance of the OpenAccessContext.
using ( EntitiesModel dbContext = new EntitiesModel() )
{
// 2. Set the command text.
const string SqlQuery = "Select * From Cars where 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<Car> cars = dbContext.ExecuteQuery<Car>( SqlQuery, parameter );
}
}
Private Sub MaterializePersistentTypes()
' 1. Create a new instance of the OpenAccessContext.
Using dbContext As New EntitiesModel()
' 2. Set the command text.
Const SqlQuery As String = "Select * From Cars where 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 cars As IEnumerable(Of Car) = dbContext.ExecuteQuery(Of Car)(SqlQuery, parameter)
End Using
End Sub
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.