Skip to content

Commit

Permalink
Persist Id values into owned collection JSON documents
Browse files Browse the repository at this point in the history
Fixes #29380

There are several aspects to #29380:
- Constructor binding (already fixed)
- Round-tripping the `Id` property value (addressed by this PR)
- Persisting key values in JSON collection documents (tracked by #28594)

I investigated persisting key values, but identity lookup requires key values in the materializer before we do the parsing of the document. This means persisted key values are not available without re-writing this part of the shaper, which we can't do for 9.

To fix the main issue (round-trip `Id`) this PR changes the way identity on owned JSON collection documents works. Instead of discovering and using the `Id` property, we treat it like any other property. We then create a shadow `__synthesizedOrdinal` property to act as the ordinal part of the key.

We need to discuss:
- Is this breaking for any scenario that was working before?
- Does this put us in a bad place for the future of owned types with explicit keys?
  • Loading branch information
ajcvickers committed Aug 20, 2024
1 parent 0e2cb90 commit 61ea89e
Show file tree
Hide file tree
Showing 16 changed files with 421 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ public override ConventionSet CreateConventionSet()

conventionSet.Replace<ValueGenerationConvention>(
new RelationalValueGenerationConvention(Dependencies, RelationalDependencies));

conventionSet.Replace<KeyDiscoveryConvention>(
new RelationalKeyDiscoveryConvention(Dependencies, RelationalDependencies));

conventionSet.Replace<QueryFilterRewritingConvention>(
new RelationalQueryFilterRewritingConvention(Dependencies, RelationalDependencies));
conventionSet.Replace<RuntimeModelConvention>(new RelationalRuntimeModelConvention(Dependencies, RelationalDependencies));

conventionSet.Replace<RuntimeModelConvention>(
new RelationalRuntimeModelConvention(Dependencies, RelationalDependencies));

return conventionSet;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions;

/// <summary>
/// A relational-specific convention inheriting from <see cref="KeyDiscoveryConvention"/>.
/// </summary>
/// <remarks>
/// <para>
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see> for more information and examples.
/// </para>
/// </remarks>
public class RelationalKeyDiscoveryConvention : KeyDiscoveryConvention, IEntityTypeAnnotationChangedConvention
{
private const string SynthesizedOrdinalPropertyName = "__synthesizedOrdinal";

/// <summary>
/// Creates a new instance of <see cref="RelationalKeyDiscoveryConvention" />.
/// </summary>
/// <param name="dependencies">Parameter object containing dependencies for this convention.</param>
/// <param name="relationalDependencies"> Parameter object containing relational dependencies for this convention.</param>
public RelationalKeyDiscoveryConvention(
ProviderConventionSetBuilderDependencies dependencies,
RelationalConventionSetBuilderDependencies relationalDependencies)
: base(dependencies)
{
RelationalDependencies = relationalDependencies;
}

/// <summary>
/// Relational provider-specific dependencies for this service.
/// </summary>
protected virtual RelationalConventionSetBuilderDependencies RelationalDependencies { get; }

/// <inheritdoc />
protected override List<IConventionProperty>? DiscoverKeyProperties(IConventionEntityType entityType)
{
var ownership = entityType.FindOwnership();
if (ownership?.DeclaringEntityType != entityType)
{
ownership = null;
}

// Don't discover key properties for owned collection types mapped to JSON so that we can persist properties
// called `Id` without attempting to persist key values.
if (ownership?.IsUnique == false
&& entityType.GetContainerColumnName() is not null)
{
return [];
}

return base.DiscoverKeyProperties(entityType);
}

/// <inheritdoc />
protected override void ProcessKeyProperties(
IList<IConventionProperty> keyProperties,
IConventionEntityType entityType)
{
var isMappedToJson = entityType.GetContainerColumnName() is not null;
var synthesizedProperty = keyProperties.FirstOrDefault(p => p.Name == SynthesizedOrdinalPropertyName);
var ownershipForeignKey = entityType.FindOwnership();
if (ownershipForeignKey?.IsUnique == false
&& isMappedToJson)
{
// This is an owned collection, so it has a composite key consisting of FK properties pointing to the owner PK,
// any additional key properties defined by the application, and then the synthesized property.
// Add these in the correct order--this is somewhat inefficient, but we are limited because we have to manipulate the
// existing collection.
var existingKeyProperties = keyProperties.ToList();
keyProperties.Clear();

// Add the FK properties to form the first part of the composite key.
foreach (var conventionProperty in ownershipForeignKey.Properties)
{
keyProperties.Add(conventionProperty);
}

// Generate the synthesized key property if it doesn't exist.
if (synthesizedProperty == null)
{
var builder = entityType.Builder.CreateUniqueProperty(typeof(int), SynthesizedOrdinalPropertyName, required: true);
builder = builder?.ValueGenerated(ValueGenerated.OnAdd) ?? builder;
synthesizedProperty = builder!.Metadata;
}

// Add non-duplicate, non-ownership, non-synthesized properties.
foreach (var keyProperty in existingKeyProperties)
{
if (keyProperty != synthesizedProperty
&& !keyProperties.Contains(keyProperty))
{
keyProperties.Add(keyProperty);
}
}

// Finally, the synthesized property always goes at the end.
keyProperties.Add(synthesizedProperty);
}
else
{
// Not an owned collection or not mapped to JSON.
if (synthesizedProperty is not null)
{
// This was an owned collection, but now is not, so remove the synthesized property.
keyProperties.Remove(synthesizedProperty);
}

base.ProcessKeyProperties(keyProperties, entityType);
}
}

/// <inheritdoc />
public override void ProcessPropertyAdded(
IConventionPropertyBuilder propertyBuilder,
IConventionContext<IConventionPropertyBuilder> context)
{
if (propertyBuilder.Metadata.Name != SynthesizedOrdinalPropertyName)
{
base.ProcessPropertyAdded(propertyBuilder, context);
}
}

/// <inheritdoc />
public virtual void ProcessEntityTypeAnnotationChanged(
IConventionEntityTypeBuilder entityTypeBuilder,
string name,
IConventionAnnotation? annotation,
IConventionAnnotation? oldAnnotation,
IConventionContext<IConventionAnnotation> context)
{
if (name == RelationalAnnotationNames.ContainerColumnName)
{
Configure(this, entityTypeBuilder);
}

static void Configure(RelationalKeyDiscoveryConvention me, IConventionEntityTypeBuilder builder)
{
me.TryConfigurePrimaryKey(builder);

foreach (var ownershipFk in builder.Metadata.GetReferencingForeignKeys())
{
if (ownershipFk.IsOwnership)
{
Configure(me, ownershipFk.DeclaringEntityType.Builder);
}
}
}
}
}
30 changes: 30 additions & 0 deletions test/EFCore.Cosmos.FunctionalTests/Query/JsonQueryCosmosFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,18 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
modelBuilder.Entity<JsonEntityBasic>().OwnsOne(
x => x.OwnedReferenceRoot, b =>
{
// Issue #29380
b.Ignore(x => x.Id);
b.OwnsOne(
x => x.OwnedReferenceBranch, bb =>
{
//issue #34026
bb.Ignore(x => x.Enums);
bb.Ignore(x => x.NullableEnums);
// Issue #29380
bb.Ignore(x => x.Id);
});
b.OwnsMany(
Expand All @@ -56,18 +62,27 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
//issue #34026
bb.Ignore(x => x.Enums);
bb.Ignore(x => x.NullableEnums);
// Issue #29380
bb.Ignore(x => x.Id);
});
});

modelBuilder.Entity<JsonEntityBasic>().OwnsMany(
x => x.OwnedCollectionRoot, b =>
{
// Issue #29380
b.Ignore(x => x.Id);
b.OwnsOne(
x => x.OwnedReferenceBranch, bb =>
{
//issue #34026
bb.Ignore(x => x.Enums);
bb.Ignore(x => x.NullableEnums);
// Issue #29380
bb.Ignore(x => x.Id);
});
b.OwnsMany(
Expand All @@ -76,6 +91,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
//issue #34026
bb.Ignore(x => x.Enums);
bb.Ignore(x => x.NullableEnums);
// Issue #29380
bb.Ignore(x => x.Id);
});
});

Expand Down Expand Up @@ -106,6 +124,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
//issue #34026
bb.Ignore(x => x.Enums);
bb.Ignore(x => x.NullableEnums);
// Issue #29380
bb.Ignore(e => e.Id);
});
b.OwnsMany(
Expand All @@ -114,6 +135,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
//issue #34026
bb.Ignore(x => x.Enums);
bb.Ignore(x => x.NullableEnums);
// Issue #29380
bb.Ignore(e => e.Id);
});
});

Expand All @@ -126,6 +150,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
//issue #34026
bb.Ignore(x => x.Enums);
bb.Ignore(x => x.NullableEnums);
// Issue #29380
bb.Ignore(e => e.Id);
});
b.OwnsMany(
Expand All @@ -134,6 +161,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
//issue #34026
bb.Ignore(x => x.Enums);
bb.Ignore(x => x.NullableEnums);
// Issue #29380
bb.Ignore(e => e.Id);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Microsoft.EntityFrameworkCore.Query;

// #34395
[CosmosCondition(CosmosCondition.DoesNotUseTokenCredential | CosmosCondition.UsesTokenCredential)]
[CosmosCondition(CosmosCondition.DoesNotUseTokenCredential)]
public class JsonQueryCosmosTest : JsonQueryTestBase<JsonQueryCosmosFixture>
{
private const string NotImplementedBindPropertyMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4378,14 +4378,17 @@ public virtual void Owned_types_mapped_to_json_are_stored_in_snapshot()
b3.Property<int>("EntityWithStringKeyEntityWithTwoPropertiesEntityWithOnePropertyId")
.HasColumnType("int");

b3.Property<int>("Id")
b3.Property<int>("__synthesizedOrdinal")
.ValueGeneratedOnAdd()
.HasColumnType("int");

b3.Property<int>("Id")
.HasColumnType("int");

b3.Property<string>("Name")
.HasColumnType("nvarchar(max)");

b3.HasKey("EntityWithStringKeyEntityWithTwoPropertiesEntityWithOnePropertyId", "Id");
b3.HasKey("EntityWithStringKeyEntityWithTwoPropertiesEntityWithOnePropertyId", "__synthesizedOrdinal");

b3.ToTable("EntityWithOneProperty", "DefaultSchema");

Expand Down Expand Up @@ -4453,14 +4456,15 @@ public virtual void Owned_types_mapped_to_json_are_stored_in_snapshot()
Assert.Equal(nameof(EntityWithStringProperty), ownedType3.DisplayName());
var pkProperties3 = ownedType3.FindPrimaryKey().Properties;
Assert.Equal("EntityWithStringKeyEntityWithTwoPropertiesEntityWithOnePropertyId", pkProperties3[0].Name);
Assert.Equal("Id", pkProperties3[1].Name);
Assert.Equal("__synthesizedOrdinal", pkProperties3[1].Name);
var ownedProperties3 = ownedType3.GetProperties().ToList();
Assert.Equal(3, ownedProperties3.Count);
Assert.Equal(4, ownedProperties3.Count);
Assert.Equal("EntityWithStringKeyEntityWithTwoPropertiesEntityWithOnePropertyId", ownedProperties3[0].Name);
Assert.Equal("Id", ownedProperties3[1].Name);
Assert.Equal("Name", ownedProperties3[2].Name);
Assert.Equal("__synthesizedOrdinal", ownedProperties3[1].Name);
Assert.Equal("Id", ownedProperties3[2].Name);
Assert.Equal("Name", ownedProperties3[3].Name);
});

private class Order
Expand Down
Loading

0 comments on commit 61ea89e

Please sign in to comment.