From 685a3ca310af6d48dced1976be3f41140078f5e4 Mon Sep 17 00:00:00 2001 From: Arnaud Boussaer Date: Fri, 22 Mar 2024 10:13:28 +0100 Subject: [PATCH] #543 Add generate option for data mapping classes --- .../CodeGenerator.cs | 3 +++ src/EntityFrameworkCore.Generator.Core/OptionMapper.cs | 1 + .../Options/MappingClassOptions.cs | 10 ++++++++++ .../Serialization/MappingClass.cs | 9 +++++++++ .../Options/full.yaml | 1 + 5 files changed, 24 insertions(+) diff --git a/src/EntityFrameworkCore.Generator.Core/CodeGenerator.cs b/src/EntityFrameworkCore.Generator.Core/CodeGenerator.cs index 2d520e7d..f1e3165e 100644 --- a/src/EntityFrameworkCore.Generator.Core/CodeGenerator.cs +++ b/src/EntityFrameworkCore.Generator.Core/CodeGenerator.cs @@ -90,6 +90,9 @@ private void GenerateQueryExtensions(EntityContext entityContext) private void GenerateMappingClasses(EntityContext entityContext) { + if (!Options.Data.Mapping.Generate) + return; + foreach (var entity in entityContext.Entities) { Options.Variables.Set(entity); diff --git a/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs b/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs index ad837307..eb6454c5 100644 --- a/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs +++ b/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs @@ -143,6 +143,7 @@ private static void MapMapping(MappingClassOptions option, MappingClass mapping) { MapClassBase(option, mapping); + option.Generate = mapping.Generate; option.Temporal = mapping.Temporal; option.RowVersion = mapping.RowVersion; } diff --git a/src/EntityFrameworkCore.Generator.Core/Options/MappingClassOptions.cs b/src/EntityFrameworkCore.Generator.Core/Options/MappingClassOptions.cs index c5ad6fc1..1c353007 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/MappingClassOptions.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/MappingClassOptions.cs @@ -17,8 +17,18 @@ public MappingClassOptions(VariableDictionary variables, string prefix) Namespace = "{Project.Namespace}.Data.Mapping"; Directory = @"{Project.Directory}\Data\Mapping"; Name = "{Entity.Name}Map"; + + Generate = true; } + /// + /// Gets or sets a value indicating whether this option is generated. + /// + /// + /// true to generate; otherwise, false. + /// + public bool Generate { get; set; } + /// /// Gets or sets if temporal table mapping is enabled. Default true /// diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/MappingClass.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/MappingClass.cs index b5c4706a..21a0e8ee 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/MappingClass.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/MappingClass.cs @@ -19,6 +19,15 @@ public MappingClass() Name = "{Entity.Name}Map"; } + /// + /// Gets or sets a value indicating whether this option is generated. + /// + /// + /// true to generate; otherwise, false. + /// + [DefaultValue(true)] + public bool Generate { get; set; } = true; + /// /// Gets or sets if temporal table mapping is enabled. Default true /// diff --git a/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml b/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml index 5d253569..a97307ad 100644 --- a/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml +++ b/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml @@ -40,6 +40,7 @@ data: properties: - ^{Table.Name}(?=Id|Name) mapping: + generate: true namespace: '{Project.Namespace}.Data.Mapping' directory: '{Project.Directory}\Data\Mapping' document: false