Question

Actually, what I need should be quite simple.

I would like to put an XML comment over all generated classes with their respective class name.
At the moment, the generated classes look like this:

//------------------------------------------------------------------------------
// <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 {
    ...
    }
}

Our T4 template is called WebEntities.tt and is included in our Entity Data Model called WebEntities.edmx.
After modifying WebEntities.tt and doing "Run Custom Tool", I would like to have the following result:

//------------------------------------------------------------------------------
// <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 {
        ...
    }
}

But my problem is to see, where and how I can add this change in the template.
The T4 template is generated by creating a new .edmx file.
I have seen some tutorials but no further explanation for that kind of T4 template.

Any idea?

Kind Regards

UPDATE:
I have added some more information to this post and changed the title properly.
Before, I assumed that the name "webEntities.tt" is common like "web.config" but that is wrong.

Was it helpful?

Solution

Dead simple this if all you want is to have the comment and not inject any additional info. I do something similar.

You have two .tt files. The comment section you are looking at is found in YourModel.Context.tt. You can simply copy it out and put it in YourModel.tt and run the custom tool.

the top of your file should look something like this

<#@ 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>
//------------------------------------------------------------------------------

<#

UPDATE

in your case if you want it just below the namespace you should this section of code and copy it here

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)#>
{
<#
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top