How to: Control the Collection Property Name
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 article will show you how to control the names of generated collection navigation properties in the parent class in a 1:m association.
The name of the collection navigation property can be based on:
- The name of the child class in the association
- A combination of the names of the child class and its foreign key property
Based on the name of the child class in the association
By default collection navigation property names are based on the name of the child class in the association. For the example, consider the schema of the Package-Location database:
- Create a new Telerik Data Access Class Library project.
- Choose the Populate from database option and click the Next button.
- Select the Package-Location database at the Setup Database Connection screen and click the Next button.
- At the Choose Database Items screen select both the Location and Package tables and click the Next button.
-
At the Define Naming Rules screen choose the Navigations tab. Notice that the Base Collection Name On setting is set to Class.
-
Click the Finish button to complete the wizard. The resulting Domain Model should look like this:
Notice the two collection navigation properties of the Package class - Packages and Packages1. Their names are based on the name of the child class Package.
Based on a combination of the names of the child class and its foreign key property
Now consider a scenario where the database schema has a second relationship:
In such scenario, if the names of the generated collection navigation properties are based on the name of the child class, they will become ambiguous - Packages and Packages1:
To prevent that you can instead use a combination of the names of the child class and its foreign key property. There are two options to chose from depending on your scenario - Class_ForeignKeyProperty and ForeignKeyProperty_Class. The example below demonstrates the ForeignKeyProperty_Class option.
- Create a new Telerik Data Access Class Library project.
- Choose the Populate from database option and click the Next button.
- Select the Package-Location database at the Setup Database Connection screen and click the Next button.
- At the Choose Database Items screen select both the Location and Package tables and click the Next button.
-
At the Define Naming Rules screen choose the Navigations tab. Set the Base Collection Name On setting to ForeignKeyProperty_Class. To achieve higher clarity of the resulting names, use Remove Suffix(es) from Foreign Key to remove the Id suffix.
-
Click the Finish button to complete the wizard. The resulting Domain Model should look like this:
We recommend you uncheck the Apply Property Naming Rules on the Reference Side checkbox when you are basing the names of the reference properties on the foreign key property.
Note that adding and removing prefixes and suffixes as well as pluralization settings apply only to the foreign key name which will be used when forming the collection navigation property name.
The two collection navigation properties of the Location class are now named SourceLocation_Packages and DestinationLocation_Packages - based on the combination of the names of the respective foreign keys of the child class (SourceLocationId and DestinationLocationId) and its name Package.
You may have noticed that in the current scenario, the names of the generated reference navigation properties have become ambiguous. To see how you can modify them, please refer to How to: Control the Reference Property Name