Using Default Mapping
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.
When you use default mapping, Telerik Data Access takes care of the property to column mapping. Or in other words, when you have a Model First Scenario and you use the default mapping functionality, Telerik Data Access will take care of the columns and table naming. On the other side, the explicit mapping gives you more control over the columns\table configuration. This topic discusses the default mapping functionality. For more information about the explicit mapping, check out the Explicit Mapping topic.
- Using the Default Mapping Option
- Naming Conventions
- Adding New Properties to Default Mapped Entity
- Mapping Entities without Identity (Primary Key)
- Controlling the SQL Type
- Controlling the Property/Column Nullability
- Default Mapping Errors
Using the Default Mapping Option
Suppose, you are doing a model first mapping and you have the following domain model.
The Customer domain class is not mapped to a table. As you know from the Model First Scenario topic, when you save your domain model and take a look at the Error List pane, you should be presented with the following warning.
This warning is completely valid, and the information is absolutely correct. The warning is letting you know that the Customer item in the Visual Designer is not mapped to anything. The project will save and compile as-is, since this is a warning, but beyond having your conceptual model you really don't have anything substantial.
What you need to do in this case is to map your domain class (e.g. Customer) to a table. In order to do that:
- Select the domain class (in this example, the Customer entity).
- Open the Mapping Details Editor (View->Other Windows->Entity Diagrams Details Editor).
-
In the Table Mappings section, check the Use Default Mapping option.
Using the "Use Default Mapping" option should be the last step when configuring an entity. The best moment to apply it on an entity, is after your entity has been fully defined, i.e. you have defined all properties and you have set the primary key. The reason is that when you use the default mapping option, you don't have control over the mapping process. For example, consider the following situation. When you have an entity that has no identity defined and you decide to click "Use Default Mapping", the relational metadata for the entity is populated and it contains an internal column that would be used to store the PK values. That internal column will be automatically added to the relational metadata. In this case Internal Identity Mechanism will be used. If you decide to alter your entity and define an identity member after you have clicked "Use Default Mapping", the new member is added to the entity metadata. However the internal identity column that came through the default mapping process will remain.
What Just Happened?
By using the Use Default Mapping option, Telerik Data Access automatically creates a table in the relational part of the model and maps all properties to columns. Note the specific icons for the default mapped tables and columns.
The specific moment here is that Telerik Data Access will give default names for the underlying table and columns. For example, the Customer domain class will be mapped to a table named 'Customer'. And the Customer properties will be mapped to columns as follow:
- Id <-> Id
- Name <-> nme
- HireDate <-> HireDate
Naming Conventions
Telerik Data Access will use a default naming strategy for the table and columns name generation. For more information about the naming conventions and how to modify the generated database names, please refer to Naming Conventions.
You need to modify the database naming settings before mapping your entity by using the default mapping functionality. Once your entity is mapped to a table, changing the database naming settings will not affect the table and the columns.
Adding New Properties to Default Mapped Entity
If you have already mapped your entity using default mapping and you want to add additional properties, there are two common approaches.
The first approach is to explicitly add a new column in the generated table using the Edit Table Dialog and map it to the new property. This process is explained in the Explicit Mapping topic. However, you should keep in mind that each modification of the table will make it non-default mapped, i.e. explicit mapped. So if you want to keep the default mapping, you need to take the second approach, i.e. you need simply to uncheck and then check again the Use Default Mapping option.
Each modification of the table will make it non-default mapped.
For more information about how to handle the model changes once your database is created, refer to Handling Model Changes.
Mapping Entities Without Identity (Primary Key)
When you have an entity that has no identity defined and you decide to click "Use Default Mapping", the relational metadata for the entity is populated and it contains an internal column that would be used to store the PK values. That internal column will be automatically added to the relational metadata. In this case Internal Identity Mechanism will be used.
Controlling the SQL Type
When you use default mapping, Telerik Data Access automatically creates CLR to SQL type mapping. You can explicitly modify the SQL Type in the Edit Table Dialog.
Controlling the Property/Column Nullability
You can specify whether the property accepts null values or not by changing the Nullable property in the Property Window. However, you should do this before clicking the Use Default Mapping option. After you have default mapped your entity, you can still change the nullability by using the Edit Table Dialog.
Default Mapping Errors
Sometimes Telerik Data Access cannot "default map" your domain classes due to errors in the definition of your domain classes (e.g. invalid property name). In this case, you will need to fix the error(s) first and then use the default mapping functionality again. For more information, please refer to Default Mapping Errors.
Next Steps
The next step is to migrate your database schema to the latest model state by using the Update Database from Model wizard. For additional information, see Handling Model Changes.
As you read in this topic, when you use default mapping, Telerik Data Access takes care of the columns and table naming. If you want to have full controls over the mapping, then you need to use the Edit Table dialog. In other words, you need to use the explicit mapping functionality.