How to: Materialize Persistent Type
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 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 ( FluentModel dbContext = new FluentModel() )
{
// 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 FluentModel()
' 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.