From a9789216d20fed3ee8ba9b6d4d88c02f431476ac Mon Sep 17 00:00:00 2001 From: Christopher Jolly Date: Sun, 19 Feb 2023 14:23:44 +0800 Subject: [PATCH] Dual table: Auto detect the dual table name on model load. Use that table for any queries. Also allow a custom override name to be set --- src/EFCore.Jet.Data/JetConfiguration.cs | 20 ++++++++----------- .../Sql/Internal/JetQuerySqlGenerator.cs | 2 +- .../Internal/JetDatabaseModelFactory.cs | 16 ++++++++++++++- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/EFCore.Jet.Data/JetConfiguration.cs b/src/EFCore.Jet.Data/JetConfiguration.cs index 314976e..b868819 100644 --- a/src/EFCore.Jet.Data/JetConfiguration.cs +++ b/src/EFCore.Jet.Data/JetConfiguration.cs @@ -33,8 +33,8 @@ namespace EntityFrameworkCore.Jet.Data } } - public static DataAccessProviderType DefaultDataAccessProviderType { get; set; } = DataAccessProviderType.Odbc; - + public static DataAccessProviderType DefaultDataAccessProviderType { get; set; } = DataAccessProviderType.Odbc; + // The SQL statement // // (SELECT COUNT(*) FROM MSysRelationships) @@ -57,17 +57,13 @@ namespace EntityFrameworkCore.Jet.Data /// /// The DUAL table or query /// - public static string DUAL { get; set; } = DUALForAccdb; + public static string CustomDualTableName = ""; + //MSysRelationships + //MSysAccessStorage + //#Dual + //(SELECT COUNT(*) FROM MSysAccessStorage) - /// - /// The dual table for accdb - /// - public const string DUALForMdb = "(SELECT COUNT(*) FROM MSysRelationships)"; - - /// - /// The dual table for accdb - /// - public const string DUALForAccdb = "(SELECT COUNT(*) FROM MSysAccessStorage)"; + public static string DetectedDualTableName = "#Dual"; /// /// Gets or sets a value indicating whether show SQL statements. diff --git a/src/EFCore.Jet/Query/Sql/Internal/JetQuerySqlGenerator.cs b/src/EFCore.Jet/Query/Sql/Internal/JetQuerySqlGenerator.cs index 7032ff8..25357d2 100644 --- a/src/EFCore.Jet/Query/Sql/Internal/JetQuerySqlGenerator.cs +++ b/src/EFCore.Jet/Query/Sql/Internal/JetQuerySqlGenerator.cs @@ -224,7 +224,7 @@ namespace EntityFrameworkCore.Jet.Query.Sql.Internal protected override void GeneratePseudoFromClause() { Sql.AppendLine() - .Append("FROM " + JetConfiguration.DUAL); + .Append("FROM " + "(SELECT COUNT(*) FROM `" + (string.IsNullOrEmpty(JetConfiguration.CustomDualTableName) ? JetConfiguration.DetectedDualTableName : JetConfiguration.CustomDualTableName) + "`)"); } private void GenerateList( diff --git a/src/EFCore.Jet/Scaffolding/Internal/JetDatabaseModelFactory.cs b/src/EFCore.Jet/Scaffolding/Internal/JetDatabaseModelFactory.cs index f0c08a4..4845c95 100644 --- a/src/EFCore.Jet/Scaffolding/Internal/JetDatabaseModelFactory.cs +++ b/src/EFCore.Jet/Scaffolding/Internal/JetDatabaseModelFactory.cs @@ -105,7 +105,21 @@ namespace EntityFrameworkCore.Jet.Scaffolding.Internal table.Database = databaseModel; databaseModel.Tables.Add(table); } - + + var tableNames = databaseModel.Tables.Select(t => t.Name).ToList(); + if (tableNames.Contains("MSysAccessStorage")) + { + JetConfiguration.DetectedDualTableName = "MSysAccessStorage"; + } + else if (tableNames.Contains("MSysRelationships")) + { + JetConfiguration.DetectedDualTableName = "MSysRelationships"; + } + else if (tableNames.Contains("#Dual")) + { + JetConfiguration.DetectedDualTableName = "#Dual"; + } + return databaseModel; } finally