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

Document how to share T4 templates across projects #4292

Open
gao-artur opened this issue Mar 9, 2023 · 5 comments
Open

Document how to share T4 templates across projects #4292

gao-artur opened this issue Mar 9, 2023 · 5 comments

Comments

@gao-artur
Copy link

Before EF Core 7, we used to override the ICSharpDbContextGenerator implementation in the IDesignTimeServices to customize the DbContext generation during the scaffold. Starting EF Core 7, this approach no longer works, and we are now exploring the t4 templates approach.

We have many (~15) DB to scaffold. Their models will be located in different folders, but they all need the same template. We have a single Scaffold.Console app, where we implemented our customizations, and it uses as a startup project during the scaffold. Unfortunately, putting the templates in the startup project folder doesn't work. It works only when templates are located in the destination folder (the folder where we write the generated files).

I have seen this issue, which was closed as Won't do. Any suggestion on how to reuse the same template without copying it 15 times and synchronizing the changes on every update?

@ErikEJ
Copy link
Contributor

ErikEJ commented Mar 9, 2023

Linked files?

@gao-artur
Copy link
Author

Tried it. But template files should be physically located in the destination folder.

@ajcvickers
Copy link
Member

@gao-artur You can reference a shared copy of the template file from within the template file of each project. For example, the contents of "MyProject\CodeTemplates\EFCore\DbContext.t4" would be

<#@ include file="C:\local\CodeTemplates\EFCore\DbContext.t4" #>

Or, with relative paths:

<#@ include file="..\..\..\..\..\CodeTemplates\EFCore\DbContext.t4" #>

And then "C:\local\CodeTemplates\EFCore\DbContext.t4" is the actual T4 template that you want to use in multiple places. Likewise for "EntityType.t4".

@gao-artur
Copy link
Author

@ajcvickers, thank you! It worked!

@ajcvickers ajcvickers changed the title t4 templates reuse in different projects Document how to share T4 templates across projects Mar 19, 2023
@ajcvickers ajcvickers transferred this issue from dotnet/efcore Mar 19, 2023
@ajcvickers ajcvickers self-assigned this Mar 22, 2023
@ajcvickers ajcvickers added this to the Backlog milestone Mar 22, 2023
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
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

3 participants