将 XML 文档/注释添加到 EF 生成的类中的属性/字段

发布于 2024-12-08 14:06:49 字数 101 浏览 0 评论 0原文

我习惯用标准 XML 文档来注释属性和类,它的含义/它们的作用。

但在 EF 生成的类中,当我重新生成模型时,这些都消失了。

还有其他方法可以做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嗼ふ静 2024-12-15 14:06:50

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

No. You will have to modify your T4 template used to generate classes (or create new custom tool for class generation) to make these comments for you.

时光礼记 2024-12-15 14:06:50

EF 生成的类都是“部分”类。因此,定义一个具有相同类骨架结构的新文件,并定义您对这些文件的注释。

示例:

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

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

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

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

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 {}
鹿港小镇 2024-12-15 14:06:50

这是一个非常古老的线程,但目前还不清楚该代码插入到 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)#>

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)#>
那伤。 2024-12-15 14:06:49

正如 Ladislav 在他的回答中所述,您需要修改 T4 模板,以便注释将包含在生成的代码中。这个答案取自这篇文章< /a>:

首先,您需要在模型设计器的属性框中指定您的注释。在文档下 ->详细描述和摘要。

然后,在模板中,您可以将其添加到要记录的属性上方:

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

这将在生成的代码中的属性上方创建一个摘要块。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文