Skip to content

Commit

Permalink
Validate that GetContainingPropertyName() is null iff IsDocumentRoot()
Browse files Browse the repository at this point in the history
Fixes #26263
  • Loading branch information
AndriySvyryd committed Aug 17, 2022
1 parent c525654 commit 036c3f7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
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 @@ -277,7 +277,7 @@ public virtual void Detects_property_and_embedded_type_mapped_to_same_property()
public virtual void Detects_owned_type_mapped_to_a_container()
{
var modelBuilder = CreateConventionModelBuilder();
modelBuilder.Entity<Customer>();
modelBuilder.Entity<Customer>().ToContainer("Orders");
modelBuilder.Entity<Order>(
ob =>
{
Expand All @@ -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 036c3f7

Please sign in to comment.