Overriding the Default Mapping
In greenfield development, it’s a good idea to keep to the default mapping. This ensures that terms are used consistently across the domain model and the database, so that everyone is using the same language and confusion is avoided. In brownfield development, this often isn’t possible, and even in greenfield projects there may be cases where the default mapping is problematic.
Mapping Individual Tables and Columns
The most common scenario for overriding the default mapping is that you are working with an existing database whose terminology you don’t want to carry forward into your domain objects.
In this case, you can override table and column mappings on an ad hoc basis, specifying an override for each table or column where you want to change the name.
Mapping Individual Tables and Columns in the Designer
· To specify the name of the table for an entity class, select the entity, go to the Properties grid, and set the Table Name option.
· To specify the name of the column for a field, select the field, go to the Properties grid, and set the Column Name option.
· To specify the name of the identity column – that is, the column that corresponds to the Id property – select the entity, go to the Properties grid, and set the Identity Column Name option.
· To specify the name of the foreign key column for an association, select the association arrow, go to the Properties grid, and set the Column Name option.
If you have dragged a table into the model from a database, and you’re dissatisfied with the inferred names, you can quickly rename an entity or field while retaining its mapping by using the Refactor > Rename command and checking the “Keep existing name as database table/column name” option.
If you have dragged a table into the model from a database, and the primary key column has a name other than Id, LightSpeed will automatically create the identity column mapping for you.
Mapping Individual Tables and Columns in Code
· To specify the name of the table for an entity class, apply TableAttribute to the class.
· To specify the name of the column for a field, apply ColumnAttribute to the field.
· To specify the name of the identity column, apply TableAttribute to the class and set the attribute’s IdColumnName.
· To specify the name of the foreign key column for an association, apply ColumnAttribute to the field containing the foreign key.
Defining Your Own Mapping Convention
Mapping table and column names works well for individual tables and columns, but if you are working with a database which uses a regular convention that happens to be different from LightSpeed’s, then you may want to encapsulate that convention rather than mapping each and every table or column. For example, some organisations use a sort of Hungarian notation to name database elements: tables are prefixed tbl, columns are prefixed col, and so on. Other organisations like to use distinct primary key names in different tables, so the Employee table’s primary key would be EmployeeId while the Customer table’s primary key would be CustomerId.
Conventions such as this are represented by implementations of the INamingStrategy interface. INamingStrategy allows you to map the names of most LightSpeed elements, but for the time being we will focus on tables and columns. We could represent the Hungarian convention as follows:
Representing Hungarian notation as a LightSpeed naming strategy |
public class HungarianNamingStrategy : INamingStrategy |
All members of INamingStrategy get passed a defaultName by LightSpeed. If you want to accept the default convention in a particular case, just return defaultName.
Once you have implemented a naming strategy to represent your convention, you must tell LightSpeed to use it. To do this, set LightSpeedContext.NamingStrategy in code, or the namingStrategyClass attribute in configuration. When setting the naming strategy in configuration, you must provide a full assembly‑qualified type name.
Specifying the Hungarian convention in configuration |
<add name="Test" |
Specifying the Hungarian convention in code |
_context.NamingStrategy = new HungarianNamingStrategy(); |
Reserved Words
Occasionally you will want to use a name in your domain model which is a reserved word in SQL. The classic example is Order, which clashes with SQL’s ORDER BY keywords. To prevent errors in this case, set the quoteIdentifiers configuration attribute, or set LightSpeedContext.QuoteIdentifiers in code.