Skip to content

Commit 12614cd

Browse files
authored
Ensure columns of relationships (FK -> PK/AK) are applied in the original order, when scaffolding. (#104)
1 parent 7d8a2f8 commit 12614cd

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

src/EFCore.Jet.Data/AdoxSchema.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,8 @@ public override DataTable GetRelationColumns()
471471
dataTable.Rows.Add(
472472
relationName,
473473
referencingColumnName,
474-
principalColumnName);
474+
principalColumnName,
475+
k + 1);
475476
}
476477
}
477478
}

src/EFCore.Jet.Data/DaoSchema.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ public override DataTable GetRelationColumns()
470470
dataTable.Rows.Add(
471471
relationName,
472472
referencingColumnName,
473-
principalColumnName);
473+
principalColumnName,
474+
j + 1);
474475
}
475476
}
476477

src/EFCore.Jet.Data/JetStoreSchemaDefinition/SchemaTables.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public static DataTable GetRelationColumnsDataTable()
110110
new DataColumn("RELATION_NAME", typeof(string)),
111111
new DataColumn("REFERENCING_COLUMN_NAME", typeof(string)),
112112
new DataColumn("PRINCIPAL_COLUMN_NAME", typeof(string)),
113+
new DataColumn("ORDINAL_POSITION", typeof(int)),
113114
});
114115

115116
return dataTable;

src/EFCore.Jet/Scaffolding/Internal/JetDatabaseModelFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ private void GetRelations(DbConnection connection, IReadOnlyList<DatabaseTable>
443443
var relationColumnsTable = new DataTable();
444444
using (var command = connection.CreateCommand())
445445
{
446-
command.CommandText = "SELECT * FROM `INFORMATION_SCHEMA.RELATION_COLUMNS` ORDER BY RELATION_NAME, REFERENCING_COLUMN_NAME, PRINCIPAL_COLUMN_NAME";
446+
command.CommandText = "SELECT * FROM `INFORMATION_SCHEMA.RELATION_COLUMNS` ORDER BY RELATION_NAME, ORDINAL_POSITION"; // no sorting would be fine as well
447447
using var reader = command.ExecuteReader();
448448
relationColumnsTable.Load(reader);
449449
}

test/EFCore.Jet.Data.Tests/JetInformationSchemaTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ public void RelationColumns(DataAccessProviderType providerType)
229229
ORDER BY RELATION_NAME, REFERENCING_COLUMN_NAME, PRINCIPAL_COLUMN_NAME");
230230

231231
AssertDataReaderContent(result, @"
232-
`RELATION_NAME` | `REFERENCING_COLUMN_NAME` | `PRINCIPAL_COLUMN_NAME`
233-
--- | --- | ---
234-
FK_Order_Details_Orders | OrderID | OrderID");
232+
`RELATION_NAME` | `REFERENCING_COLUMN_NAME` | `PRINCIPAL_COLUMN_NAME` | `ORDINAL_POSITION`
233+
--- | --- | --- | ---
234+
FK_Order_Details_Orders | OrderID | OrderID | 1");
235235
}
236236

237237
[DataTestMethod]

test/EFCore.Jet.FunctionalTests/Scaffolding/JetDatabaseModelFactoryTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,36 @@ FOREIGN KEY (ForeignKeyId) REFERENCES PrincipalTable(Id) ON DELETE SET NULL
15111511
DROP TABLE PrincipalTable;");
15121512
}
15131513

1514+
[ConditionalFact]
1515+
public void Relationship_column_order_is_as_defined()
1516+
{
1517+
Test(
1518+
@"
1519+
CREATE TABLE PrincipalTable (
1520+
IdB int,
1521+
IdA int,
1522+
PRIMARY KEY (IdB, IdA)
1523+
);
1524+
1525+
CREATE TABLE DependentTable (
1526+
Id int PRIMARY KEY,
1527+
ForeignKeyIdB int,
1528+
ForeignKeyIdA int,
1529+
FOREIGN KEY (ForeignKeyIdB, ForeignKeyIdA) REFERENCES PrincipalTable(IdB, IdA)
1530+
);",
1531+
Enumerable.Empty<string>(),
1532+
Enumerable.Empty<string>(),
1533+
dbModel =>
1534+
{
1535+
var fk = Assert.Single(dbModel.Tables.Single(t => t.Name == "DependentTable").ForeignKeys);
1536+
1537+
Assert.Equal(fk.PrincipalColumns, fk.PrincipalTable.PrimaryKey.Columns);
1538+
},
1539+
@"
1540+
DROP TABLE DependentTable;
1541+
DROP TABLE PrincipalTable;");
1542+
}
1543+
15141544
#endregion
15151545

15161546
#region Warnings

0 commit comments

Comments
 (0)