【发布时间】:2011-05-09 17:29:31
【问题描述】:
我想为我的 edmx-model 中的每个实体创建一个名为 {0}Validator.cs 的单独类文件(现在不关心它的内容)。
这似乎可行,但我无法阻止我的 T4 模板首先删除我的所有文件。我怎样才能摆脱这种行为?
我发现如果我调用 fileManager.Process(true),我的 validator.tt 文件下的所有文件都将被重新创建(我不希望这样)。
有什么想法吗? 谢谢!
<#@ 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);
#>
【问题讨论】:
标签: entity-framework t4