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
710 stars 227 forks source link

Editing template to include double-quotes around fields. #741

Closed MarkLFT closed 1 year ago

MarkLFT commented 2 years ago

I am trying to add a const string with the field name of each field to allow the use of strongly typed field names instead of magic strings. Our previous ORM created these automatically and they are very useful. For example:

public bool IsAllowed {get; set; }
public const string IsAllowedField = "IsAllowed";

To achieve this here I tried adding the following to the ttinclude file

public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
public const string {{NameHumanCase}}Field = "{{NameHumanCase}}"; {{#newline}}

The problem I have is the double quotes around the fieldname in the second line. (The first line is shown to show location only) I have tried """, I have tried /", I have tried '/"', and after dozens of hours of searching, and trial and error, I am unable to find the correct way to do this. Can you please help.

The addition of this line might also be helpful for others to avoid the use of magic strings.

Thanks for any advise you can give,

MarkLFT commented 2 years ago

After I stopped looking for the solution to this, I found the answer. A simple ""{{NameHumanCase}}"" was the answer. And it works great.

I haven't closed this, as I am hoping you will consider adding this to the base templates so it is available as standard. ;-)

Again many thanks for a great product, and sorry for posting so many questions.

sjh37 commented 2 years ago

The Field property, is that how its named in the C# class, or how it is named in the database? i.e. it could be named is_allowed in the database, and IsAllowed in the C# class.

Could you give me an example of where you would find this useful?

MarkLFT commented 2 years ago

What I have done is in the ttinclude added the following line:

public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}""; {{#newline}}

This is inserted directly below the row that defines th Poco class properties. The result is I get the extra lines in the PoCO class as follows

public string Code { get; set; }
public const string CodeField = "Code"; 
public string IsoCode { get; set; }
public const string IsoCodeField = "IsoCode"; 

The purpose behind this is, in my code, it is often necessary for me to define the column I am referring to. Without these const strings I would be forced to use magic strings i.e. column["Code"] instead of column[TableName.CodeField].

These types of references are used a lot in Infragistics and Syncfusion, plus others, controls to define from the datasource which columns to bind for the display/value properties. In our Cashier program, we have over 300 such binding references. I would hate to think how many there will be in our main hotel system application, certainly well into the thousands.

I am sure there will be other ways this can be achieved, but in my experience, this is by far the most performant.

sjh37 commented 2 years ago

Thanks Mark. I'll add it as a boolean option in the .tt file to include them or not :-)

MarkLFT commented 2 years ago

Many thanks, greatly appreciated.

MarkLFT commented 2 years ago

Did you get a chance to add this boolean option in version 3.6? I have taken a look but cannot seem to find it.

MarkLFT commented 2 years ago

Great thanks

MarkLFT commented 1 year ago

Hi Simon,

I was wondering if you ever got around to this? I am currently updating our data classes to .Net 7 EF Core 7, and using the new 3.8 templates, but it is a bit of a pain as I must remember to re-add my workarounds.

sjh37 commented 1 year ago

This has now been added, and will be in the next release. The new settings flag is called Settings.IncludeFieldNameConstants

Settings this to true will return a class with properties like:

public int Id { get; set; } // ID (Primary key)
public const string IdField = "Id";
public int OrderId { get; set; } // OrderID
public const string OrderIdField = "OrderId";
public string Sku { get; set; } // sku (length: 15)
public const string SkuField = "Sku";
sjh37 commented 1 year ago

This has been released in v3.8.2. Any problems, please let me know.