簡體   English   中英

向 EF 生成的類中的屬性/字段添加 XML 文檔/注釋

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

我習慣於使用標准 XML 文檔來評論屬性和類,這意味着什么/它們做什么。

但是在 EF 生成的課程中,當我重新生成模型時,這些都消失了。

有沒有另一種方法可以做到這一點?

正如 Ladislav 在他的回答中所說,您需要修改 T4 模板,以便將注釋包含在生成的代碼中。 這個答案取自這篇文章

首先,您需要在模型設計器的屬性框中指定您的注釋。 在文檔 -> 詳細描述和摘要下。

然后在模板中,例如,您可以在要記錄的屬性上方添加以下內容:

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

這將在生成的代碼中在您的屬性上方創建一個摘要塊。

不可以。您必須修改用於生成類的 T4 模板(或為類生成創建新的自定義工具)才能為您做出這些評論。

這是一個非常古老的線程,但並不清楚該代碼在 t4 中的何處插入。 撰寫本文時 t4 的版本如下。 這也會將 LongDescription 放在備注部分(如果存在)。

前面的代碼:

<#
    }

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

插入的代碼:

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

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

成功代碼:

    <#=codeStringGenerator.Property(edmProperty)#>

EF 生成的類都是“部分”類。 因此,定義一個具有相同類骨架結構的新文件,並定義對這些文件的注釋。

例子:

EF 生成的類(Model.designer.cs):

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

您自己的文件 (ModelDocumentation.cs):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM