2

Creating entity relationship through CDS Graphical Modeler for Visual Studio Cod...

 3 months ago
source link: https://blogs.sap.com/2022/05/03/creating-entity-relationship-through-cds-graphical-modeler-for-vscode/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
May 3, 2022 5 minute read

Creating entity relationship through CDS Graphical Modeler for Visual Studio Code

0 3 229

In this blog post, we’ll demonstrate how to create entity relationships for CDS models by using the CDS Graphical Modeler for Visual Studio Code.

CDS Entity Relationship Modeling using CDS modeler for VSCODE

CDS Graphical Modeler is now available on Visual Studio Code as well, and if you want to use the modeler in VSCODE platform you can download and install the modeler extension from https://marketplace.visualstudio.com/items?itemName=SAPSE.vscode-wing-cds-editor-vsc. For more details please refer to the previous blog post at https://blogs.sap.com/2022/04/29/an-introduction-to-cds-graphical-modeler-for-visual-studio-code/.

Previouslly I wrote a blog post about how to create entity relationships by using the CDS modeler for SAP Business Application Studio at https://blogs.sap.com/2021/04/08/cds-entity-relationship-modeling-using-cds-graphical-modeler/. Now we have a new way of creating entity relationships much more easily by just using simple drag and drop, which offers the developer better productivity in creating complex entity relationships very quickly even for complex CDS models.

Supposed we have below CDS model for espm:

namespace com.sap.cds.espm.db;

using
{
    cuid,
    managed,
    temporal
}
from '@sap/cds/common';

entity Customers : cuid, managed
{
    name : String;
    officialName : String;
    companyName : String;
    address1 : String;
    address2 : String;
    industryRanking1 : String;
    industryRanking2 : String;
    industryRanking3 : String;
    sector1 : String;
    sector2 : String;
    sector3 : String;
    revenue : Decimal;
    forecast1 : Integer;
    forecast2 : Integer;
    payment1 : String;
    payment2 : String;
    payment3 : String;
    vertical1 : String;
    vertical2 : String;
    certical3 : String;
}

entity Orders : cuid, managed
{
    name : String;
    name1 : String;
    name2 : String;
    paymentMethod1 : String;
    paymentMethod2 : String;
    deliveryAddress1 : String;
    deliveryAddress2 : String;
    deliveryAddress3 : String;
    total1 : Double;
    total2 : Double;
}

aspect OrderItems : cuid, managed
{
    name1 : String;
    name2 : String;
    name3 : String;
    listPrice1 : String;
    actualPrice1 : String;
    pic1 : String;
    pic2 : String;
    pic3 : String;
}

entity Products : cuid, managed
{
    name : String;
    officialName : String;
    longName1 : String;
    longName2 : String;
    title1 : String;
    title2 : String;
    image1 : String;
    image2 : String;
    manufacture : String;
    isOem : Boolean;
    validTo : Date;
    validTo2 : Date;
}

entity ProductDetails : temporal
{
    internalName1 : String;
    internalName2 : String;
    internalName3 : String;
    designDate : Date;
    designDate2 : Date;
    detail1 : String;
    detail2 : String;
    detail3 : String;
}

We now want to create the entity relationships among those entities using the CDS modeler. Open the CDS file using CDS Graphical Modeler for Visual Studio Code:

2022-05-03_09-18-58.png

2022-05-03_09-20-33.png

Now if we want to create a to-many association relationship from the Customers entity to the Orders entity, click the Customers entity and select “Add Relationship” context menu:

2022-05-03_09-22-09.png

You will see a pointer line that moves along with your mouse pointer:

2022-05-03_09-23-17.png

Move the line end point to the target Orders entity and release your mouse:

2022-05-03_09-24-30.png

The relationship dialog shows up:

2022-05-03_09-25-54.png

Change the “Relationship type” to “To-Many”, you will notice that a backlink property is automatically filled:

2022-05-03_09-26-46.png

Click “Create” button to dismiss the dialog, you will see the relationship has been created between the Customers entity and the Orders entity:

2022-05-03_09-28-43.png

Checking the CDS file content you will see the corresponding associations have been created as well:

entity Customers : cuid, managed
{
    name : String;
    officialName : String;
    companyName : String;
    address1 : String;
    address2 : String;
    industryRanking1 : String;
    industryRanking2 : String;
    industryRanking3 : String;
    sector1 : String;
    sector2 : String;
    sector3 : String;
    revenue : Decimal;
    forecast1 : Integer;
    forecast2 : Integer;
    payment1 : String;
    payment2 : String;
    payment3 : String;
    vertical1 : String;
    vertical2 : String;
    certical3 : String;
    orders : Association to many Orders on orders.customers = $self;
}

entity Orders : cuid, managed
{
    name : String;
    name1 : String;
    name2 : String;
    paymentMethod1 : String;
    paymentMethod2 : String;
    deliveryAddress1 : String;
    deliveryAddress2 : String;
    deliveryAddress3 : String;
    total1 : Double;
    total2 : Double;
    customers : Association to one Customers;
}

Now we want to create a to-many composition from the Orders to the OrderItems entity:

2022-05-03_09-31-19.png

and the relationship dialog pops up:

2022-05-03_09-32-20.png

Since the OrderItems is an aspect, so the only relationship that is allowed here is Composition. change the relationship type to “To-Many”:

2022-05-03_09-34-07.png

Click “Create” button to close the dialog, and you will see the relationship has been created between the Orders and OrderItems:

2022-05-03_09-34-54.png

We can also verify that by checking the CDS file content:

entity Orders : cuid, managed
{
    name : String;
    name1 : String;
    name2 : String;
    paymentMethod1 : String;
    paymentMethod2 : String;
    deliveryAddress1 : String;
    deliveryAddress2 : String;
    deliveryAddress3 : String;
    total1 : Double;
    total2 : Double;
    customers : Association to one Customers;
    orderItems : Composition of many OrderItems;
}

aspect OrderItems : cuid, managed
{
    name1 : String;
    name2 : String;
    name3 : String;
    listPrice1 : String;
    actualPrice1 : String;
    pic1 : String;
    pic2 : String;
    pic3 : String;
}

Now let’s create a to-one managed association from the OrderItems to Products entity:

2022-05-03_09-37-49.png

Keep everything default and click “Create” button to dismiss the dialog, and the relationship is setup:

2022-05-03_09-38-57.png

If we want to keep some of the product detail information to a separate ProductDetails entity to keep the Products entity clean, we need to setup a bi-directional association relationship between the 2 entities. First try to create a to-one managed association from the ProductDetails entity to the Products entity:

2022-05-03_09-41-02.png

In the popped up relationship dialog as below, leave everything as default:

2022-05-03_09-41-59.png

Click “Create” button to finish the relationship setup:

2022-05-03_09-43-15.png

Now make the relationship bi-directional so that we can navigate from the Products to ProductDetails by creating a to-one association from the Products entity to the ProductDetails:

2022-05-03_09-43-51.png

In the relationship dialog, keep everything as default:

2022-05-03_09-45-12.png

Click “Create” button to finish the relationship setup:

2022-05-03_09-46-23.png

The last thing we want to do is to make the “products” association in the ProductDetails entity the key property. Click the “products” property and click “Hide/Show Property Sheet” toolbar button:

2022-05-03_09-47-36.png

You will see the property sheet has shown up:

2022-05-03_09-49-09.png

Check the “Key” checkbox and notice the products to-one managed association has now become the key property for the entity:

2022-05-03_09-50-05.png

Now the entire relationships for the CDS model have been successfully by using the CDS Graphical Modeler:

2022-05-03_09-52-04.png

Conclusion

In the blog post, we have demonstrated how to create entity relationships for CDS models through drag and drop together with the relationship dialog very easily by using the CDS Graphical Modeler for Visual Studio Code.

References

CDS entity relationship modeling using CDS Graphical Modeler

An introduction to CDS Graphical Modeler for Visual Studio Code


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK