Adding custom property attributes in Entity Framework code

前端 未结 6 1737
迷失自我
迷失自我 2020-12-08 14:43

Is there any way to add custom attributes to properties in EF generated code? The only thing I can see as a plausible solution would be to come up with a custom T4 template

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-08 15:21

    You can add this to EDMX file, with Designer also:

    
                
                  [MyCustomAttribute]
                
    
    

    And replace T4:

    void WriteProperty(CodeGenerationTools code, EdmProperty edmProperty)
    {
        WriteProperty(Accessibility.ForProperty(edmProperty),
                      code.Escape(edmProperty.TypeUsage),
                      code.Escape(edmProperty),
                      code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
                      code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
    }
    

    With:

    void WriteProperty(CodeGenerationTools code, EdmProperty edmProperty)
    {
        if(edmProperty.Documentation != null && string.IsNullOrWhiteSpace(edmProperty.Documentation.Summary) == false)
        {
        #>
        <#=edmProperty.Documentation.Summary#>
    <#+
        }
        WriteProperty(Accessibility.ForProperty(edmProperty),
                      code.Escape(edmProperty.TypeUsage),
                      code.Escape(edmProperty),
                      code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
                      code.SpaceAfter(Accessibility.ForSetter(edmProperty)));
    }
    

    And for Entity Framework 6, replace

    public string Property(EdmProperty edmProperty)
    {
        return 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)));
    }
    

    with

    public string Property(EdmProperty edmProperty)
    {
        var description = String.Empty;
        bool isAttribute = false;
    
        if(edmProperty.Documentation != null &&
            string.IsNullOrWhiteSpace(edmProperty.Documentation.Summary) == false)
        {
            string summary = edmProperty.Documentation.Summary;
            if (!String.IsNullOrEmpty(summary) && summary.First() == '[' && summary.Last() == ']')
            {
                isAttribute = true;
            }
    
            if (isAttribute)
            {
                description = String.Format("\r\n\t{0}\r\n\t", summary);
            }
            else
            {
                description = String.Format("\r\n\t/// \r\n\t/// {0}\r\n\t/// \r\n\t", 
                    summary);
            }
    
        }
    
        return string.Format(
            CultureInfo.InvariantCulture,
            "{5}{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)),
            description);
    }
    

    Warnings:

    • Namespaces need to be resolved absolutely.
    • Assumes attributes begin with '[' and end with ']' -- no other error checking
    • If an opening and closing brace isn't found, the entity framework property summary is wrapped in an XML triple slash comment.
    • Attempts to match default visual studio styling information (really just indents) which may or may not be the case for your project. This includes new lines.

    sample output:

    /// 
    /// content type
    /// 
    public System.Guid ContentType { get; set; }
    
    [System.ComponentModel.DisplayName("Last Modified")]
    public System.DateTime LastModified { get; set; }
    

提交回复
热议问题