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

How T4 generates multiple files #1825

Closed
Seanxwy opened this issue Jul 24, 2019 · 8 comments · Fixed by #3098
Closed

How T4 generates multiple files #1825

Seanxwy opened this issue Jul 24, 2019 · 8 comments · Fixed by #3098
Milestone

Comments

@Seanxwy
Copy link

Seanxwy commented Jul 24, 2019

No description provided.

@igor-tkachev
Copy link
Member

@Seanxwy
Copy link
Author

Seanxwy commented Jul 24, 2019

This example is not a data-independent model file directly from the database

@igor-tkachev
Copy link
Member

So, you need to generate a data model from database but every table should be in a separate file. Right?

@Seanxwy
Copy link
Author

Seanxwy commented Jul 25, 2019

@igor-tkachev Yeah, that's it

@Drapondur
Copy link

Drapondur commented Feb 6, 2020

So, you need to generate a data model from database but every table should be in a separate file. Right?

This is, although not too tough, not an entirely simple task, at least as far as I can see.
I came up with the following solution provided in the attached file MultiFileSupport.zip.
I made this for linq2db 2.9.5.

Put the file in your project directory (not the LinqToDB.Templates directory).

Then in your .tt-file:

<#@ template language="C#" debug="True" hostSpecific="True"                           #>
<#@ output extension=".generated.cs"                                                  #>

<#@ include file="$(LinqToDBT4SqlServerTemplatesDirectory)MultipleFiles.ttinclude" #>
<#@ include file="$(LinqToDBT4SqlServerTemplatesDirectory)LinqToDB.SqlServer.Tools.ttinclude" #>
<#@ include file="$(LinqToDBT4SqlServerTemplatesDirectory)PluralizationService.ttinclude"     #>

<#//@ include file="$(ProjectDir)LinqToDB.Templates\MultipleFiles.ttinclude" #>
<#//@ include file="$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.Tools.ttinclude" #>
<#//@ include file="$(ProjectDir)LinqToDB.Templates\PluralizationService.ttinclude"     #>

<#@ include file="$(ProjectDir)MultiFileSupport.ttinclude"     #>

//The usual stuff here like
//	LoadSqlServerMetadata("MyServer", "MyDatabase", "User", "Password");
//etc. up to

GenerateModel();

//and now
SyncProject();

There is room for improvement, but you get the gist.

@ademchenko
Copy link

@Drapondur , works awesome. Thanks a lot!

@Drapondur
Copy link

Drapondur commented Mar 3, 2020

Thanks ademchenko. Yes it seems to work except for the case that files do not get deleted properly.
After having dropped a table the generated file for the table was still there.
I believe that is the case for SDK style projects only. I haven't used it for the old project style yet.
However, I added a workaround I uploaded to here: MultiFileSupport.zip
A new variable named ClearMultiFileDir (bool) is introduced which is false per default.
If set to true all files with the extension defined in MultiFileExtension will be deleted from the target directory before new files are generated.
Important: If you use the file I uploaded to this post you must replace SyncProject() at the end of your tt-file with FinalizeMultiFiles()

<#@ template language="C#" debug="True" hostSpecific="True"                           #>
<#@ output extension=".generated.cs"                                                  #>

<#@ include file="$(LinqToDBT4SqlServerTemplatesDirectory)MultipleFiles.ttinclude" #>
<#@ include file="$(LinqToDBT4SqlServerTemplatesDirectory)LinqToDB.SqlServer.Tools.ttinclude" #>
<#@ include file="$(LinqToDBT4SqlServerTemplatesDirectory)PluralizationService.ttinclude"     #>

<#//@ include file="$(ProjectDir)LinqToDB.Templates\MultipleFiles.ttinclude" #>
<#//@ include file="$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.Tools.ttinclude" #>
<#//@ include file="$(ProjectDir)LinqToDB.Templates\PluralizationService.ttinclude"     #>

<#@ include file="$(ProjectDir)MultiFileSupport.ttinclude"     #>

//The usual stuff here like
//	LoadSqlServerMetadata("MyServer", "MyDatabase", "User", "Password");
//etc. up to

GenerateModel();

//and now
FinalizeMultiFiles(); 

Have fun!

@emiletu
Copy link

emiletu commented Dec 28, 2020

FinalizeMultiFiles

I can't run with the latest one. Can you help me?

@MaceWindu MaceWindu mentioned this issue Jul 5, 2021
34 tasks
@MaceWindu MaceWindu added this to the 4.0.0 milestone Feb 13, 2022
@MaceWindu MaceWindu modified the milestones: 4.1.0, 4.0.0 Mar 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

6 participants