In this section you will learn the difference between managed and unmanaged associations in CAP CDS. This information will help you better understand the annotation syntax in the exercise which follows.
In the database model, associations need to be mapped to foreign-key relationships. CAP CDS supports both unmanaged and managed associations.
Unmanaged associations have to use the existing properties of the source and target entity. In this case, CAP does not generate any properties, and it's not possible to use other associations. The ON
clause may contain any kind of join conditions referring to available foreign key properties. The following example demonstrates how you can associate the entity Customer
with the key property CustomerID
to the entity Booking
with the foreign key property to_Customer_CustomerID
:
entity Booking { ...
to_Customer_CustomerID : String(6);
to_Customer : Association to Passenger on to_Customer.CustomerID = to_Customer_CustomerID;
}
ExpandFor managed associations, a foreign key constraint is automatically generated. That constraint ties the foreign key properties of the source entity (automatically added for a managed association) to the respective primary key properties of the target entity (already given):
entity Booking { ...
to_Customer: Association to Passenger;
}
ExpandWhen you compare managed association to unmanaged, you can find two convenient shortcuts:
- The Booking entity no longer has the foreign key property that was named
to_Customer_CustomerID
- The association
to_Customer
no longer has an ON
clause
When CAP generates the OData metadata document at runtime, you'll see that the API still contains both a referential constraint representing the ON
clause and the foreign key property to_Customer_CustomerID
:
<EntityType Name="Booking">
<Key>
<PropertyRef Name="BookingUUID"/>
<PropertyRef Name="IsActiveEntity"/>
</Key>
...
<NavigationProperty Name="to_Customer" Type="TravelService.Passenger">
<ReferentialConstraint Property="to_Customer_CustomerID" ReferencedProperty="CustomerID"/>
</NavigationProperty>
<Property Name="to_Customer_CustomerID" Type="Edm.String" MaxLength="6"/>
</EntityType>
ExpandKeep in mind the difference between the managed and unmanaged associations and what is generated at runtime. In particular, you'll find that a few annotations which you would expect at foreign key property level, need to be maintained at association level. For managed associations foreign key properties are only available at runtime. The example below shows annotating the to_Customer_CustomerID
field of the entity Booking
with the text LastName
of the associated customer when using a managed association:
annotate Booking with { ...
to_Customer @Common.Text: to_Customer.LastName;
}
ExpandWhen using an unmanaged association, the foreign key property is already available at the source entity at the design time:
annotate Booking with { ...
to_Customer_CustomerID @Common.Text: to_Customer.LastName;
}
ExpandNext Steps
For more information, see: