diff --git a/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs b/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs index beb5daac8b9..fb674031523 100644 --- a/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs +++ b/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs @@ -60,6 +60,13 @@ protected virtual void ValidateSharedContainerCompatibility( continue; } + var ownership = entityType.FindOwnership(); + if (ownership != null) + { + throw new InvalidOperationException(CosmosStrings.OwnedTypeDifferentContainer( + entityType.DisplayName(), ownership.PrincipalEntityType.DisplayName(), container)); + } + if (!containers.TryGetValue(container, out var mappedTypes)) { mappedTypes = new List(); diff --git a/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs b/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs index 9523f38b1be..197e81d7a42 100644 --- a/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs +++ b/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs @@ -227,6 +227,14 @@ public static string OrphanedNestedDocumentSensitive(object? entityType, object? GetString("OrphanedNestedDocumentSensitive", nameof(entityType), nameof(missingEntityType), nameof(keyValue)), entityType, missingEntityType, keyValue); + /// + /// The entity type '{entityType}' is owned by the entity type '{owner}', but is mapped to the container '{container}'. Owned types mapped to a container directly are not supported, remove this configuration to allow the owned type to be embedded in the same document as the owner. + /// + public static string OwnedTypeDifferentContainer(object? entityType, object? owner, object? container) + => string.Format( + GetString("OwnedTypeDifferentContainer", nameof(entityType), nameof(owner), nameof(container)), + entityType, owner, container); + /// /// The partition key specified in the 'WithPartitionKey' call '{partitionKey1}' and the partition key specified in the 'Where' predicate '{partitionKey2}' must be identical to return any results. Remove one of them. /// diff --git a/src/EFCore.Cosmos/Properties/CosmosStrings.resx b/src/EFCore.Cosmos/Properties/CosmosStrings.resx index 99ccab5273c..314782e698f 100644 --- a/src/EFCore.Cosmos/Properties/CosmosStrings.resx +++ b/src/EFCore.Cosmos/Properties/CosmosStrings.resx @@ -226,6 +226,9 @@ The entity of type '{entityType}' is mapped as part of the document mapped to '{missingEntityType}', but there is no tracked entity of this type with the key value '{keyValue}'. + + The entity type '{entityType}' is owned by the entity type '{owner}', but is mapped to the container '{container}'. Owned types mapped to a container directly are not supported, remove this configuration to allow the owned type to be embedded in the same document as the owner. + The partition key specified in the 'WithPartitionKey' call '{partitionKey1}' and the partition key specified in the 'Where' predicate '{partitionKey2}' must be identical to return any results. Remove one of them. diff --git a/test/EFCore.Cosmos.Tests/Infrastructure/CosmosModelValidatorTest.cs b/test/EFCore.Cosmos.Tests/Infrastructure/CosmosModelValidatorTest.cs index deb8a117c46..99fce63f1b9 100644 --- a/test/EFCore.Cosmos.Tests/Infrastructure/CosmosModelValidatorTest.cs +++ b/test/EFCore.Cosmos.Tests/Infrastructure/CosmosModelValidatorTest.cs @@ -273,6 +273,23 @@ public virtual void Detects_property_and_embedded_type_mapped_to_same_property() nameof(Order.OrderDetails), nameof(Order.PartitionId), typeof(Order).Name, "Details"), modelBuilder); } + [ConditionalFact] + public virtual void Detects_owned_type_mapped_to_a_container() + { + var modelBuilder = CreateConventionModelBuilder(); + modelBuilder.Entity(); + modelBuilder.Entity( + ob => + { + var ownedType = ob.OwnsOne(o => o.OrderDetails).OwnedEntityType; + ownedType.SetContainer("Details"); + }); + + VerifyError( + CosmosStrings.OwnedTypeDifferentContainer( + nameof(OrderDetails), nameof(Order), "Details"), modelBuilder); + } + [ConditionalFact] public virtual void Detects_missing_discriminator() {