Skip to content
Open
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
8 changes: 4 additions & 4 deletions src/EFCore/Metadata/Conventions/RuntimeModelConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ private RuntimeForeignKey Create(IForeignKey foreignKey, RuntimeEntityType runti
{
var principalEntityType = runtimeEntityType.Model.FindEntityType(foreignKey.PrincipalEntityType.Name)!;
return runtimeEntityType.AddForeignKey(
runtimeEntityType.FindProperties(foreignKey.Properties.Select(p => p.Name))!,
foreignKey.Properties.Select(p => FindRuntimeProperty(runtimeEntityType, p)).ToArray(),
GetKey(foreignKey.PrincipalKey, principalEntityType),
principalEntityType,
foreignKey.DeleteBehavior,
Expand Down Expand Up @@ -810,7 +810,7 @@ private RuntimeSkipNavigation Create(ISkipNavigation navigation, RuntimeEntityTy
/// <returns>The corresponding read-optimized foreign key.</returns>
protected virtual RuntimeForeignKey GetForeignKey(IForeignKey foreignKey, RuntimeEntityType entityType)
=> entityType.FindDeclaredForeignKeys(
entityType.FindProperties(foreignKey.Properties.Select(p => p.Name))!)
foreignKey.Properties.Select(p => FindRuntimeProperty(entityType, p)).ToArray())
.Single(fk => fk.PrincipalEntityType.Name == foreignKey.PrincipalEntityType.Name
&& fk.PrincipalKey.Properties.Select(p => p.Name).SequenceEqual(
foreignKey.PrincipalKey.Properties.Select(p => p.Name)));
Expand All @@ -822,7 +822,7 @@ protected virtual RuntimeForeignKey GetForeignKey(IForeignKey foreignKey, Runtim
/// <param name="entityType">The declaring entity type.</param>
/// <returns>The corresponding read-optimized key.</returns>
protected virtual RuntimeKey GetKey(IKey key, RuntimeEntityType entityType)
=> entityType.FindKey(entityType.FindProperties(key.Properties.Select(p => p.Name))!)!;
=> entityType.FindKey(key.Properties.Select(p => FindRuntimeProperty(entityType, p)).ToArray())!;

/// <summary>
/// Gets the corresponding index in the read-optimized model.
Expand All @@ -832,7 +832,7 @@ protected virtual RuntimeKey GetKey(IKey key, RuntimeEntityType entityType)
/// <returns>The corresponding read-optimized index.</returns>
protected virtual RuntimeIndex GetIndex(IIndex index, RuntimeEntityType entityType)
=> index.Name == null
? entityType.FindIndex(entityType.FindProperties(index.Properties.Select(p => p.Name))!)!
? entityType.FindIndex(index.Properties.Select(p => FindRuntimePropertyBase(entityType, p)).ToArray())!
: entityType.FindIndex(index.Name)!;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2312,6 +2312,37 @@ public virtual void Can_define_alternate_key_on_complex_property_via_string_dott
Assert.Same(entityType, alternateKey.DeclaringEntityType);
}

[Fact]
public virtual void Can_use_alternate_key_on_complex_property_as_principal_key()
{
var modelBuilder = CreateModelBuilder();

modelBuilder
.Ignore<Order>()
.Ignore<IndexedClass>()
.Entity<ComplexProperties>(b =>
{
b.Ignore(e => e.Customer);
b.Ignore(e => e.CollectionQuarks);
b.Ignore(e => e.QuarksCollection);
b.Ignore(e => e.DoubleProperty);
b.HasAlternateKey(e => e.Quarks.Up);
b.HasMany(e => e.Customers).WithOne()
.HasForeignKey("ParentUp")
.HasPrincipalKey("Quarks.Up");
});

var model = modelBuilder.FinalizeModel();
Comment thread
AndriySvyryd marked this conversation as resolved.
var entityType = model.FindEntityType(typeof(ComplexProperties))!;
var quarksType = entityType.GetComplexProperties().Single(p => p.Name == nameof(ComplexProperties.Quarks))
.ComplexType;
var upProperty = quarksType.FindProperty(nameof(Quarks.Up))!;

var foreignKey = model.GetEntityTypes().Single(t => t.ClrType == typeof(Customer)).GetForeignKeys()
.Single(fk => fk.PrincipalEntityType == entityType);
Assert.Same(upProperty, foreignKey.PrincipalKey.Properties.Single());
}

[Fact]
public virtual void Can_define_index_on_complex_property_via_lambda()
{
Expand Down
Loading