Skip to content

Commit d8d4744

Browse files
authored
Add option to ignore msys tables when scaffolding (#236)
* Add option to ignore msys tables when scaffolding
1 parent 183a5b9 commit d8d4744

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

src/EFCore.Jet.Data/AdoxSchema.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class AdoxSchema : SchemaProvider
1313
private readonly dynamic _connection;
1414
private readonly dynamic _catalog;
1515

16+
private bool _ignoreMsys;
1617
public AdoxSchema(JetConnection connection, bool naturalOnly, bool readOnly)
1718
: this(connection, readOnly)
1819
{
@@ -22,7 +23,7 @@ public AdoxSchema(JetConnection connection, bool naturalOnly, bool readOnly)
2223
public AdoxSchema(JetConnection connection, bool readOnly)
2324
{
2425
_connection = new ComObject("ADODB.Connection");
25-
26+
_ignoreMsys = connection.IgnoreMsys;
2627
try
2728
{
2829
var connectionString = GetOleDbConnectionString(connection.ActiveConnectionString);
@@ -105,6 +106,11 @@ public override DataTable GetTables()
105106

106107
var tableName = (string)table.Name;
107108

109+
if (tableName.StartsWith("MSys", StringComparison.OrdinalIgnoreCase) && _ignoreMsys)
110+
{
111+
continue;
112+
}
113+
108114
// Depending on the provider (ODBC or OLE DB) used, the Tables collection might contain VIEWs
109115
// that take parameters, which makes them procedures.
110116
// We make sure here, that we exclude any procedures from the returned table list.
@@ -202,6 +208,11 @@ public override DataTable GetColumns()
202208
using var table = tables[i];
203209
var tableName = (string)table.Name;
204210

211+
if (tableName.StartsWith("MSys", StringComparison.OrdinalIgnoreCase) && _ignoreMsys)
212+
{
213+
continue;
214+
}
215+
205216
using var columns = table.Columns;
206217
var columnCount = columns.Count;
207218

@@ -282,6 +293,11 @@ public override DataTable GetIndexes()
282293
using var table = tables[i];
283294
var tableName = (string)table.Name;
284295

296+
if (tableName.StartsWith("MSys", StringComparison.OrdinalIgnoreCase) && _ignoreMsys)
297+
{
298+
continue;
299+
}
300+
285301
using var indexes = table.Indexes;
286302
var indexCount = (int)indexes.Count;
287303

@@ -337,6 +353,11 @@ public override DataTable GetIndexColumns()
337353
using var table = tables[i];
338354
var tableName = (string)table.Name;
339355

356+
if (tableName.StartsWith("MSys", StringComparison.OrdinalIgnoreCase) && _ignoreMsys)
357+
{
358+
continue;
359+
}
360+
340361
using var indexes = table.Indexes;
341362
var indexCount = (int)indexes.Count;
342363

@@ -384,6 +405,11 @@ public override DataTable GetRelations()
384405
using var table = tables[i];
385406
var referencingTableName = (string)table.Name;
386407

408+
if (table.Name.StartsWith("MSys", StringComparison.OrdinalIgnoreCase) && _ignoreMsys)
409+
{
410+
continue;
411+
}
412+
387413
using var keys = table.Keys;
388414
var keyCount = (int)keys.Count;
389415

@@ -446,6 +472,11 @@ public override DataTable GetRelationColumns()
446472
{
447473
using var table = tables[i];
448474

475+
if (table.Name.StartsWith("MSys", StringComparison.OrdinalIgnoreCase) && _ignoreMsys)
476+
{
477+
continue;
478+
}
479+
449480
using var keys = table.Keys;
450481
var keyCount = (int)keys.Count;
451482

src/EFCore.Jet.Data/JetConnection.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class JetConnection : DbConnection, IDisposable, ICloneable
2828
internal string? FileNameOrConnectionString => ConnectionString;
2929

3030
public const string DefaultDualTableName = "#Dual";
31-
31+
private bool _ignoreMSys;
3232
/// <summary>
3333
/// Initializes a new instance of the <see cref="JetConnection"/> class.
3434
/// </summary>
@@ -81,6 +81,8 @@ public JetConnection(string? fileNameOrConnectionString, DbProviderFactory? data
8181
/// </value>
8282
public bool IsEmpty { get; set; }
8383

84+
public bool IgnoreMsys => _ignoreMSys;
85+
8486
/// <summary>
8587
/// Gets the <see cref="T:System.Data.Common.DbProviderFactory" /> for this <see cref="T:System.Data.Common.DbConnection" />.
8688
/// </summary>
@@ -358,6 +360,12 @@ public override void Open()
358360
var connectionStringBuilder = DataAccessProviderFactory.CreateConnectionStringBuilder();
359361
connectionStringBuilder.ConnectionString = connectionString;
360362

363+
if (connectionStringBuilder.Remove("IgnoreMsys"))
364+
{
365+
_ignoreMSys = true;
366+
connectionString = connectionStringBuilder.ToString();
367+
}
368+
361369
if (string.IsNullOrWhiteSpace(connectionStringBuilder.GetProvider()))
362370
{
363371
var provider = GetMostRecentCompatibleProviders(dataAccessProviderType.Value)

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ private static string ColumnKey(DatabaseTable table, string columnName)
5252

5353
private readonly IDiagnosticsLogger<DbLoggerCategory.Scaffolding> _logger;
5454
private readonly IRelationalTypeMappingSource _typeMappingSource;
55+
56+
private bool _ignoreMsys = false;
57+
private List<string> _msysNames = new List<string>();
5558
/// <summary>
5659
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
5760
/// directly from your code. This API may change or be removed in future releases.
@@ -75,7 +78,6 @@ public override DatabaseModel Create(string connectionString, DatabaseModelFacto
7578
{
7679
Check.NotEmpty(connectionString, nameof(connectionString));
7780
Check.NotNull(options, nameof(options));
78-
7981
using var connection = new JetConnection(connectionString);
8082
return Create(connection, options);
8183
}
@@ -99,6 +101,7 @@ public override DatabaseModel Create(
99101
if (!connectionStartedOpen)
100102
{
101103
connection.Open();
104+
_ignoreMsys = ((JetConnection)connection).IgnoreMsys;
102105
}
103106

104107
try
@@ -133,11 +136,11 @@ public override DatabaseModel Create(
133136
}
134137

135138
var tableNames = databaseModel.Tables.Select(t => t.Name).ToList();
136-
if (tableNames.Contains("MSysAccessStorage"))
139+
if (tableNames.Contains("MSysAccessStorage") || _msysNames.Contains("MSysAccessStorage"))
137140
{
138141
JetConfiguration.DetectedDualTableName = "MSysAccessStorage";
139142
}
140-
else if (tableNames.Contains("MSysRelationships"))
143+
else if (tableNames.Contains("MSysRelationships") || _msysNames.Contains("MSysRelationships"))
141144
{
142145
JetConfiguration.DetectedDualTableName = "MSysRelationships";
143146
}
@@ -196,8 +199,13 @@ private IReadOnlyList<DatabaseTable> GetTables(
196199
? new DatabaseTable() { Database = databaseModel, Name = name! }
197200
: new DatabaseView() { Database = databaseModel, Name = name! };
198201

202+
var isMsys = table.Name.StartsWith("MSys", StringComparison.OrdinalIgnoreCase);
203+
if (isMsys)
204+
{
205+
_msysNames.Add(table.Name);
206+
}
199207
var isValidByFilter = filter?.Invoke(table.Schema!, table.Name) ?? true;
200-
if (isValidByFilter)
208+
if (isValidByFilter && !(_ignoreMsys && isMsys))
201209
{
202210
tables.Add(table);
203211
}

0 commit comments

Comments
 (0)