-
-
Notifications
You must be signed in to change notification settings - Fork 304
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
T4 customizations and tips #1499
Comments
I love the new T4 templates. I have used it to create C# 11 required properties. This is my line 104 of the public <#= needsInitializer ? "required " : "" #><#= code.Reference(property.ClrType) #><#= needsNullable ? "?" : "" #> <#= property.Name #> { get; set; } This generates this code if you use the Thanks for your great work! Fons |
@sonnemaf Great suggestion! |
Would there be a way to support any T4 file name that is dropped into the templates folder rather than just the specific named three that are used for scaffolding out of the box. For example I could make a files called Services.T4, or Controllers.T4, Views.T4 etc. My work around now is to create that code then rename them upon running the PowerTools. |
@mrunks not possible. But a setting to specify the location of the folder structure is very possible. Maybe create a separate issue and I will investigate. |
I am not clear how a different folder structure would help. I just want to be able to read additional files. Or are you suggesting having multiple folders all with the same file names and then it would iterate through each of the folders ? |
@mrunks I suggest you create a new issue and explain your exact requirements |
Is it possible to add the DisplayName Attribute in the T4 template? I've did a search but I've not see where anyone asked this or seen it implemented. [Display(Name= "Price")] |
@KayakFisher205 Where would the value for Name in Display come from? |
It could be the comment from the model, yes. I will provide an example later. |
@KayakFisher205 Just have a look at how "comments" are currently applied in the templates. |
When there is an error in the T4, the reverse engineering produces a general error message in the output window which makes it hard to find the root cause. Would it be possible to produce clear error messages like the output of a tt file > Run custom tool? E.g. output reverse engineer: at ReverseEngineerResult EFCorePowerTools.Handlers.ReverseEngineer.ResultDeserializer.BuildResult(string output) |
@AchrWasUnavailable Please ask in the EF Core repo, it needs to be fixed there (or maybe even in VS) |
I have two customizations in my own code. First one is But I also have the code to avoid empty list allocations for collection navigations. So that we can go from this:
…to this:
This is done with this quick hack (obviously copy-pasting 3 times the field name generation logic is a no-no)
… and inside properties loop:
|
@piskov Thanks! Is this not "Custom Collection Initializers" from the list above? |
@ErikEJ yeah, you’re absolutely right: to my shame, I’ve dismissed it due to “hashset” and “constructor” in the name and reinvented the wheel :-) |
@piskov no worries, your sample code is very valuable! I am comsidering at one point to create a "super" template where you can toggle these advanced options on. |
Hi Erik, I see "INotifyPropertyChanged properties" in the checklist above. I am guessing this means the tool will be able to create model properties with property change notification. Any idea when this will be released? Thanks for all your hard work on this tool. |
@rajibsm it’s really easy doing it yourself by customizing EntityType.t4 template. For example to use ObservableCollection instead of list replace
… with this
Same goes for properties: you need to split them into fields + property declarations which is really easy if you use my code as a sample. Both things should not take more than 30 minutes :-) It will probably not cover most of the EF cases but will do for your code (i. e. work correctly 95% of the time with some manual adjustments required later). |
@piskov Thank you for pointing me in the right direction. This was my first time editing T4 templates, but I was able to do it. Installing a T4 formatter extension for Visual Studio really helped to navigate the code template. @ErikEJ also has this helpful youtube video that will help with editing T4 templates. |
I have uploaded the start of a "Power" template, with the following features:
https://github.com/ErikEJ/EFCorePowerTools/tree/master/samples/CodeTemplates/EFCore |
I downloaded the templates mentioned above, but wasnt really sure what I needed to change to get the enum mapping support. Neverless, Thank you |
Is it possible to have some (or maybe all) generated classes derive from a base entity? i.e. add |
@rick1c Yes, but it would go against Database First reverse engineering |
My dotnetConf video
The text was updated successfully, but these errors were encountered: