C# 修改t4模板以在两个文件中生成写入文本
我的实体模型(MyModel)有MyModel.tt。我需要修改MyModel.tt文件。我希望simpleProperties在一个类中编写,而navigationProperties和ComplexProperty在另一个类中编写。我已经找到了这些行,但现在完全不知道该怎么办 这是(我认为)我必须编写的代码,它将在不同的类中编写属性C# 修改t4模板以在两个文件中生成写入文本,c#,entity-framework-5,t4,C#,Entity Framework 5,T4,我的实体模型(MyModel)有MyModel.tt。我需要修改MyModel.tt文件。我希望simpleProperties在一个类中编写,而navigationProperties和ComplexProperty在另一个类中编写。我已经找到了这些行,但现在完全不知道该怎么办 这是(我认为)我必须编写的代码,它将在不同的类中编写属性 <# } var simpleProperties = typeMapper.GetSimpleProperties(entity);
<#
}
var simpleProperties = typeMapper.GetSimpleProperties(entity);
if (simpleProperties.Any())
{
foreach (var edmProperty in simpleProperties)
{
#>
// TODO: Write this in entityName.cs
<#=codeStringGenerator.Property(edmProperty)#>
<#
}
}
if (complexProperties.Any())
{
#>
<#
foreach(var complexProperty in complexProperties)
{
#>
// TODO: Write this in entityNameComplex.cs
<#=codeStringGenerator.Property(complexProperty)#>
<#
}
}
//TODO:在entityName.cs中写入此项
//TODO:在entityNameComplex.cs中编写
我不确定您是在问如何编写“codeStringGenerator”存根,还是仅仅将输出分成两个文件。如果您只是想将输出分成两个文件,那么下面的小片段应该可以工作
<#
relativeOutputFilePath = @"\Output\" + oneTable.Name + "_List.aspx";
TemplateHelper.WriteTemplateOutputToFile(relativeOutputFilePath, Host, GenerationEnvironment);
GenerationEnvironment = new System.Text.StringBuilder();
#>
基本上,所有这些操作都是获取到目前为止由模板生成的字符串,将其写入到您选择的文件中,然后为下一个模板重置该字符串
这是从StackOverflow中提取的,它可能提供更多信息