Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent T4 template from deleting existing files

I want to create for each entity in my edmx-model a separate class file called {0}Validator.cs (do not care about its content by now).

This seems to work, but I can't work it out to prevent my T4 template from deleting all my files first. How can I get rid of this behavior?

What I found out is that if I call fileManager.Process(true), all the files under my validator.tt file will be recreated (and I don't want this).

Any ideas please? Thanks!

<#@ template language="C#" debug="false" hostspecific="true"#>
//<#@ include file="EF.Utility.CS.ttinclude"#>
<#@output extension=".cs"#>

<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);

string inputFile =@"ServicesEntities.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);

// for test purposes only...
fileManager.Process(true);

// for each entity, create a xxxValidator.cs file

foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
     string fileName = entity.Name + "Validator.cs";
     string filePath = this.Host.TemplateFile.Substring(0,this.Host.TemplateFile.LastIndexOf(@"\")); 
     filePath = filePath + @"\" + fileName;

     if(!File.Exists(filePath)) 
     {
          fileManager.StartNewFile(filePath);

#>
// the content of the validator class
public partial class <#=code.Escape(entity)#>
{
    public bool ValidateModel()
    {
    // enter checkmethods here!!! again
    return true;
    }
}
<#          
    }           
}

fileManager.Process(true);

#>
like image 430
HarryK Avatar asked May 09 '11 17:05

HarryK


People also ask

How to debug T4 template?

To debug a design-time text template, save the text template file, and then choose Debug T4 Template on the shortcut menu of the file in Solution Explorer. To debug a run-time text template, simply debug the application to which it belongs.

What is true about T4 templates in Entity Framework?

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.

What are. Tt files?

TT stands for - Visual Studio Text Template is a software development tool created by the Microsoft. Further explanation - TT file contains text block and control logic used for generating new files. To write the Text Template file we can use either - Visual C# or Visual Basic Code.


1 Answers

I am in the exact same situation. I want to generate buddy classes for data annotation. I don't want to put data into the edmx file and alter my templates to add the correct annotations based on the edmx data. That would take too long. The easiest solution is to generate a buddy class and set it so it doesn't regenerate every time. In this case efficiency is more important than the convention that T4 classes should always be regenerated.

Dane Morgridge figured out a clever way to do this. He checks to see if the file already exists. If it does he reads it in and writes it back out the way it was. If it doesn't he renders his template. Check out DbContext Templates\IRepository.tt. https://github.com/danemorgridge/efrepo

Here are the relevant sections.

string OutputFile(string filename)
{
    using(StreamReader sr = new StreamReader(Path.Combine(GetCurrentDirectory(),filename)))
    {
        string contents = sr.ReadToEnd();
        return contents;
    }
}

if(!DoesFileExist(entity.Name + "Repository.cs"))
{
    fileManager.StartNewFile(entity.Name + "Repository.cs");
}
else
{
    fileManager.StartNewFile(entity.Name + "Repository.cs");
    this.Write(OutputFile(entity.Name + "Repository.cs"));
}
like image 146
Derek Powles Avatar answered Oct 31 '22 01:10

Derek Powles