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

Add Settings.EnumNamespace #740

Open matt-neubert opened 2 years ago

matt-neubert commented 2 years ago

Blazor webassembly projects creates a Client, Server and Shared projects. Typically you wouldn't share the EF poco objects, but you may want to reuse the enums. Therefore, you may want to place them in the Shared project. It would be good give specific namespace for the enums when NOT generating them in the server t4 transform. While you can add the the project namespace in Settings.AdditionalNamespaces, it seems to be more logical to place them with the other namespace configuration sections. This then would be added to the appropriate files when there is an enum type replacement.

Settings.PocoNamespace              = ""; // "YourProject.Entities";
Settings.ContextNamespace           = ""; // "YourProject.Context";
Settings.InterfaceNamespace         = ""; // "YourProject.Interfaces";
Settings.PocoConfigurationNamespace = ""; // "YourProject.Configuration";
Settings.EnumNamespace = ""; // "YourProject.Configuration";
sjh37 commented 2 years ago

Hi @matt-neubert

To do this, simply add the generator again to the Shared project, and copy your <database>.tt settings from your current file.

Shared project

In the shared project edit the following line to just the elements you want

Settings.ElementsToGenerate = Elements.Enum;

// You have to now reference the main project, so you need to add in its namespace.
Settings.PocoNamespace              = "YourProject.Main"; // "YourProject.Entities";
Settings.ContextNamespace           = "YourProject.Main"; // "YourProject.Context";
Settings.InterfaceNamespace         = "YourProject.Main"; // "YourProject.Interfaces";
Settings.PocoConfigurationNamespace = "YourProject.Main"; // "YourProject.Configuration";

Main project

And in your main project, remove enum element as this is generated in another project:

Settings.ElementsToGenerate = Elements.Poco | Elements.Context | Elements.Interface | Elements.PocoConfiguration;

// You have to now reference the other enum project, so you need to add in the namespace where it is used
Settings.EnumNamespace = "YourProject.Shared"; // The namespace is of your shared project where the Enums reside.

I show this in a video over at PluralSight if you have a subscription: https://www.pluralsight.com/courses/code-first-entity-framework-legacy-databases. The module is this one https://app.pluralsight.com/course-player?clipId=7ca1ff82-fbb5-40f3-8c26-cc4e0752751f