Skip to content

Commit 24d4490

Browse files
committed
change database.exclude to include tables and columns
1 parent 271f0cd commit 24d4490

File tree

11 files changed

+139
-23
lines changed

11 files changed

+139
-23
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,28 @@ Entity Framework Core Generator supports generating [Read](https://efg.loresoft.
155155

156156
## Change Log
157157

158+
### Version 7.0
159+
160+
- support for .net 9
161+
- update project to use nullable reference types
162+
- Breaking change to configuration file. The `database.exclude` section now has a `database.exclude.tables` and `database.exclude.columns` section.
163+
164+
configuration file update example:
165+
166+
```YAML
167+
database:
168+
# exclude tables or columns
169+
exclude:
170+
# list of expressions for tables to exclude, source is Schema.TableName
171+
tables:
172+
- exact: dbo.SchemaVersions
173+
- regex: dbo\.SchemaVersions$
174+
# list of expressions for columns to exclude, source is Schema.TableName.ColumnName
175+
columns:
176+
- exact: dbo.User.Password
177+
- regex: dbo\.User\.Password$
178+
```
179+
158180
### Version 6.0
159181

160182
- upgrade to .net 8

docs/configuration.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,17 @@ database:
4646
schemas:
4747
- dbo
4848

49-
# list of expressions for tables to exclude, source is Schema.TableName
49+
# exclude tables or columns: Changed in v7.x
5050
exclude:
51-
- exact: dbo.SchemaVersions
52-
- regex: dbo\.SchemaVersions$
51+
# list of expressions for tables to exclude, source is Schema.TableName
52+
tables:
53+
- exact: dbo.SchemaVersions
54+
- regex: dbo\.SchemaVersions$
55+
# list of expressions for columns to exclude, source is Schema.TableName.ColumnName
56+
columns:
57+
- exact: dbo.User.Password
58+
- regex: dbo\.User\.Password$
59+
5360

5461
# table naming hint for how existing tables are named. Default: Singular
5562
tableNaming: Mixed|Plural|Singular

sample/Tracker/Tracker.Core/generation.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ database:
77
provider: SqlServer
88
connectionString: Data Source=(local);Initial Catalog=Tracker;Integrated Security=True;TrustServerCertificate=True
99
exclude:
10-
- exact: 'dbo.SchemaVersions'
10+
tables:
11+
- exact: 'dbo.SchemaVersions'
1112
data:
1213
context:
1314
name: '{Database.Name}Context'

src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public EntityContext Generate(GeneratorOptions options, DatabaseModel databaseMo
6767

6868
foreach (var table in tables)
6969
{
70-
if (IsIgnored(table, _options.Database.Exclude))
70+
if (IsIgnored(table, _options.Database.Exclude.Tables))
7171
{
7272
_logger.LogDebug(" Skipping Table : {schema}.{name}", table.Schema, table.Name);
7373
continue;
@@ -175,6 +175,11 @@ private void CreateProperties(Entity entity, DatabaseTable tableSchema)
175175
foreach (var column in columns)
176176
{
177177
var table = column.Table;
178+
if (IsIgnored(column, _options.Database.Exclude.Columns))
179+
{
180+
_logger.LogDebug(" Skipping Column : {Schema}.{Table}.{Column}", table.Schema, table.Name, column.Name);
181+
continue;
182+
}
178183

179184
var mapping = column.StoreType.HasValue() ? _typeMapper.FindMapping(column.StoreType) : null;
180185
if (mapping == null)
@@ -309,7 +314,7 @@ private void CreateRelationships(EntityContext entityContext, Entity entity, Dat
309314
foreach (var foreignKey in tableSchema.ForeignKeys)
310315
{
311316
// skip relationship if principal table is ignored
312-
if (IsIgnored(foreignKey.PrincipalTable, _options.Database.Exclude))
317+
if (IsIgnored(foreignKey.PrincipalTable, _options.Database.Exclude.Tables))
313318
{
314319
_logger.LogDebug(" Skipping Relationship : {name}", foreignKey.Name);
315320
continue;
@@ -728,6 +733,16 @@ private static bool IsIgnored(DatabaseTable table, IEnumerable<MatchOptions> exc
728733
return IsIgnored(name, excludeExpressions, includeExpressions);
729734
}
730735

736+
private static bool IsIgnored(DatabaseColumn column, IEnumerable<MatchOptions> exclude)
737+
{
738+
var table = column.Table;
739+
var name = $"{table.Schema}.{table.Name}.{column.Name}";
740+
var includeExpressions = Enumerable.Empty<MatchOptions>();
741+
var excludeExpressions = exclude ?? [];
742+
743+
return IsIgnored(name, excludeExpressions, includeExpressions);
744+
}
745+
731746
private static bool IsIgnored<TOption>(Property property, TOption options, SharedModelOptions sharedOptions)
732747
where TOption : ModelOptionsBase
733748
{

src/EntityFrameworkCore.Generator.Core/OptionMapper.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,8 @@ private static void MapDatabase(DatabaseOptions option, DatabaseModel database)
186186

187187
MapList(option.Tables, database.Tables);
188188
MapList(option.Schemas, database.Schemas);
189-
MapList(option.Exclude, database.Exclude, (match) =>
190-
{
191-
var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Exclude{option.Exclude.Count:0000}");
192-
return MapMatch(option.Variables, match, prefix);
193-
});
189+
190+
MapDatabaseMatch(option.Exclude, database.Exclude);
194191
}
195192

196193
private static void MapProject(ProjectOptions option, ProjectModel project)
@@ -201,6 +198,23 @@ private static void MapProject(ProjectOptions option, ProjectModel project)
201198
option.FileScopedNamespace = project.FileScopedNamespace;
202199
}
203200

201+
private static void MapDatabaseMatch(DatabaseMatchOptions option, DatabaseMatchModel? match)
202+
{
203+
if (match == null)
204+
return;
205+
206+
MapList(option.Tables, match.Tables, (match) =>
207+
{
208+
var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Table{option.Tables?.Count:0000}");
209+
return MapMatch(option.Variables, match, prefix);
210+
});
211+
212+
MapList(option.Columns, match.Columns, (match) =>
213+
{
214+
var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Column{option.Columns?.Count:0000}");
215+
return MapMatch(option.Variables, match, prefix);
216+
});
217+
}
204218

205219
private static void MapList<T>(IList<T> targetList, IList<T>? sourceList)
206220
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using EntityFrameworkCore.Generator.Serialization;
2+
3+
namespace EntityFrameworkCore.Generator.Options;
4+
5+
/// <summary>
6+
/// Represents options for matching database tables and columns during a database operation.
7+
/// </summary>
8+
public class DatabaseMatchOptions : OptionsBase
9+
{
10+
/// <summary>
11+
/// Represents options for matching database tables and columns during a database operation.
12+
/// </summary>
13+
/// <param name="variables">A dictionary of variables used to configure the matching options. Cannot be null.</param>
14+
/// <param name="prefix">An optional prefix used to filter or qualify the matching criteria. Can be null.</param>
15+
public DatabaseMatchOptions(VariableDictionary variables, string? prefix)
16+
: base(variables, prefix)
17+
{
18+
Tables = [];
19+
Columns = [];
20+
}
21+
22+
/// <summary>
23+
/// Gets or sets a list of regular expression of tables to ignore.
24+
/// </summary>
25+
/// <value>
26+
/// The list of regular expression of tables to ignore.
27+
/// </value>
28+
public List<MatchOptions> Tables { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets a list of regular expression of columns to ignore.
32+
/// </summary>
33+
/// <value>
34+
/// The list of regular expression of columns to ignore.
35+
/// </value>
36+
public List<MatchOptions> Columns { get; set; }
37+
}

src/EntityFrameworkCore.Generator.Core/Options/DatabaseOptions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
// Ignore Spelling: Schemas
2+
23
using System.ComponentModel;
34

45
namespace EntityFrameworkCore.Generator.Options;
@@ -20,7 +21,7 @@ public DatabaseOptions(VariableDictionary variables, string? prefix)
2021
TableNaming = TableNaming.Singular;
2122
Tables = [];
2223
Schemas = [];
23-
Exclude = [];
24+
Exclude = new DatabaseMatchOptions(Variables, Prefix);
2425
}
2526

2627
/// <summary>
@@ -104,6 +105,6 @@ public string? Name
104105
/// <value>
105106
/// The exclude table options.
106107
/// </value>
107-
public List<MatchOptions> Exclude { get; }
108+
public DatabaseMatchOptions Exclude { get; }
108109

109110
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace EntityFrameworkCore.Generator.Serialization;
2+
3+
/// <summary>
4+
/// Represents a model that specifies patterns for tables and columns to be ignored during processing.
5+
/// </summary>
6+
public class DatabaseMatchModel
7+
{
8+
/// <summary>
9+
/// Gets or sets a list of regular expression of tables to ignore.
10+
/// </summary>
11+
/// <value>
12+
/// The list of regular expression of tables to ignore.
13+
/// </value>
14+
public List<MatchModel>? Tables { get; set; }
15+
16+
/// <summary>
17+
/// Gets or sets a list of regular expression of columns to ignore.
18+
/// </summary>
19+
/// <value>
20+
/// The list of regular expression of columns to ignore.
21+
/// </value>
22+
public List<MatchModel>? Columns { get; set; }
23+
}

src/EntityFrameworkCore.Generator.Core/Serialization/DatabaseModel.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,7 @@ public class DatabaseModel
7272
public List<string>? Schemas { get; set; }
7373

7474
/// <summary>
75-
/// Gets or sets the exclude table options.
75+
/// Gets or sets the model that specifies which elements should be ignored during processing.
7676
/// </summary>
77-
/// <value>
78-
/// The exclude table options.
79-
/// </value>
80-
public List<MatchModel>? Exclude { get; set; }
81-
77+
public DatabaseMatchModel? Exclude { get; set; }
8278
}

src/EntityFrameworkCore.Generator/GenerateCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected override int OnExecute(CommandLineApplication application)
7676
if (Validator.HasValue)
7777
configuration.Model.Validator.Generate = Validator.Value;
7878

79-
// conver to options format to support variables
79+
// convert to options format to support variables
8080
var options = OptionMapper.Map(configuration);
8181

8282
var result = _codeGenerator.Generate(options);

0 commit comments

Comments
 (0)