简体   繁体   中英

Add XML documentation / comments to properties/fields in EF generated classes

i have the habbit to comment properties and classes with the standard XML documentation, what it means / what they do.

But in EF generated classes off course, these are all gone when i regenerate the model.

Is there another way to do this?

As Ladislav stated in his answer, you need to modify the T4 template so the comments will be included in the generated code. This answer was taken from this article :

First of all you need to specify your comments in the properties boxes of the model designer. Under Documentation -> Long Description, and Summary.

Then in the template, you can for example add this above the property you want to document:

<#if (!ReferenceEquals(edmProperty.Documentation, null))
{
#>
/// <summary>
/// <#=edmProperty.Documentation.Summary#> – <#=edmProperty.Documentation.LongDescription#>
/// </summary>
<#}#>

This will create a summary block above your property in the generated code.

不可以。您必须修改用于生成类的 T4 模板(或为类生成创建新的自定义工具)才能为您做出这些评论。

This is a very old thread, but it wasn't immediately clear where in the t4 this code is inserted. The version of the t4 as of this writing follows. This also puts the LongDescription in the Remarks section, if it exists.

Preceeding code:

<#
    }

    var simpleProperties = typeMapper.GetSimpleProperties(entity);
    if (simpleProperties.Any())
    {
        foreach (var edmProperty in simpleProperties)
        {
#>

Inserted code:

<#
            if (!ReferenceEquals(edmProperty.Documentation, null))
            {
#>

    /// <summary>
    /// <#=edmProperty.Documentation.Summary#>
    /// </summary>
<#              if (edmProperty.Documentation.LongDescription.Length > 0)
                {#>
    /// <remarks>
    /// <#=edmProperty.Documentation.LongDescription#>
    /// </remarks>
<#              }
            }#>

Succeeding code:

    <#=codeStringGenerator.Property(edmProperty)#>

The EF generated classes are all "partial" classes. So, define a new file with the same class skeleton structure, and define your comments on those.

Example:

The EF generated class (Model.designer.cs):

public partial class Student : EntityObject {... // bunch of generated code}

Your own file (ModelDocumentation.cs):

/// <summary> Student documentation... </summary>
public partial class Student {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM