Designer - Inheritance mapping
Preface
LLBLGen Pro offers full inheritance mapping starting with version 1.0.2005.1. To be able to map inheritance hierarchies in an easy, productive way, the designer
offers various ways to create inheritance hierarchies, which are discussed in the following paragraphs. It's important you've read
the section
Concepts - Entity inheritance and relational models so you're familiar with the names used in this section.
Not only the creation of hierarchies is discussed, also the destruction of (parts of) the hierarchies is described.
Creating hierarchies of type TargetPerEntity
To create a hierarchy of type TargetPerEntity, you have two options. One is the option to let LLBLGen Pro find all hierarchies of TargetPerEntity in the entities
in the project. To start this option, right-click
Entities in the project explorer and select
Construct 'Target-per-entity' Hierarchies as shown
in the following screenshot.
Creating all TargetPerEntity hierarchies in one go, before
After you've selected the option, LLBLGen Pro will find all hierarchies of type TargetPerEntity and will construct them for you. On the example database in the
screenshot, one hierarchy was found, Employee <- Manager <- BoardMember, Employee <- Clerk, which gives the following project explorer overview
Creating all TargetPerEntity hierarchies in one go, after
Which is the hierarchy shown in the screenshot for the Inheritance sub tab in the section
Adding and editing entities. The automatical construction of entity hierarchies rejects entities which are in a typed list so the user won't run into the situation where the hierarchy
has to be destroyed which isn't allowed for entities which are in a typedlist. A warning is shown which entities were rejected and why. A warning is also
shown when an entity is made a subtype of another entity manually and one or both are in a typed list.
Another option to create a hierarchy of type TargetPerEntity is to build the hierarchy per entity. You activate this option by right-clicking an entity and then
selecting the context menu option
Make Sub-type of which will show you an entity type which is a candidate to be a supertype for the currently selected
entity. See the screenshot below for an example.
Making an entity a subtype of another entity in a hierarchy of type TargetPerEntity
After selecting the suggested supertype from the context menu, LLBLGen Pro will create the hierarchy for you and make the selected entity in the project explorer
a subtype of the entity selected from the context menu.
Creating hierarchies of type TargetPerEntityHierarchy
Hierarchies of type TargetPerEntityHierarchy are all mapped, per hierarchy, onto the same target. This means that creating a hierarchy of type TargetPerEntityHierarchy
is done differently than the creation of hierarchies of type TargetPerEntity, as described in the previous paragraph. To create a hierarchy of type
TargetPerEntityHierarchy, you right-click in the project explorer the entity which will become the
supertype of a newly created subtype, and then
you select the option
Create Sub-type For This Entity from the context menu, as shown in the following screenshot
Creating a subtype of another entity in a hierarchy of type TargetPerEntityHierarchy
Selecting that option will bring up the following dialog which will allow you to specify the discriminator values and entity name for the subtype. If the
entity you right-clicked in the project explorer isn't in a hierarchy of type TargetPerEntityHierarchy, you also have to specify the discriminator value for
that entity.
Dialog for creating a subtype of another entity in a hierarchy of type TargetPerEntityHierarchy
The screenshot above shows that the discriminator column is
CarType, which is of type System.Int32. The selected discriminator column's type
is leading for which discriminator values are allowed. LLBLGen Pro will list all columns which have as .NET type byte/int16/int32/int64/Guid/Decimal and string.
All known types in the hierarchy are also shown in the dialog to help you specify unique values for the discriminator values. In the example above, the
hierarchy is new, and no types are defined yet.
By clicking
OK, the subtype will be created as a new entity, mapped onto the same target as the supertype, and the new entity is defined as a subtype
of the chosen supertype.
Note: |
Discriminator fields shouldn't be foreign key fields. The reason for this is that by setting a different related entity, the type of the entity could change because the discriminator field value changes, which is impossible as that would mean the entity object in memory should also change type dynamically. |
LLBLGen Pro will automatically
unmap in the supertype all fields which are nullable, if the supertype is the root of the hierarchy. This is to help you
setup the hierarchy in less time, as in TargetPerEntityHierarchy hierarchies, it's typical that fields mapped in subtypes are nullable in the target. No fields
are mapped in the newly created subtype, you've to specify them yourself in the entity editor. See
Designer - Adding and editing entities.
Besides right-clicking the supertype entity in the project explorer, you can also right-click an entity in the hierarchy view on an entity's Inheritance info tab,
in the entity editor. See for more details
Designer - Adding and editing entities.
Viewing hierarchies
After a hierarchy is created, you can view the hierarchy in the project explorer by clicking open the entity nodes of entities in the hierarchy and then by expanding
the Sub-types nodes as shown in the following screenshot.
Hierarchy in project explorer
Another way of viewing the hierarchy is to open the entity editor of one of the entities in the hierarchy, which will show the complete hierarchy in the Inheritance info
tab of the entity editor, as shown in this section:
Designer - Adding and editing entities
Destroying hierarchies
Creating hierarchies also means that from time to time, hierarchies have to be destroyed or parts of hierarchies have to be removed. Destroying a hierarchy isn't the
same as deleting an entity from the project: by deleting an entity from the project, it's automatically removed from the hierarchies it's in, and the subtypes of the
entity to delete are automatically removed from the hierarchy as well, however removing an entity from a hierarchy simply means that the entity isn't in the hierarchy
after the action has been completed, it will become a separate entity in the project.
An entity which isn't a
leaf in the hierarchy (i.e.: it has at least one subtype), can't be removed from the hierarchy alone: all its subtypes have to be removed
as well. To do so, you right-click in the project explorer the entity from which you want the hierarchy to be destroyed and select
Destroy Hierarchy, Starting
With This Entity from the context menu, as shown in the following screenshot.
Destroying a hierarchy
After selecting this option, all entities below the selected entity and the selected entity will be removed from the hierarchy they're in and will become separate
entities in the project. You can't undo this option. This option is also selectable from the context menu of the hierarchy viewer on the Inheritance info tab in the
entity editor.
If you right-click an entity which is a leaf, e.g. it doesn't have any subtypes, you can remove it from the hierarchy its in by simply selecting
Remove From Hierarchy, which will make it a separate entity in the project. You can't undo this option. If the leaf entity was in a hierarchy of type
TargetPerEntityHierarchy you can't re-add it to the hierarchy after the action.