With the 2010 Q2 release of Telerik OpenAccess ORM we introduced a lot of new features, including forward mapping capabilities for the visual designer. This makes the life much easier when it comes to defining a domain model which contains classes in a hierarchy. Until now it was only possible to use the vertical strategy for mapping a class hierarchy but in Q2 we have enabled the flat strategy as well. Any combination of flat and vertical mapping is also supported. This leaves the horizontal strategy as the last one to be supported, probably later this year. Now let's see how those things work. The starting point is a small database shown below:
Create a new project in Visual Studio and start the OpenAccess domain model wizard from the Telerik -> OpenAccess -> Add Domain Model... menu. Select the Populate from database option and advance to the next page. Note that you can start with a completely empty project if you want to use forward mapping from the very beginning.
Select the appropriate connection on page two, select all available tables on page three and finish the wizard.
The newly created diagram should look like this:
We are going to use vertical mapping for the Car and Train classes. To do that, their identities have to be removed first. Those classes will inherit the identity property of the base class (Vehicle). To delete the ID properties just select them in the diagram and press Delete. The next step is to drag two Inheritance relationship items from the toolbox so that the Car and Train classes actually derive from Vehicle. At the end the diagram would look as shown below.
At this stage you might get the following validation errors in the Error List window:
The last step is to set the inheritance strategy of the Car and Train classes to Vertical. This can be done from the Inheritance Mappings page of the Details Editor:
Note that this step should be performed separately for each derived class. The strategy of the base class should be left with the default value.
By default OpenAccess would use a discriminator column to determine which is the type of the records in the table of the base type. In this example however, no discriminator column is used so it should be disabled by setting the "{no}" string as a discriminator value for the Vehicle class. This has already been automated in the latest internal build of the product (2010.2.804.5) and it will be enough just to select "
At this point the model is ready, can be compiled and used:
using
(TransportEntityDiagrams context =
new
TransportEntityDiagrams())
{
Car car =
new
Car() { Seats = 5, MaxSpeed = 220, Color =
"Green"
, Price = 15000 };
Train train =
new
Train() { Seats = 1200, MaxSpeed = 110, Coaches = 10 };
context.Add(car);
context.Add(train);
context.SaveChanges();
foreach
(Vehicle vehicle
in
context.Vehicles)
{
Console.WriteLine(
string
.Format(
"Type: {0}, Max speed: {1}"
, vehicle.GetType().Name, vehicle.MaxSpeed));
}
}
In the next post more datails will be given about extending the model with new classes, using flat mapping and updating the database via the Update Database from Model wizard.