Skip to content

Commit

Permalink
Validate that GetContainingPropertyName() is null iff IsDocumentRoot()
Browse files Browse the repository at this point in the history
Fixes #27342
  • Loading branch information
AndriySvyryd authored Aug 17, 2022
1 parent 6e1047b commit 6d8bc7d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ protected virtual void ValidateSharedContainerCompatibility(
entityType.DisplayName(), ownership.PrincipalEntityType.DisplayName(), container));
}

if (entityType.GetContainingPropertyName() != null)
{
throw new InvalidOperationException(CosmosStrings.ContainerContainingPropertyConflict(
entityType.DisplayName(), container, entityType.GetContainingPropertyName()));
}

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 @@ -126,6 +126,9 @@
<data name="ConnectionStringConflictingConfiguration" xml:space="preserve">
<value>Both the connection string and CredentialToken, account key or account endpoint were specified. Specify only one set of connection details.</value>
</data>
<data name="ContainerContainingPropertyConflict" xml:space="preserve">
<value>The entity type '{entityType}' is mapped to the container '{container}' but it is also configured as being contained in property '{property}'.</value>
</data>
<data name="CosmosNotInUse" xml:space="preserve">
<value>Cosmos-specific methods can only be used when the context is using the Cosmos provider.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,24 @@ public virtual void Detects_owned_type_mapped_to_a_container()
nameof(OrderDetails), nameof(Order), "Details"), modelBuilder);
}

[ConditionalFact]
public virtual void Detects_conflicting_containing_property()
{
var modelBuilder = CreateConventionModelBuilder();
modelBuilder.Entity<Customer>().ToContainer("Orders");
modelBuilder.Entity<Order>().ToContainer("Orders").Metadata.SetContainingPropertyName("Prop");

VerifyError(CosmosStrings.ContainerContainingPropertyConflict(nameof(Order), "Orders", "Prop"), modelBuilder);
}

[ConditionalFact]
public virtual void Detects_missing_discriminator()
{
var modelBuilder = CreateConventionModelBuilder();
modelBuilder.Entity<Customer>().ToContainer("Orders").HasNoDiscriminator();
modelBuilder.Entity<Order>().ToContainer("Orders");

VerifyError(CosmosStrings.NoDiscriminatorProperty(typeof(Customer).Name, "Orders"), modelBuilder);
VerifyError(CosmosStrings.NoDiscriminatorProperty(nameof(Customer), "Orders"), modelBuilder);
}

[ConditionalFact]
Expand Down

0 comments on commit 6d8bc7d

Please sign in to comment.