Skip to content

Commit

Permalink
Cosmos: Fail in model validation for owned types that are mapped to a…
Browse files Browse the repository at this point in the history
… different container.

Fixes #26538
  • Loading branch information
AndriySvyryd committed Aug 10, 2022
1 parent b85b83f commit 37ee7d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<IEntityType>();
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore.Cosmos/Properties/CosmosStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@
<data name="OrphanedNestedDocumentSensitive" xml:space="preserve">
<value>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}'.</value>
</data>
<data name="OwnedTypeDifferentContainer" xml:space="preserve">
<value>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.</value>
</data>
<data name="PartitionKeyMismatch" xml:space="preserve">
<value>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.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Customer>();
modelBuilder.Entity<Order>(
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()
{
Expand Down

0 comments on commit 37ee7d7

Please sign in to comment.