Designer - Adding custom relations

Preface

LLBLGen Pro creates, from the found Foreign Key constraints and the Unique Constraints defined, per entity a set of relations. Often this is more than enough, however it can be that the database doesn't have any foreign key constraints defined. When that's the case, and legacy databases often lack foreign key constraints, a lot of LLBLGen Pro's functionality can't be used, simply because there are no relations defined. Starting with v1.0.2004.1, LLBLGen Pro contains functionality to allow you to design relations for entities, which are used as if they were relations created from Foreign Key constraints. Custom relations are visualized with the normal relation icon with a blue dot:

You can create these custom relations by using or the buttons on the Relations sub tab in the entity editor or by clicking with your right mouse button on the Relations child node of an entity in the Project Explorer. As there are two different types of relations: 1:1/1:n/m:1 vs. m:n, there are two different designers for these relations, one for each type. These designers are discussed below. Custom relations are required if you want to add relations between entities which are mapped onto tables in different catalogs (SqlServer).

Adding 1:1/1:n/m:1 relations

This is the designer you'll need the most if you plan to create your relations by hand using the designers. M:n relations are based on a 1:n and an m:1 relation so it is wise to start with this designer first. The logic behind the designer determines what the type of the relation is, so you can't make mistakes in that. It also enlists only related fields of the same type as the current field to relate, which also limits errors. A typical example is shown below.



Custom 1:1/1:n/m:1 relation editor


You first specify on which side the current entity is. In the example the AdminRightsPerAdministrator is a typical foreign key only entity, it relates two entities (AdminRight and Administrator). It has two relations, one with AdminRight and one with Administrator. In the screenshot, you see the relation with AdminRight in progress. We select the AdminRightId field in AdminRightsPerAdministrator, as that's the FK field for this relation. The dialog will automatically pre-select fields which have the same name, to speed up relation creation. The designer furthermore asks us to define the name for the field mapped on this relation, which is also pre-constructed for you using the related entity name, to speed-up relation creation. If we want new possible m:n relations automatically added as well and if we also want this relation to add to the related entity (AdminRight in this case). This last option is enabled by default if the user preference HideManyToManyRelationsOnCreation under the 'Catalog Refresher' section is set to false. On the right, a checkbox is checked to keep the editor open after you've clicked 'Create'. This is to save time when you're adding a lot of relations.

By clicking 'Create', the defined relation is first checked if it doesn't exist already. If it doesn't, the name for the field mapped onto this relation is also verified. If everything is OK, it is created, eventually new m:n relations and the opposite relation in the related entity is also created. The GUI is updated automatically with this new relation.

note 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.

There is no way to 'edit' a relation in a designer, as editing a relation would mean: create a different relation, discard the old one. Because relations can be present in Typed Lists, users first have to remove the old relation. Editing the field mapped on the relation can be done on the Fields mapped on relations sub tab.

Adding m:n relations

When there are a couple of 1:n and m:1 relations present in your project, you can add m:n relations. If you haven't checked the 'Automatically detect new m:n relations' checkbox when you added a new relation, it is possible that you actually can add an m:n relation which isn't present in the system. Below is a screenshot of the custom m:n relation editor:



Custom m:n relation editor


It lists the two relations required for the m:n relation to create. Because we created two relations from AdminRightsPerAdministrator, an m:n relation can be created between AdminRight and Administrator. The screenshot shows that relation. You again have the option to create the m:n relation also in the related entity, the name of the field mapped on this relation and the ability to keep the editor open to add multiple relations more easier.

Please note that selfservicing requires relations to be present at both sides (i.e.: Customer - Order and Order - Customer have to be present).

LLBLGen Pro v2.6 documentation. ©2002-2008 Solutions Design