Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum property type replacement enhancements #739

Closed
matt-neubert opened this issue Jan 20, 2022 · 4 comments
Closed

Enum property type replacement enhancements #739

matt-neubert opened this issue Jan 20, 2022 · 4 comments
Assignees

Comments

@matt-neubert
Copy link

I'm not sure if this is a feature request or a general how to question. I like to identify enum tables by naming conventions for example it may start with REF_ or it may end in _LUT. What I would like to do is create this list dynamically. The PK of this table would be enum value and the table always has Name column which is the enum name. (Maybe even any other columns in the table would become attributes on the enum ... but that would be bonus). This is not only for creating the enum but also for the enum property type replacement. In the UpdateColumn method I would like to have a way to check to see if the column is an FK column and if it is to be able to find the parent column's table name and also check if it conformed to the naming convention. If I could get to that info I would be able to add it to enumDefinitions then the type could be replaced by the enum type.

These were just my thoughts but would be open to other ways to automate this. When we were working back on version 2.8 at a former employer, we created a property on the Column class named ParentColumn & TableName and placed the pkCol on the fkCol, so we could do interrogate c.ParentColumn.TableName. I see ParentTable is now on the Column class so that wouldn't be needed. I think the FK is more complex now in that it handles multi-column FKs, so not sure where to go.

@sjh37 sjh37 self-assigned this Jan 20, 2022
@sjh37
Copy link
Owner

sjh37 commented Jan 20, 2022

I tried but couldn't get it to work out of the box, so I am modifying the generator to allow you to do this. I'll make sure to tag the changeset with this case number so you can see the code change I have made.

@sjh37
Copy link
Owner

sjh37 commented Jan 20, 2022

Here is the changeset: c10aad9

Add this to your .tt file:

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
                }
            }
        }
    };

change NameField = "Name",

You can update your own .ttinclude file, or wait for the next release. Any problems add a commen.

@sjh37
Copy link
Owner

sjh37 commented Jan 24, 2022

public static Action<Table> AddEnum = delegate (Table table)
{
    if (table.HasPrimaryKey && table.PrimaryKeys.Count() == 1 && table.Columns.Any(x => x.PropertyType == "string"))
    {
        if (table.NameHumanCase.StartsWith("REF_", StringComparison.InvariantCultureIgnoreCase) ||
            table.NameHumanCase.EndsWith  ("_LUT", StringComparison.InvariantCultureIgnoreCase))
        {
            try
            {
                Enumerations.Add(new EnumerationSettings
                {
                    Name       = table.NameHumanCase.Replace("REF_","").Replace("_LUT","") + "Enum",
                    Table      = table.Schema.DbName + "." + table.DbName,
                    NameField  = table.Columns.First(x => x.PropertyType == "string").DbName,
                    ValueField = "Name"
                });

                // This will cause this table to not be reverse engineered.
                // This means it was only required to generate an enum and can now be removed.
                table.RemoveTable = true; // Remove this line if you want to keep it in your dbContext.
            }
            catch
            {
                // Swallow exception
            }
        }
    }
};

sjh37 added a commit that referenced this issue Jan 24, 2022
@sjh37
Copy link
Owner

sjh37 commented Feb 13, 2022

Now released in v3.6.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants