From c10aad9b7cf47af151792a637b8de66eeab23e50 Mon Sep 17 00:00:00 2001 From: Simon Hughes Date: Thu, 20 Jan 2022 23:26:07 +0000 Subject: [PATCH] 739 Add ability to automatically add enum tables programatically. --- BuildTT/BuildTT.cs | 27 +++ .../Database.tt | 27 +++ .../EF.Reverse.POCO.v3.ttinclude | 215 +++++++++++------- Generator/Filtering/DbContextFilter.cs | 1 + Generator/Filtering/IDbContextFilter.cs | 1 + Generator/Filtering/MultiContextFilter.cs | 5 + Generator/Filtering/SingleContextFilter.cs | 5 + Generator/Generator.csproj | 1 - Generator/Generators/Generator.cs | 9 + Generator/Settings.cs | 27 +++ Tester.Integration.EFCore6/EnumOnly.tt | 36 ++- 11 files changed, 261 insertions(+), 93 deletions(-) diff --git a/BuildTT/BuildTT.cs b/BuildTT/BuildTT.cs index 5400648e..51effc82 100644 --- a/BuildTT/BuildTT.cs +++ b/BuildTT/BuildTT.cs @@ -144,6 +144,7 @@ private static void CreateTT(string generatorRoot, string ttRoot) // Enumerations *********************************************************************************************************************** // Create enumerations from database tables // List the enumeration tables you want read and generated for + // Also look at the AddEnum callback to add your own Settings.Enumerations = new List { // Example @@ -402,6 +403,32 @@ private static void CreateTT(string generatorRoot, string ttRoot) } }; + Settings.AddEnum = delegate (Table table) + { + /*if (table.HasPrimaryKey && table.PrimaryKeys.Count() == 1 && table.Columns.Any(x => x.PropertyType == ""string"")) + { + // Example IF to only choose tables with a certain naming conventions for enums + if (table.NameHumanCase.StartsWith(""REF_"", StringComparison.InvariantCultureIgnoreCase) || + table.NameHumanCase.EndsWith(""_LUT"", StringComparison.InvariantCultureIgnoreCase)) + { + try + { + Settings.Enumerations.Add(new EnumerationSettings + { + Name = table.NameHumanCase + ""Enum"", + Table = table.Schema.DbName + ""."" + table.DbName, + NameField = table.Columns.First(x => x.PropertyType == ""string"").DbName, // Or specify your own + ValueField = table.PrimaryKeys.Single().DbName // Or specify your own + }); + } + catch + { + // Swallow exception + } + } + }*/ + }; + // Use the following function if you need to apply additional modifications to a enum // Called just before UpdateEnumMember Settings.UpdateEnum = delegate (Enumeration enumeration) diff --git a/EntityFramework.Reverse.POCO.Generator/Database.tt b/EntityFramework.Reverse.POCO.Generator/Database.tt index 91e18deb..55fdf5ea 100644 --- a/EntityFramework.Reverse.POCO.Generator/Database.tt +++ b/EntityFramework.Reverse.POCO.Generator/Database.tt @@ -128,6 +128,7 @@ // Enumerations *********************************************************************************************************************** // Create enumerations from database tables // List the enumeration tables you want read and generated for + // Also look at the AddEnum callback to add your own Settings.Enumerations = new List { // Example @@ -386,6 +387,32 @@ } }; + Settings.AddEnum = delegate (Table table) + { + /*if (table.HasPrimaryKey && table.PrimaryKeys.Count() == 1 && table.Columns.Any(x => x.PropertyType == "string")) + { + // Example IF to only choose tables with a certain naming conventions for enums + if (table.NameHumanCase.StartsWith("REF_", StringComparison.InvariantCultureIgnoreCase) || + table.NameHumanCase.EndsWith("_LUT", StringComparison.InvariantCultureIgnoreCase)) + { + try + { + Settings.Enumerations.Add(new EnumerationSettings + { + Name = table.NameHumanCase + "Enum", + Table = table.Schema.DbName + "." + table.DbName, + NameField = table.Columns.First(x => x.PropertyType == "string").DbName, // Or specify your own + ValueField = table.PrimaryKeys.Single().DbName // Or specify your own + }); + } + catch + { + // Swallow exception + } + } + }*/ + }; + // Use the following function if you need to apply additional modifications to a enum // Called just before UpdateEnumMember Settings.UpdateEnum = delegate (Enumeration enumeration) diff --git a/EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude b/EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude index 7244449f..edb7a8e7 100644 --- a/EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude +++ b/EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude @@ -118,6 +118,7 @@ // Create enumerations from database tables // List the enumeration tables you want read and generated for + // Also look at the AddEnum callback to add your own public static List Enumerations = new List { // Example @@ -352,6 +353,32 @@ return ".UseIdentityColumn()"; }; + public static Action AddEnum = delegate (Table table) + { + /*if (table.HasPrimaryKey && table.PrimaryKeys.Count() == 1 && table.Columns.Any(x => x.PropertyType == "string")) + { + // Example IF to only choose tables with a certain naming conventions for enums + if (table.NameHumanCase.StartsWith("REF_", StringComparison.InvariantCultureIgnoreCase) || + table.NameHumanCase.EndsWith("_LUT", StringComparison.InvariantCultureIgnoreCase)) + { + try + { + Enumerations.Add(new EnumerationSettings + { + Name = table.NameHumanCase + "Enum", + Table = table.Schema.DbName + "." + table.DbName, + NameField = table.Columns.First(x => x.PropertyType == "string").DbName, // Or specify your own + ValueField = table.PrimaryKeys.Single().DbName // Or specify your own + }); + } + catch + { + // Swallow exception + } + } + }*/ + }; + // Use the following function if you need to apply additional modifications to a enum // Called just before UpdateEnumMember public static Action UpdateEnum = delegate (Enumeration enumeration) @@ -950,6 +977,11 @@ Settings.UpdateColumn?.Invoke(column, table, EnumDefinitions); } + public override void AddEnum(Table table) + { + Settings.AddEnum?.Invoke(table); + } + public override void UpdateEnum(Enumeration enumeration) { Settings.UpdateEnum?.Invoke(enumeration); @@ -1916,6 +1948,7 @@ public abstract string MappingTableRename(string mappingTable, string tableName, string entityName); public abstract void UpdateTable(Table table); public abstract void UpdateColumn(Column column, Table table); + public abstract void AddEnum(Table table); public abstract void UpdateEnum(Enumeration enumeration); public abstract void UpdateEnumMember(EnumerationMember enumerationMember); public abstract void ViewProcessing(Table view); @@ -2078,6 +2111,7 @@ string MappingTableRename(string mappingTable, string tableName, string entityName); void UpdateTable(Table table); void UpdateColumn(Column column, Table table); + void AddEnum(Table table); void UpdateEnum(Enumeration enumeration); void UpdateEnumMember(EnumerationMember enumerationMember); void ViewProcessing(Table view); @@ -2450,6 +2484,11 @@ Settings.UpdateColumn?.Invoke(column, table, null); } + public override void AddEnum(Table table) + { + + } + public override void UpdateEnum(Enumeration enumeration) { @@ -4016,7 +4055,6 @@ public bool InitialisationOk { get; private set; } protected DatabaseReader DatabaseReader; - protected FileHeaderFooter FileHeaderFooter; public readonly IDbContextFilterList FilterList; protected abstract bool AllowFkToNonPrimaryKey(); @@ -4032,10 +4070,9 @@ protected abstract string GetCascadeOnDelete(bool cascadeOnDelete); protected abstract string GetForeignKeyConstraintName(string foreignKeyConstraintName); - - private DbProviderFactory _factory; - public bool HasAcademicLicence; - public bool HasTrialLicence; + private bool _hasAcademicLicence; + private bool _hasTrialLicence; + private FileHeaderFooter _fileHeaderFooter; private readonly StringBuilder _preHeaderInfo; private readonly string _codeGeneratedAttribute; private readonly FileManagementService _fileManagementService; @@ -4047,77 +4084,48 @@ _fileManagementService = fileManagementService; _fileManagerType = fileManagerType; InitialisationOk = false; - _factory = null; DatabaseReader = null; - FileHeaderFooter = null; + _fileHeaderFooter = null; _preHeaderInfo = new StringBuilder(1024); _codeGeneratedAttribute = string.Format("[GeneratedCode(\"EF.Reverse.POCO.Generator\", \"{0}\")]", EfrpgVersion.Version()); FilterList = new DbContextFilterList(); } - public void Init(string singleDbContextSubNamespace) + public void Init(DatabaseReader databaseReader, string singleDbContextSubNamespace) { - var providerName = "unknown"; - - try - { - var licence = ReadAndValidateLicence(); - if(licence == null) - return; - - providerName = DatabaseProvider.GetProvider(Settings.DatabaseType); - BuildPreHeaderInfo(licence); + var licence = ReadAndValidateLicence(); + if(licence == null) + return; - _factory = DbProviderFactories.GetFactory(providerName); - if (_factory == null) - { - _fileManagementService.Error("Database factory is null, cannot continue"); - return; - } + BuildPreHeaderInfo(licence); - DatabaseReader = DatabaseReaderFactory.Create(_factory); - if (DatabaseReader == null) - { - _fileManagementService.Error("Cannot create a database reader due to unknown database type."); - return; - } - - DatabaseReader.Init(); + DatabaseReader = databaseReader; + if (DatabaseReader == null) + { + _fileManagementService.Error("Cannot create a database reader due to unknown database type."); + return; + } - if (Settings.IncludeConnectionSettingComments) - _preHeaderInfo.Append(DatabaseDetails()); + DatabaseReader.Init(); - if (Settings.UseDataAnnotations) - { - Settings.AdditionalNamespaces.Add("System.ComponentModel.DataAnnotations"); - Settings.AdditionalNamespaces.Add("System.ComponentModel.DataAnnotations.Schema"); - } + if (Settings.IncludeConnectionSettingComments) + _preHeaderInfo.Append(DatabaseDetails()); - HasAcademicLicence = licence.LicenceType == LicenceType.Academic; - HasTrialLicence = licence.LicenceType == LicenceType.Trial; - InitialisationOk = FilterList.ReadDbContextSettings(DatabaseReader, singleDbContextSubNamespace); - _fileManagementService.Init(FilterList.GetFilters(), _fileManagerType); - } - catch (Exception x) + if (Settings.UseDataAnnotations) { - var error = FormatError(x); - Console.WriteLine(error); - - _fileManagementService.Error(_preHeaderInfo.ToString()); - _fileManagementService.Error(string.Empty); - _fileManagementService.Error("// ------------------------------------------------------------------------------------------------"); - _fileManagementService.Error(string.Format("// WARNING: Failed to load provider \"{0}\" - {1}", providerName, error)); - _fileManagementService.Error("// Allowed providers:"); - foreach (DataRow fc in DbProviderFactories.GetFactoryClasses().Rows) - { - var s = string.Format("// \"{0}\"", fc[2]); - _fileManagementService.Error(s); - } - _fileManagementService.Error(string.Empty); - _fileManagementService.Error("/*" + x.StackTrace + "*/"); - _fileManagementService.Error("// ------------------------------------------------------------------------------------------------"); - _fileManagementService.Error(string.Empty); + Settings.AdditionalNamespaces.Add("System.ComponentModel.DataAnnotations"); + Settings.AdditionalNamespaces.Add("System.ComponentModel.DataAnnotations.Schema"); } + + _hasAcademicLicence = licence.LicenceType == LicenceType.Academic; + _hasTrialLicence = licence.LicenceType == LicenceType.Trial; + InitialisationOk = FilterList.ReadDbContextSettings(DatabaseReader, singleDbContextSubNamespace); + _fileManagementService.Init(FilterList.GetFilters(), _fileManagerType); + } + + public string GetPreHeaderInfo() + { + return _preHeaderInfo.ToString(); } private Licence ReadAndValidateLicence() @@ -4160,11 +4168,20 @@ public void LoadEnums() { - if (_factory == null || DatabaseReader == null || !Settings.ElementsToGenerate.HasFlag(Elements.Enum)) + if (DatabaseReader == null || !Settings.ElementsToGenerate.HasFlag(Elements.Enum)) return; try { + foreach (var filterKeyValuePair in FilterList.GetFilters()) + { + var filter = filterKeyValuePair.Value; + foreach (var table in filter.Tables) + { + filter.AddEnum(table); + } + } + if (Settings.GenerateSingleDbContext) { // Single-context @@ -4222,7 +4239,7 @@ public void LoadSequences() { - if (_factory == null || DatabaseReader == null || !Settings.ElementsToGenerate.HasFlag(Elements.Context)) + if (DatabaseReader == null || !Settings.ElementsToGenerate.HasFlag(Elements.Context)) return; try @@ -4258,7 +4275,7 @@ public void LoadTables() { - if (_factory == null || DatabaseReader == null || + if (DatabaseReader == null || !(Settings.ElementsToGenerate.HasFlag(Elements.Poco) || Settings.ElementsToGenerate.HasFlag(Elements.Context) || Settings.ElementsToGenerate.HasFlag(Elements.Interface) || @@ -4667,7 +4684,7 @@ table.SetPrimaryKeys(); } - if (HasTrialLicence) + if (_hasTrialLicence) filter.Tables.TrimForTrialLicence(); } } @@ -4686,7 +4703,7 @@ public void LoadStoredProcs() { - if (_factory == null || DatabaseReader == null || !DatabaseReader.CanReadStoredProcedures()) + if (DatabaseReader == null || !DatabaseReader.CanReadStoredProcedures()) return; try @@ -4802,7 +4819,7 @@ { if (!filter.IsExcluded(sp)) { - if (HasTrialLicence) + if (_hasTrialLicence) { const int n = 1 + 2 + 3 + 4; if (filter.StoredProcs.Count < n) @@ -5288,14 +5305,14 @@ codeOutputList.Add(enumType + enumeration.EnumName, codeGenerator.GenerateEnum(enumeration)); } - FileHeaderFooter = new FileHeaderFooter(filter.SubNamespace); + _fileHeaderFooter = new FileHeaderFooter(filter.SubNamespace); if (!Settings.GenerateSeparateFiles) { var preHeader = _preHeaderInfo.ToString(); if(!string.IsNullOrWhiteSpace(preHeader)) _fileManagementService.WriteLine(preHeader.Trim()); - var header = FileHeaderFooter.Header; + var header = _fileHeaderFooter.Header; if (!string.IsNullOrWhiteSpace(header)) _fileManagementService.WriteLine(header.Trim()); @@ -5306,7 +5323,7 @@ _fileManagementService.WriteLine(usings.Trim()); } - var ns = FileHeaderFooter.Namespace; + var ns = _fileHeaderFooter.Namespace; if (!string.IsNullOrWhiteSpace(ns)) { _fileManagementService.WriteLine(""); @@ -5350,7 +5367,7 @@ .ToList()); if (!Settings.GenerateSeparateFiles) - _fileManagementService.WriteLine(FileHeaderFooter.Footer); + _fileManagementService.WriteLine(_fileHeaderFooter.Footer); } private void WriteCodeOutputForGroup(CodeGenerator codeGenerator, string regionNameForGroup, bool writePreHeaderInfo, List list) @@ -5383,7 +5400,7 @@ _fileManagementService.WriteLine(preHeader.Trim()); } - var header = FileHeaderFooter.Header; + var header = _fileHeaderFooter.Header; if (!string.IsNullOrWhiteSpace(header)) _fileManagementService.WriteLine(header.Trim()); @@ -5394,7 +5411,7 @@ _fileManagementService.WriteLine(usings.Trim()); } - var ns = FileHeaderFooter.Namespace; + var ns = _fileHeaderFooter.Namespace; if (!string.IsNullOrWhiteSpace(ns)) { _fileManagementService.WriteLine(""); @@ -5405,7 +5422,7 @@ WriteLines(IndentCode(code, regionNameForGroup, firstInGroup, lastInGroup)); if (Settings.GenerateSeparateFiles) - _fileManagementService.WriteLine(FileHeaderFooter.Footer); + _fileManagementService.WriteLine(_fileHeaderFooter.Footer); _fileManagementService.ForceWriteToOuter = false; } @@ -5433,7 +5450,7 @@ } } - if (firstInGroup && (HasAcademicLicence || HasTrialLicence)) + if (firstInGroup && (_hasAcademicLicence || _hasTrialLicence)) { lines.Add(IndentedStringBuilder(indentNum, "// ****************************************************************************************************")); lines.Add(IndentedStringBuilder(indentNum, "// This is not a commercial licence, therefore only a few tables/views/stored procedures are generated.")); @@ -5487,11 +5504,6 @@ _fileManagementService.WriteLine(line); } - private static string FormatError(Exception ex) - { - return ex.Message.Replace("\r\n", "\n").Replace("\n", " "); - } - private void BuildPreHeaderInfo(Licence licence) { if (Settings.ShowLicenseInfo) @@ -5539,6 +5551,11 @@ var rx = new Regex("password=[^\";]*", RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.IgnoreCase); return rx.Replace(conn, "password=**zapped**;"); } + + private string FormatError(Exception ex) + { + return ex.Message.Replace("\r\n", "\n").Replace("\n", " "); + } } // Fill in the following functions with your own code if not using EF6 / EfCore @@ -6197,6 +6214,7 @@ } } + public static class GeneratorFactory { public static Generator Create(FileManagementService fileManagementService, Type fileManagerType, string singleDbContextSubNamespace = null) @@ -6221,8 +6239,37 @@ throw new ArgumentOutOfRangeException(); } - generator.Init(singleDbContextSubNamespace); - return generator; + var providerName = "unknown"; + try + { + providerName = DatabaseProvider.GetProvider(Settings.DatabaseType); + var factory = DbProviderFactories.GetFactory(providerName); + var databaseReader = DatabaseReaderFactory.Create(factory); + generator.Init(databaseReader, singleDbContextSubNamespace); + return generator; + } + catch (Exception x) + { + var error = x.Message.Replace("\r\n", "\n").Replace("\n", " "); + Console.WriteLine(error); + + fileManagementService.Error(generator.GetPreHeaderInfo()); + fileManagementService.Error(string.Empty); + fileManagementService.Error("// ------------------------------------------------------------------------------------------------"); + fileManagementService.Error(string.Format("// WARNING: Failed to load provider \"{0}\" - {1}", providerName, error)); + fileManagementService.Error("// Allowed providers:"); + foreach (DataRow fc in DbProviderFactories.GetFactoryClasses().Rows) + { + var s = string.Format("// \"{0}\"", fc[2]); + fileManagementService.Error(s); + } + fileManagementService.Error(string.Empty); + fileManagementService.Error("/*" + x.StackTrace + "*/"); + fileManagementService.Error("// ------------------------------------------------------------------------------------------------"); + fileManagementService.Error(string.Empty); + } + + return null; } } @@ -12993,7 +13040,7 @@ and limitations under the License. col.IsIdentity = true; } - if (!col.IsPrimaryKey && filter.IsExcluded(col)) + if (!col.IsPrimaryKey && filter.IsExcluded(col)) col.Hidden = true; col.IsFixedLength = (rt.TypeName == "char" || rt.TypeName == "nchar"); diff --git a/Generator/Filtering/DbContextFilter.cs b/Generator/Filtering/DbContextFilter.cs index 73a0ace8..76b8f526 100644 --- a/Generator/Filtering/DbContextFilter.cs +++ b/Generator/Filtering/DbContextFilter.cs @@ -30,6 +30,7 @@ protected DbContextFilter() public abstract string MappingTableRename(string mappingTable, string tableName, string entityName); public abstract void UpdateTable(Table table); public abstract void UpdateColumn(Column column, Table table); + public abstract void AddEnum(Table table); public abstract void UpdateEnum(Enumeration enumeration); public abstract void UpdateEnumMember(EnumerationMember enumerationMember); public abstract void ViewProcessing(Table view); diff --git a/Generator/Filtering/IDbContextFilter.cs b/Generator/Filtering/IDbContextFilter.cs index d4f3c879..150de30b 100644 --- a/Generator/Filtering/IDbContextFilter.cs +++ b/Generator/Filtering/IDbContextFilter.cs @@ -21,6 +21,7 @@ public interface IDbContextFilter string MappingTableRename(string mappingTable, string tableName, string entityName); void UpdateTable(Table table); void UpdateColumn(Column column, Table table); + void AddEnum(Table table); void UpdateEnum(Enumeration enumeration); void UpdateEnumMember(EnumerationMember enumerationMember); void ViewProcessing(Table view); diff --git a/Generator/Filtering/MultiContextFilter.cs b/Generator/Filtering/MultiContextFilter.cs index 1492fd1a..5ca4ff7a 100644 --- a/Generator/Filtering/MultiContextFilter.cs +++ b/Generator/Filtering/MultiContextFilter.cs @@ -346,6 +346,11 @@ public override void UpdateColumn(Column column, Table table) Settings.UpdateColumn?.Invoke(column, table, null); } + public override void AddEnum(Table table) + { + + } + public override void UpdateEnum(Enumeration enumeration) { diff --git a/Generator/Filtering/SingleContextFilter.cs b/Generator/Filtering/SingleContextFilter.cs index 3e934ffe..3f3df69f 100644 --- a/Generator/Filtering/SingleContextFilter.cs +++ b/Generator/Filtering/SingleContextFilter.cs @@ -94,6 +94,11 @@ public override void UpdateColumn(Column column, Table table) Settings.UpdateColumn?.Invoke(column, table, EnumDefinitions); } + public override void AddEnum(Table table) + { + Settings.AddEnum?.Invoke(table); + } + public override void UpdateEnum(Enumeration enumeration) { Settings.UpdateEnum?.Invoke(enumeration); diff --git a/Generator/Generator.csproj b/Generator/Generator.csproj index 5a8841e3..c77519f9 100644 --- a/Generator/Generator.csproj +++ b/Generator/Generator.csproj @@ -86,7 +86,6 @@ - diff --git a/Generator/Generators/Generator.cs b/Generator/Generators/Generator.cs index fa31e8b9..04cac232 100644 --- a/Generator/Generators/Generator.cs +++ b/Generator/Generators/Generator.cs @@ -138,6 +138,15 @@ public void LoadEnums() try { + foreach (var filterKeyValuePair in FilterList.GetFilters()) + { + var filter = filterKeyValuePair.Value; + foreach (var table in filter.Tables) + { + filter.AddEnum(table); + } + } + if (Settings.GenerateSingleDbContext) { // Single-context diff --git a/Generator/Settings.cs b/Generator/Settings.cs index a8aeca79..e0a08a3c 100644 --- a/Generator/Settings.cs +++ b/Generator/Settings.cs @@ -92,6 +92,7 @@ public static class Settings // Create enumerations from database tables // List the enumeration tables you want read and generated for + // Also look at the AddEnum callback to add your own public static List Enumerations = new List { // Example @@ -326,6 +327,32 @@ c.NameHumanCase .Equals("SomeColumn", StringComparison.InvariantCultu return ".UseIdentityColumn()"; }; + public static Action
AddEnum = delegate (Table table) + { + /*if (table.HasPrimaryKey && table.PrimaryKeys.Count() == 1 && table.Columns.Any(x => x.PropertyType == "string")) + { + // Example IF to only choose tables with a certain naming conventions for enums + if (table.NameHumanCase.StartsWith("REF_", StringComparison.InvariantCultureIgnoreCase) || + table.NameHumanCase.EndsWith("_LUT", StringComparison.InvariantCultureIgnoreCase)) + { + try + { + Enumerations.Add(new EnumerationSettings + { + Name = table.NameHumanCase + "Enum", + Table = table.Schema.DbName + "." + table.DbName, + NameField = table.Columns.First(x => x.PropertyType == "string").DbName, // Or specify your own + ValueField = table.PrimaryKeys.Single().DbName // Or specify your own + }); + } + catch + { + // Swallow exception + } + } + }*/ + }; + // Use the following function if you need to apply additional modifications to a enum // Called just before UpdateEnumMember public static Action UpdateEnum = delegate (Enumeration enumeration) diff --git a/Tester.Integration.EFCore6/EnumOnly.tt b/Tester.Integration.EFCore6/EnumOnly.tt index 2b06841e..c57affce 100644 --- a/Tester.Integration.EFCore6/EnumOnly.tt +++ b/Tester.Integration.EFCore6/EnumOnly.tt @@ -29,7 +29,7 @@ // Elements to generate *************************************************************************************************************** // Add the elements that should be generated when the template is executed. // Multiple projects can be used that separate the different concerns. - Settings.ElementsToGenerate = Elements.Enum; + //Settings.ElementsToGenerate = Elements.Enum; // Filtering ************************************************************************************************************************** // Filtering can now be done via one or more Regex's and one or more functions. @@ -121,13 +121,6 @@ Table = "EnumTest.DaysOfWeek", // Database table containing enum values. e.g. "DaysOfWeek" NameField = "TypeName", // Column containing the name for the enum. e.g. "TypeName" ValueField = "TypeId" // Column containing the values for the enum. e.g. "TypeId" - }, - new EnumerationSettings - { - Name = "Colour", - Table = "Colour", - NameField = "Name", - ValueField = "Id" } // Code will be generated as: // public enum Name @@ -201,6 +194,33 @@ // If TableSuffix is "Entity" then Order will be OrderEntity Settings.TableSuffix = null; + Settings.AddEnum = delegate (Table table) + { + if (table.HasPrimaryKey && table.PrimaryKeys.Count() == 1 && table.Columns.Any(x => x.PropertyType == "string")) + { + // Example IF to only choose tables with a certain naming conventions for enums + if (table.NameHumanCase.StartsWith("Car", StringComparison.InvariantCultureIgnoreCase) || + table.NameHumanCase.StartsWith("Colour", StringComparison.InvariantCultureIgnoreCase)) + { + try + { + Settings.Enumerations.Add(new EnumerationSettings + { + Name = table.NameHumanCase + "Enum", + Table = table.Schema.DbName + "." + table.DbName, + NameField = table.Columns.First(x => x.PropertyType == "string").DbName, // Or specify your own + ValueField = table.PrimaryKeys.Single().DbName // Or specify your own + }); + } + catch + { + // Swallow exception + } + } + } + }; + + // Call-backs ************************************************************************************************************************* // AddRelationship is a helper function that creates ForeignKey objects and adds them to the foreignKeys list