Skip to content

Commit

Permalink
Add composition to owned entities with explicit key
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Svensson committed Jun 3, 2024
1 parent 89bc591 commit a47714a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,16 @@ protected override IEnumerable<Attribute> GetMemberAttributes(PropertyDescriptor
&& !(navigation.ForeignKey.Properties.Any(static p => p.IsShadowProperty()));
#endif

if (addAssociationAttribute && pd.Attributes[typeof(AssociationAttribute)] is null)
if (addAssociationAttribute)
{
attributes.Add(EFCoreTypeDescriptionContext.CreateAssociationAttribute(navigation));
if (pd.Attributes[typeof(AssociationAttribute)] is null)
attributes.Add(EFCoreTypeDescriptionContext.CreateAssociationAttribute(navigation));
#if NET
if (navigation.TargetEntityType.IsOwned() && pd.Attributes[typeof(CompositionAttribute)] is null)
{
attributes.Add(new CompositionAttribute());
}
#endif
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,13 @@ EF Core owned entities are mapped to OpenRiaServices's [Complex Types](https://o


### Owned Entities with explicit key
If an explicit key to an owned entity then they are mapped as a normal entity.
If an explicit key to an owned entity then they are mapped as a normal entity and the navigation property to the owned entity adds `[Composition]`

You should generally also mark the property with `[Composition]` so that the owned entity is always sent togheter with the `Owner` in case the owner changes
in order to have the owned entity available during Insert, Update and Delete operations
This makes the owned entity available during Insert, Update and Delete operations and prevents if from accidentaly having all fields set to `null`.

## Complex Types

The *Complex Types* introduced in EF Core 8 is partially supported with some limitations.

1. The types are mapped to to OpenRiaServices's [Complex Types]
2. Any ef core configuration/metadata applied to the ComplexType (as part of fluent configuration) **IS NOT** discovered.The `DbDomainService` and `DbDomainServiceDescriptionProvider` does not process *Complex Types* at all. 3. Attributes on the types are discovered as expected
2. Any ef core configuration/metadata applied to the ComplexType (as part of fluent configuration) **IS NOT** discovered.The `DbDomainServiceDescriptionProvider` and `DbDomainService` classes does not have any special handling of *Complex Types*. * Attributes on the types are discovered as expected using the normal built in reflection based attribute discovery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ public OwnedEntityWithBackNavigation OwnedEntityWithBackNavigation
/// Gets or sets the associated <see cref="OwnedEntityWithExplicitId"/> entity.
/// </summary>
[Association("FK_Employees_Employees_EmployeeId|owns:OwnedEntityWithExplicitId", "EmployeeId", "EmployeeId")]
[Composition()]
public OwnedEntityWithExplicitId OwnedEntityWithExplicitId
{
get
Expand Down Expand Up @@ -343,6 +344,7 @@ public OwnedEntityWithExplicitId OwnedEntityWithExplicitId
/// </summary>
[Association("FK_Employees_Employees_EmployeeId|owns:OwnedEntityWithExplicitIdAndBackNavigation" +
"", "EmployeeId", "EmployeeId")]
[Composition()]
public OwnedEntityWithExplicitIdAndBackNavigation OwnedEntityWithExplicitIdAndBackNavigation
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ Namespace EFCoreModels.Scenarios.OwnedTypes
''' <summary>
''' Gets or sets the associated <see cref="OwnedEntityWithExplicitId"/> entity.
''' </summary>
<Association("FK_Employees_Employees_EmployeeId|owns:OwnedEntityWithExplicitId", "EmployeeId", "EmployeeId")> _
<Association("FK_Employees_Employees_EmployeeId|owns:OwnedEntityWithExplicitId", "EmployeeId", "EmployeeId"), _
Composition()> _
Public Property OwnedEntityWithExplicitId() As OwnedEntityWithExplicitId
Get
If (Me._ownedEntityWithExplicitId Is Nothing) Then
Expand All @@ -333,7 +334,8 @@ Namespace EFCoreModels.Scenarios.OwnedTypes
''' Gets or sets the associated <see cref="OwnedEntityWithExplicitIdAndBackNavigation"/> entity.
''' </summary>
<Association("FK_Employees_Employees_EmployeeId|owns:OwnedEntityWithExplicitIdAndBackNavigation"& _
"", "EmployeeId", "EmployeeId")> _
"", "EmployeeId", "EmployeeId"), _
Composition()> _
Public Property OwnedEntityWithExplicitIdAndBackNavigation() As OwnedEntityWithExplicitIdAndBackNavigation
Get
If (Me._ownedEntityWithExplicitIdAndBackNavigation Is Nothing) Then
Expand Down

0 comments on commit a47714a

Please sign in to comment.