In this post we will discuss how to add associations in a forward mapping scenario with our Visual Designer. We will start from scratch and build a model with two entities – Category and Product where each Product belongs to at most one Category (a zero-or-one-to-many relationship from the categories to the products).
1. In the solution explorer right-click your project (where you want to add the persistent types) and select OpenAccess -> Add Domain Model. We will create an empty model.
2. The next step is to create a new database which we will name WarehouseDB. Creating a brand new database can be achieved when we select the New Connection option in the next dialog. There we can pick the server name and type our database name. When we click OK we are prompted to add the database to our server.
3. After our database has been successfully created we can add our entities by dragging a MetaClass from the toolbox to the design surface.
What is important here is not to forget to include a property of the product which refers to its category (CategoryID).
4. After our entities are ready it is essential that we build our project.
CAUTION This step is not a recommendation; it is required so that the mapping process continues smoothly without errors!
We can now open the Model Schema Explorer, right-click and from the context menu choose the update database option.
5. In this step we will choose a folder where we will save our generated DDL script. Since we want to define a relational model and immediately execute the generated script we could select Generate and Execute Schema Definition Script. Here is a blog post which sheds some light on the different options we have.
6. When we click Next we receive a script which we can execute right away with the Execute Script option.
7. Now that we have created our tables we would like to update our model and make it aware of the existence of those tables.
8. After we have applied the changes our Model Schema Explorer should be in a state with grayed out Category and Product tables.
9. Now we can map our entities to the newly added tables with the help of the Mapping Details Editor.
10. With our mapping in place we can finally drag an association from our toolbox between the two entities.
11. When the dragging process is over a dialog pops up where we can configure the relationship between the Category and the Product.
12. One way to verify that our associations are correctly introduced is to open the code in our entity generated.cs files and see whether we have a Products collection property in the Category class and a Category property in our Product class.
13. Our final steps are to rebuild the project and forward map the constraint we have just imposed on our entities – the option Update Database From Model comes in handy. At this step we will select the schema migration script option. The next dialog allows for pinpointing the entities involved in the update of our relational model. As is shown on the picture below, we are confronted with two lists – the Excluded items and the Items to add list. The Excluded items list contains the persistent types for which the value of ShouldUpdateSchema property is false and the Items to add list contains the persistent types with this property set to true. The property internally controls whether the metadata for a class will update the schema. This means that you may introduce constraints and entities which will remain only in the metamodel and will never be propagated to the database layer. The purpose of the arrow buttons is to move items between the two lists. You can move all the entities from one list to the other by clicking the double headed arrow buttons. A mischievous check box below the items lists (Override model behavior and mark everything to update schema) allows to completely override the model behavior with the current state of the domain model. As a result all the entities will be added to the Items to add list. For the purposes of our example we will select the Product and Category entities and move them to the Items to add list.
14. When we click Next, we can see the generated constraints – a foreign key association in our example – and modify it according to our specific needs. We will not modify the constraint as it is simple enough in this case.
15. After successful execution of the script, we have a fully functional domain model mapped to a relational one.
It is worth pointing out, however, that very soon we will introduce the option to forward map our model with incompletely defined associations (no need to create the tables prior to the addition of the relationships).
Stay tuned for more on our new forward mapping technology.
Copyright © 2016, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
Progress, Telerik, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks or appropriate markings.