none
Data annotations from EDMX file by modifying the Poco template

    Question

  • Hello,

    Is there a way to generate the Data Annotations from EDMX file by modifying the Poco template. It makes sense to generate validations from edmx as database has already the knowledge of what is required and length. Is there any Microsoft and/or external template which can do it?

    Thanks

    Monday, June 14, 2010 11:58 AM

Answers

  • Hello,

    In my case I modified the T4 template to add MaxLength property:

    <# // begin max length
    if (code.Escape(edmProperty.TypeUsage) == "string")
    {
      int maxLength = 0;
      if (Int32.TryParse(edmProperty.TypeUsage.Facets["MaxLength"].Value.ToString(), out maxLength))
      {
    #>	
    public int <#=code.Escape(edmProperty)#>MaxLength
    {
    	<#=code.SpaceAfter(Accessibility.ForGetter(edmProperty))#> get { return <#=code.CreateLiteral(maxLength)#>; }
    }
    <#
    	}
    }
    // end max length
    #>
    Hope that helps.
    • Marked as answer by sas79gi Wednesday, June 16, 2010 3:17 PM
    Wednesday, June 16, 2010 5:13 AM

All replies

  • Hi,

    You can edit the POCO T4 template by yourself and insert the use of data annotations.
    You can read the following post which can get you a starting point of how to do that:
    http://www.olegsych.com/2008/09/t4-tutorial-creating-reusable-code-generation-templates/

    I hope it will help you.


    Gil Fink
    Monday, June 14, 2010 1:16 PM
  • Isn't it there a already a template instead of modfying the existing template? Moreover, I've another question as posted in following link, but I'm unable to get any help there. Could you please help me with that also?

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/ee3b1d1c-e243-449d-9ae1-2e6f2e8e6ddb 
    Monday, June 14, 2010 2:21 PM
  • Hello,

    In my case I modified the T4 template to add MaxLength property:

    <# // begin max length
    if (code.Escape(edmProperty.TypeUsage) == "string")
    {
      int maxLength = 0;
      if (Int32.TryParse(edmProperty.TypeUsage.Facets["MaxLength"].Value.ToString(), out maxLength))
      {
    #>	
    public int <#=code.Escape(edmProperty)#>MaxLength
    {
    	<#=code.SpaceAfter(Accessibility.ForGetter(edmProperty))#> get { return <#=code.CreateLiteral(maxLength)#>; }
    }
    <#
    	}
    }
    // end max length
    #>
    Hope that helps.
    • Marked as answer by sas79gi Wednesday, June 16, 2010 3:17 PM
    Wednesday, June 16, 2010 5:13 AM
  • Could you add some context as to which template you modified and where? I want to do a similar thing to the DbContext templae with EF4.1 Update 1 but instead of adding an attribute, add a "const" for MaxLength (e.g. const uint MyProperty_MaxLen 16) for each string property so I don't have the overhead of reflection. Thanks, Dave
    Tuesday, September 13, 2011 11:10 PM
  • Got it working by adding the following when generating primitives in the WriteProperty call -

            foreach (var edmProperty in primitiveProperties)
            {
    	// add MaxLength from the edmx storage model
    	if (code.Escape(edmProperty.TypeUsage) == "string")
    	{
    	  int maxLength = 0;
    	  if (Int32.TryParse(edmProperty.TypeUsage.Facets["MaxLength"].Value.ToString(), out maxLength))
    	  {
    	#>	
        public const uint <#=code.Escape(edmProperty)#>_MaxLen = <#=code.CreateLiteral(maxLength)#>; // this was added by customizing the SrfClasses.tt template
    	<#
                }
    	}
    
    


     

    Wednesday, September 14, 2011 3:33 PM