Skip to content

Commit 923f9dd

Browse files
committed
Filter out indexer navigations when sorting columns
Fixes #27504
1 parent ffda3b5 commit 923f9dd

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ namespace Microsoft.EntityFrameworkCore.Migrations.Internal;
1616
/// </summary>
1717
public class MigrationsModelDiffer : IMigrationsModelDiffer
1818
{
19+
private static readonly bool QuirkEnabled27504
20+
= AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27504", out var enabled) && enabled;
21+
1922
private static readonly Type[] DropOperationTypes =
2023
{
2124
typeof(DropIndexOperation),
@@ -784,7 +787,9 @@ private static IEnumerable<IProperty> GetSortedProperties(IEntityType entityType
784787

785788
var linkingNavigationProperty = linkingForeignKey.PrincipalToDependent?.PropertyInfo;
786789
var properties = GetSortedProperties(linkingForeignKey.DeclaringEntityType, table).ToList();
787-
if (linkingNavigationProperty == null)
790+
if (linkingNavigationProperty == null
791+
|| (linkingForeignKey.PrincipalToDependent!.IsIndexerProperty()
792+
&& !QuirkEnabled27504))
788793
{
789794
leastPriorityProperties.AddRange(properties);
790795

test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs

+31
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,37 @@ public void Can_add_tables_with_entity_splitting_with_seed_data()
14591459
Assert.Equal("Animal", m.Name);
14601460
}));
14611461

1462+
[ConditionalFact]
1463+
public void Add_owned_types()
1464+
=> Execute(
1465+
_ => { },
1466+
_ => { },
1467+
modelBuilder =>
1468+
{
1469+
modelBuilder.Entity(
1470+
"Order",
1471+
x =>
1472+
{
1473+
x.Property<int>("Id");
1474+
x.OwnsOne("Address", "ShippingAddress");
1475+
x.OwnsOne("Address", "BillingAddress");
1476+
});
1477+
},
1478+
upOps => Assert.Collection(
1479+
upOps,
1480+
o =>
1481+
{
1482+
var m = Assert.IsType<CreateTableOperation>(o);
1483+
Assert.Equal("Order", m.Name);
1484+
}),
1485+
downOps => Assert.Collection(
1486+
downOps,
1487+
o =>
1488+
{
1489+
var m = Assert.IsType<DropTableOperation>(o);
1490+
Assert.Equal("Order", m.Name);
1491+
}));
1492+
14621493
[ConditionalFact]
14631494
public void Add_owned_type_with_seed_data()
14641495
=> Execute(

test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8883,13 +8883,13 @@ CONSTRAINT [PK_Entity_NestedCollection] PRIMARY KEY ([OwnedEntityId], [Id]),
88838883
CONSTRAINT [FK_Entity_NestedCollection_Entity_OwnedEntityId] FOREIGN KEY ([OwnedEntityId]) REFERENCES [Entity] ([Id]) ON DELETE CASCADE
88848884
);
88858885
""",
8886-
//
8886+
//
88878887
"""
88888888
CREATE TABLE [Entity_OwnedCollection] (
88898889
[EntityId] int NOT NULL,
88908890
[Id] int NOT NULL IDENTITY,
8891-
[NestedReference2_Number3] int NULL,
88928891
[Date2] datetime2 NOT NULL,
8892+
[NestedReference2_Number3] int NULL,
88938893
CONSTRAINT [PK_Entity_OwnedCollection] PRIMARY KEY ([EntityId], [Id]),
88948894
CONSTRAINT [FK_Entity_OwnedCollection_Entity_EntityId] FOREIGN KEY ([EntityId]) REFERENCES [Entity] ([Id]) ON DELETE CASCADE
88958895
);

0 commit comments

Comments
 (0)