Add documentation to generated code in entity framework model first

前端 未结 1 1168
一个人的身影
一个人的身影 2020-12-18 11:20

I have been using Entity Framework model first since VS 2010. When I build my project, EF generates a Model.Designer.cs file containing all entities. This designer file also

1条回答
  •  萌比男神i
    2020-12-18 11:55

    I think you'll have to modified the T4 file. I've got the same problem and read through the T4 file a bit, and tried to follow the instruction here: http://karlz.net/blog/index.php/2010/01/16/xml-comments-for-entity-framework/

    However, we're using VS 2012 and the instruction doesn't seem to work 100%. I ended up changing the property generation code at the end of the T4 file and it works exactly how I wanted it to be. The changes are in CodeStringGenerator.Property() and CodeStringGenerator.NavigationProperty()

    public string Property(EdmProperty edmProperty)
    {
        string doc = "";
        if (edmProperty.Documentation != null)
        {
            doc = string.Format(
            CultureInfo.InvariantCulture,
            "\n\t\t/// \n\t\t/// {0} - {1}\n\t\t/// \n\t\t",
            edmProperty.Documentation.Summary ?? "",
            edmProperty.Documentation.LongDescription ?? "");
        }
    
        return doc + string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1} {2} {{ {3}get; {4}set; }}",
            Accessibility.ForProperty(edmProperty),
            _typeMapper.GetTypeName(edmProperty.TypeUsage),
            _code.Escape(edmProperty),
            _code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
            _code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
    }
    
    public string NavigationProperty(NavigationProperty navigationProperty)
    {
        var endType = _typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType());
        string doc = "";
        if (navigationProperty.Documentation != null)
        {
            doc = string.Format(
            CultureInfo.InvariantCulture,
            "\n\t\t/// \n\t\t/// {0} - {1}\n\t\t/// \n\t\t",
            navigationProperty.Documentation.Summary ?? "",
            navigationProperty.Documentation.LongDescription ?? "");
        }
    
        return doc + string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1} {2} {{ {3}get; {4}set; }}",
            AccessibilityAndVirtual(Accessibility.ForProperty(navigationProperty)),
            navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,
            _code.Escape(navigationProperty),
            _code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),
            _code.SpaceAfter(Accessibility.ForSetter(navigationProperty)));
    }
    

    Note that it won't work with class documentation, so you have to do something like this with entity and complex type

    <#=codeStringGenerator.UsingDirectives(inHeader: false)#>
    <#if (!ReferenceEquals(entity.Documentation, null))
    {
    #>
    /// 
    /// <#=entity.Documentation.Summary#> – <#=entity.Documentation.LongDescription#>
    /// 
    <#}#>
    <#=codeStringGenerator.EntityClassOpening(entity)#>
    

    0 讨论(0)
提交回复
热议问题