Monday, 29 July 2013

Simple steps to modify T4 template generated by Entity Framework 5 to add attributes to Entity

Microsoft Entity Framework generates a T4 template that adds entity models. This is really helpful as we can use the template to create the same set of entities wherever needed in the same or another project. However one of thing that the template does not provide is the data annotation attributes and other entity attributes. However there are a few templates available online which do this. In one of my requirements I was needed to add a few attributes to the entities and I did not find a template online that meets my exact requirements. So I decided to modify the model template generated by EF5. My requirement was to add the attributes: Required, DisplayName, StringLength and EdmScalarPropertyAttribute. For example I want the below way:

public partial class Section
{
        [DisplayName("Section Id")]
        [Required]
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        public int SectionId { get; set; }
     
        [DisplayName("Section Name")]
        [Required]
        [StringLength(50)]
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        public string SectionName { get; set; }
     
        public byte[] RowId { get; set; }
  }
  
After a little searching in the internet I got an example:

 
This is a quite good example to follow. After a going through a few more references I modified the generated T4 template to generate the entities the way I want. There are basically following two steps to do.
  1. Add code for adding assembly reference to the project (ex: System.ComponentModel.DataAnnotations)
  2. Add necessary methods for adding the attributes.
The final template can be dowloaded along with example from the below link :

https://github.com/susantagit/CodeRepository

My changes to the original template can be detected by search the template "EntityModelTemplate.tt" with the string "Change:".

The project contains template and the folder Entities contains projects that demonstrates the working of the template.

On running the template there may be warning as below :
Compiling transformation: Assuming assembly reference 'EnvDTE, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' matches 'EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a', you may need to supply runtime policy 
In order to avoid this we can add a app.config file with following
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1" appliesTo="v2.0.50318">
      <dependentAssembly>
        <assemblyIdentity name="EnvDTE" publicKeyToken=
            "b03f5f7f11d50a3a"/>
        <bindingRedirect oldVersion="7.0.3300.0"
        newVersion="8.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

1 comment:

  1. Thanks for your blog Susanta.. It is very clear and well organized. Looking forward for more blogs :)

    ReplyDelete