Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ private void CreateProperties(Entity entity, DatabaseTable tableSchema)

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

if (IsIgnored(foreignKey, _options.Database.Exclude.Relationships))
{
_logger.LogDebug(" Skipping Relationship : {name}", foreignKey.Name);
continue;
}

CreateRelationship(entityContext, entity, foreignKey);
}

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

private static bool IsIgnored(DatabaseForeignKey relationship, IEnumerable<MatchOptions> exclude)
{
var table = relationship.Table;
var name = $"{table.Schema}.{table.Name}.{relationship.Name}";
var includeExpressions = Enumerable.Empty<MatchOptions>();
var excludeExpressions = exclude ?? [];

return IsIgnored(name, excludeExpressions, includeExpressions);
}

private static bool IsIgnored<TOption>(Property property, TOption options, SharedModelOptions sharedOptions)
where TOption : ModelOptionsBase
{
Expand Down
6 changes: 6 additions & 0 deletions src/EntityFrameworkCore.Generator.Core/OptionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ private static void MapDatabaseMatch(DatabaseMatchOptions option, DatabaseMatchM
var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Column{option.Columns?.Count:0000}");
return MapMatch(option.Variables, match, prefix);
});

MapList(option.Relationships, match.Relationships, (match) =>
{
var prefix = OptionsBase.AppendPrefix(option.Prefix, $"Relationship{option.Relationships?.Count:0000}");
return MapMatch(option.Variables, match, prefix);
});
}

private static void MapList<T>(IList<T> targetList, IList<T>? sourceList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public DatabaseMatchOptions(VariableDictionary variables, string? prefix)
{
Tables = [];
Columns = [];
Relationships = [];
}

/// <summary>
Expand All @@ -34,4 +35,12 @@ public DatabaseMatchOptions(VariableDictionary variables, string? prefix)
/// The list of regular expression of columns to ignore.
/// </value>
public List<MatchOptions> Columns { get; set; }

/// <summary>
/// Gets or sets a list of regular expression of relationships to ignore.
/// </summary>
/// <value>
/// The list of regular expression of relationships to ignore.
/// </value>
public List<MatchOptions> Relationships { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ public class DatabaseMatchModel
/// The list of regular expression of columns to ignore.
/// </value>
public List<MatchModel>? Columns { get; set; }

/// <summary>
/// Gets or sets a list of regular expression of relationships to ignore.
/// </summary>
/// <value>
/// The list of regular expression of relationships to ignore.
/// </value>
public List<MatchModel>? Relationships { get; set; }
}
Loading