Imran Rizvi Imran Rizvi - 2 months ago 12
C# Question

How to add Serializable attributes in all entities generated by Entity Framework 5.0

I am using EF 5.0 to generate POCO entities and using it in a seperate Data Access layer

I want to tag all the entities [Serializable]

How to modify template to add Serializable attribute?

Answer

Following are the steps to do so:

1) Open the ModelName.Entity.tt file

2) Locate following string in this file

<#=Accessibility.ForType(entity)

3) Write [Serializable] above this line, so the code will look like this

[Serializable] 
<#=Accessibility.ForType(entity)

Save the file , open the entity file, you can see that it has the desire tag written, you don't even have to generate entities again.

Comments