sjh37 / EntityFramework-Reverse-POCO-Code-First-Generator

EntityFramework Reverse POCO Code First Generator - Beautifully generated code that is fully customisable. This generator creates code as if you reverse engineered a database and lovingly created the code by hand. It is free to academics (you need a .edu or a .ac email address), not free for commercial use. Obtain your licence from
https://www.reversepoco.co.uk/
Other
706 stars 230 forks source link

EnumerationSettings - filter items/rows #747

Closed R-Norman closed 2 years ago

R-Norman commented 2 years ago

Is there a way to filter the data used for an enum (not all rows)? If not, please add the ability to filter which rows are used for enum generation. In my use case, the filter needs to use a column not included in the enum itself. This could be a DB where or maybe a callback that can include additional rows to evaluate.

Basically, I have a large table that has data for multiple applications and really only want data for one at a time as enums.

For now, I will continue to code this specific enum manually (or maybe create app specific views just for this generation in the DB).

Thanks

sjh37 commented 2 years ago

Interesting... I'll give it some thought :-)

sjh37 commented 2 years ago

Change the following function so it returns the right SQL query you need for your tables:

protected override string EnumSQL(string table, string nameField, string valueField)
{
    return string.Format("SELECT {0} as NameField, {1} as ValueField, * FROM {2};", nameField, valueField, table);
}

To something like this:

protected override string EnumSQL(string table, string nameField, string valueField)
{
    if(table.Equals("dbo.SomeTable", StringComparison.InvariantCultureIgnoreCase))
        return string.Format("SELECT todo;", nameField, valueField, table);

    if(table.Equals("dbo.AnotherTable", StringComparison.InvariantCultureIgnoreCase))
        return string.Format("SELECT todo;", nameField, valueField, table);

    return string.Format("SELECT {0} as NameField, {1} as ValueField, * FROM {2};", nameField, valueField, table);
}
sjh37 commented 2 years ago

Closing this issue. If the above does not work, please add a comment and I will re-open.

R-Norman commented 2 years ago

I am assuming the code you suggested goes into ttinclude file? I could not get that to work. There are two methods that have code in them and I even tried to break them by messing up the SQL but the existing enums were still generated normally.

However, I was able to get this working with a hack using simple SQL injection :) Table = "dbo.TableForEnum WHERE TypeId=1",

Please do not "fix" this until another way is confirmed or added :) Ideally, injection should not be allowed anywhere. The addition of a "WHERE" clause parameter might be the simplest way to support the same functionality without injection.

sjh37 commented 2 years ago

Yes, the above was for a change to the .ttinclude file. I won't be adding this as permanent feature unless other people also want this, and I can get it to work in a simple common sense way for everyone. I'm glad you got something working.