Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get partition keys, container name, and JsonIdDefinition from the root entity in the hierarchy by default #34252

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ public static void SetPartitionKeyPropertyName(this IMutableEntityType entityTyp
/// <param name="entityType">The entity type.</param>
/// <returns>The names of the partition key properties, or <see langword="null"/> if not set.</returns>
public static IReadOnlyList<string> GetPartitionKeyPropertyNames(this IReadOnlyEntityType entityType)
=> entityType[CosmosAnnotationNames.PartitionKeyNames] as IReadOnlyList<string> ?? Array.Empty<string>();
=> entityType[CosmosAnnotationNames.PartitionKeyNames] as IReadOnlyList<string>
?? entityType.BaseType?.GetPartitionKeyPropertyNames()
?? Array.Empty<string>();

/// <summary>
/// Sets the names of the properties that are used to store the hierarchical partition key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ protected virtual void ValidateSharedContainerCompatibility(
continue;
}

if (entityType.BaseType != null
&& entityType.FindAnnotation(CosmosAnnotationNames.ContainerName)?.Value != null)
{
throw new InvalidOperationException(
CosmosStrings.ContainerNotOnRoot(entityType.DisplayName(), entityType.BaseType.DisplayName()));
}

var ownership = entityType.FindOwnership();
if (ownership != null)
{
Expand Down Expand Up @@ -345,6 +352,13 @@ protected virtual void ValidateKeys(
}
else
{
if (entityType.BaseType != null
&& entityType.FindAnnotation(CosmosAnnotationNames.PartitionKeyNames)?.Value != null)
{
throw new InvalidOperationException(
CosmosStrings.PartitionKeyNotOnRoot(entityType.DisplayName(), entityType.BaseType.DisplayName()));
}

foreach (var partitionKeyPropertyName in partitionKeyPropertyNames)
{
var partitionKey = entityType.FindProperty(partitionKeyPropertyName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ public static bool IsDocumentRoot(this IReadOnlyEntityType entityType)
/// </summary>
/// <param name="entityType">The entity type.</param>
public static IJsonIdDefinition? GetJsonIdDefinition(this IEntityType entityType)
=> entityType.GetOrAddRuntimeAnnotationValue(CosmosAnnotationNames.JsonIdDefinition,
static e =>
((CosmosModelRuntimeInitializerDependencies)e!.Model.FindRuntimeAnnotationValue(
CosmosAnnotationNames.ModelDependencies)!).JsonIdDefinitionFactory.Create(e),
entityType);
=> entityType.BaseType?.GetJsonIdDefinition()
?? entityType.GetOrAddRuntimeAnnotationValue(
CosmosAnnotationNames.JsonIdDefinition,
static e =>
((CosmosModelRuntimeInitializerDependencies)e!.Model.FindRuntimeAnnotationValue(
CosmosAnnotationNames.ModelDependencies)!).JsonIdDefinitionFactory.Create(e),
entityType);
}
16 changes: 16 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.

6 changes: 6 additions & 0 deletions src/EFCore.Cosmos/Properties/CosmosStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@
<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="ContainerNotOnRoot" xml:space="preserve">
<value>An Azure Cosmos DB container name is defined on entity type '{entityType}', which inherits from '{baseEntityType}'. Container names must be defined on the root entity type of a hierarchy.</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 Expand Up @@ -289,6 +292,9 @@
<data name="PartitionKeyBadValueType" xml:space="preserve">
<value>The partition key value supplied for '{propertyType}' property '{entityType}.{property}' is of type '{valueType}'. Partition key values must be of a type assignable to the property.</value>
</data>
<data name="PartitionKeyNotOnRoot" xml:space="preserve">
<value>A partition key is defined on entity type '{entityType}', which inherits from '{baseEntityType}'. Partition keys must be defined on the root entity type of a hierarchy.</value>
</data>
<data name="PartitionKeyMissing" xml:space="preserve">
<value>Unable to execute a 'ReadItem' query since the partition key value is missing. Consider using the 'WithPartitionKey' method on the query to specify partition key to use.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
modelBuilder.Entity<SharedContainerEntity2>()
.ToContainer("SharedContainer")
.HasPartitionKey(e => e.PartitionKey);
modelBuilder.Entity<SharedContainerEntity2Child>()
.HasPartitionKey(e => e.PartitionKey);
modelBuilder.Entity<SharedContainerEntity2Child>();
}

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public InstantiationBinding ServiceOnlyConstructorBinding
=> throw new NotImplementedException();

IReadOnlyEntityType IReadOnlyEntityType.BaseType
=> throw new NotImplementedException();
=> null!;

IReadOnlyModel IReadOnlyTypeBase.Model
=> throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,69 @@ public virtual void Detects_missing_partition_key_on_first_type()
VerifyError(CosmosStrings.NoPartitionKey(nameof(Customer), "", nameof(Order), "PartitionId", "Orders"), modelBuilder);
}

[ConditionalFact] // Issue #34176
public virtual void Partition_keys_do_not_need_to_be_explicitly_configured_on_non_root_types()
{
var modelBuilder = CreateConventionModelBuilder();

modelBuilder.Entity<A>().HasPartitionKey(e => e.P0).ToContainer("As");
modelBuilder.Entity<B>().HasPartitionKey(e => e.P0).ToContainer("As");
modelBuilder.Entity<C>();
modelBuilder.Entity<D>();
modelBuilder.Entity<F>();

Validate(modelBuilder);
}

[ConditionalFact] // Issue #34176
public virtual void Partition_keys_can_only_be_defined_on_the_root_of_a_hierarchy()
{
var modelBuilder = CreateConventionModelBuilder();

modelBuilder.Entity<A>(
b =>
{
b.HasPartitionKey(e => e.P0);
b.ToContainer("As");
b.HasKey(e => new { e.P0, e.P1 });
});

modelBuilder.Entity<B>().HasPartitionKey(e => e.P0).ToContainer("As");
modelBuilder.Entity<C>().ToContainer("As");
modelBuilder.Entity<D>().HasPartitionKey(e => e.P1).ToContainer("As");
modelBuilder.Entity<F>().ToContainer("As");

VerifyError(CosmosStrings.PartitionKeyNotOnRoot(nameof(D), nameof(A)), modelBuilder);
}

[ConditionalFact] // Issue #34176
public virtual void Container_does_not_need_to_be_explicitly_configured_on_non_root_types()
{
var modelBuilder = CreateConventionModelBuilder();

modelBuilder.Entity<A>().HasPartitionKey(e => e.P0).ToContainer("As");
modelBuilder.Entity<B>().HasPartitionKey(e => e.P0).ToContainer("As");
modelBuilder.Entity<C>();
modelBuilder.Entity<D>();
modelBuilder.Entity<F>();

Validate(modelBuilder);
}

[ConditionalFact] // Issue #34176
public virtual void Container_can_only_be_defined_on_the_root_of_a_hierarchy()
{
var modelBuilder = CreateConventionModelBuilder();

modelBuilder.Entity<A>().HasPartitionKey(e => e.P0).ToContainer("As");
modelBuilder.Entity<B>().HasPartitionKey(e => e.P0).ToContainer("As");
modelBuilder.Entity<C>();
modelBuilder.Entity<D>().ToContainer("Ds");
modelBuilder.Entity<F>();

VerifyError(CosmosStrings.ContainerNotOnRoot(nameof(D), nameof(A)), modelBuilder);
}

[ConditionalFact]
public virtual void Detects_missing_partition_keys_one_last_type()
{
Expand Down