Adding custom property attributes in Entity Framework code

前端 未结 6 1727
迷失自我
迷失自我 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:05

    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)));
    }
    

提交回复
热议问题