Skip to content

Commit 5ebc5c4

Browse files
committed
Add option to exclude relationships
1 parent 5e467e5 commit 5ebc5c4

4 files changed

Lines changed: 40 additions & 1 deletion

File tree

src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private void CreateProperties(Entity entity, DatabaseTable tableSchema)
311311

312312
private void CreateRelationships(EntityContext entityContext, Entity entity, DatabaseTable tableSchema)
313313
{
314-
foreach (var foreignKey in tableSchema.ForeignKeys)
314+
foreach (var foreignKey in tableSchema.ForeignKeys.OrderBy(fk => fk.Name))
315315
{
316316
// skip relationship if principal table is ignored
317317
if (IsIgnored(foreignKey.PrincipalTable, _options.Database.Exclude.Tables))
@@ -320,6 +320,12 @@ private void CreateRelationships(EntityContext entityContext, Entity entity, Dat
320320
continue;
321321
}
322322

323+
if (IsIgnored(foreignKey, _options.Database.Exclude.Relationships))
324+
{
325+
_logger.LogDebug(" Skipping Relationship : {name}", foreignKey.Name);
326+
continue;
327+
}
328+
323329
CreateRelationship(entityContext, entity, foreignKey);
324330
}
325331

@@ -748,6 +754,16 @@ private static bool IsIgnored(DatabaseColumn column, IEnumerable<MatchOptions> e
748754
return IsIgnored(name, excludeExpressions, includeExpressions);
749755
}
750756

757+
private static bool IsIgnored(DatabaseForeignKey relationship, IEnumerable<MatchOptions> exclude)
758+
{
759+
var table = relationship.Table;
760+
var name = $"{table.Schema}.{table.Name}.{relationship.Name}";
761+
var includeExpressions = Enumerable.Empty<MatchOptions>();
762+
var excludeExpressions = exclude ?? [];
763+
764+
return IsIgnored(name, excludeExpressions, includeExpressions);
765+
}
766+
751767
private static bool IsIgnored<TOption>(Property property, TOption options, SharedModelOptions sharedOptions)
752768
where TOption : ModelOptionsBase
753769
{

src/EntityFrameworkCore.Generator.Core/OptionMapper.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ private static void MapDatabaseMatch(DatabaseMatchOptions option, DatabaseMatchM
216216
var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Column{option.Columns?.Count:0000}");
217217
return MapMatch(option.Variables, match, prefix);
218218
});
219+
220+
MapList(option.Relationships, match.Relationships, (match) =>
221+
{
222+
var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Relationship{option.Relationships?.Count:0000}");
223+
return MapMatch(option.Variables, match, prefix);
224+
});
219225
}
220226

221227
private static void MapList<T>(IList<T> targetList, IList<T>? sourceList)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public DatabaseMatchOptions(VariableDictionary variables, string? prefix)
1717
{
1818
Tables = [];
1919
Columns = [];
20+
Relationships = [];
2021
}
2122

2223
/// <summary>
@@ -34,4 +35,12 @@ public DatabaseMatchOptions(VariableDictionary variables, string? prefix)
3435
/// The list of regular expression of columns to ignore.
3536
/// </value>
3637
public List<MatchOptions> Columns { get; set; }
38+
39+
/// <summary>
40+
/// Gets or sets a list of regular expression of relationships to ignore.
41+
/// </summary>
42+
/// <value>
43+
/// The list of regular expression of relationships to ignore.
44+
/// </value>
45+
public List<MatchOptions> Relationships { get; set; }
3746
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ public class DatabaseMatchModel
2020
/// The list of regular expression of columns to ignore.
2121
/// </value>
2222
public List<MatchModel>? Columns { get; set; }
23+
24+
/// <summary>
25+
/// Gets or sets a list of regular expression of relationships to ignore.
26+
/// </summary>
27+
/// <value>
28+
/// The list of regular expression of relationships to ignore.
29+
/// </value>
30+
public List<MatchModel>? Relationships { get; set; }
2331
}

0 commit comments

Comments
 (0)