从ADO.NET实体数据模型(实体框架)修改T4模板

时间:2013-10-10 12:12:42

标签: c# entity-framework-5 t4

实际上,我需要的应该是非常简单的。

我想对所有生成的类及其各自的类名进行XML注释。
目前,生成的类看起来像这样:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MyProject.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Foo {
    ...
    }
}

我们的T4模板名为WebEntities.tt,包含在名为WebEntities.edmx的实体数据模型中。
修改WebEntities.tt并执行“运行自定义工具”后,我希望得到以下结果:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MyProject.Models
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// My comments for Foo
    /// </summary>
    public partial class Foo {
        ...
    }
}

但我的问题是看,在哪里以及如何我可以在模板中添加此更改。
通过创建新的.edmx文件生成T4模板。
我已经看过一些教程,但没有进一步解释这种T4模板。

有什么想法吗?

亲切的问候

更新:
我在这篇文章中添加了更多信息并正确更改了标题。
在此之前,我认为名称“webEntities.tt”很常见,例如“web.config”,但这是错误的。

1 个答案:

答案 0 :(得分:0)

如果您想要的只是注释并且不注入任何其他信息,那么这很简单。我做了类似的事情。

你有两个.tt文件。您正在查看的评论部分位于YourModel.Context.tt中。您只需将其复制出来并将其放入YourModel.tt并运行自定义工具。

文件的顶部应该看起来像这样

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

const string inputFile = @"YourModel.edmx";
var textTransform = DynamicTextTransformation.Create(this);
var code = new CodeGenerationTools(this);
var ef = new MetadataTools(this);
var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
var fileManager = EntityFrameworkTemplateFileManager.Create(this);
var itemCollection = new EdmMetadataLoader(textTransform.Host, textTransform.Errors).CreateEdmItemCollection(inputFile);
var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);

if (!typeMapper.VerifyCaseInsensitiveTypeUniqueness(typeMapper.GetAllGlobalItems(itemCollection), inputFile))
{
    return string.Empty;
}

#>
//------------------------------------------------------------------------------
// <auto-generated>
// <#=GetResourceString("Template_GeneratedCodeCommentLine1")#>
// 
// <#=GetResourceString("Template_GeneratedCodeCommentLine2")#>
// <#=GetResourceString("Template_GeneratedCodeCommentLine3")#>
// </auto-generated>
//------------------------------------------------------------------------------

<#

更新

在您的情况下,如果您希望它位于命名空间的下方,您应该使用此部分代码并将其复制到此处

foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
    fileManager.StartNewFile(entity.Name + ".cs");
    BeginNamespace(code);
#>
// <Summary>
//    My Comments for <#=entity.Name #>
// </Summary>
//--
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
<#=codeStringGenerator.EntityClassOpening(entity)#>
{
<#