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

I use EF 5.0 to create POCO objects and use it in a separate data access layer

I want to tag all entities [Serializable]

How to change a template to add a Serializable attribute?

+8
c # entity-framework
source share
6 answers

Below are the steps for this:

1) Open the file ModelName.Entity.tt

2) Find the next line 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 will see that it has written desire text, you do not even need to create objects again.

+5
source share

If you do not want to edit the template, you can also do this in a separate code file - due to the convenience of the partial classes. So if the types here are Foo , Bar and Baz in the My.Namespace namespace, you can create a separate file in the same project using:

 using System; namespace My.Namespace { [Serializable] partial class Foo {} [Serializable] partial class Bar {} [Serializable] partial class Baz {} } 

It will then be combined with the generated half using the [Serializable] attribute. It will also allow you to add custom methods to these types or provide a body for any partial method implementations that the template declares.

I must, however, warn you: [Serializable] suggests using BinaryFormatter ; this is not necessarily a good candidate. It would be preferable to look at serializers based on contracts. I would be very surprised if the EF template did not have the ability to output attributes for the DataContractSerializer (in particular, [DataContract] / [DataMember] ). BinaryFormatter can be very problematic as you upgrade your software.

+11
source share

if you are using an entity framework 5.0 or higher Add the [Serializable] tag between this code:

 <#=codeStringGenerator.UsingDirectives(inHeader: false)#> [Serializable] <#=codeStringGenerator.EntityClassOpening(entity)#> 
+11
source share

For Entity Framework 6, add Serializable over the two parts in Model.tt

 [Serializable] Partial <#=Accessibility.ForType(complex)#> [Serializable] <#=codeStringGenerator.EntityClassOpening(entity)#> 
+8
source share

Here's what you do for EF 6 (at least);

In the model.tt file, find the following lines:

 public string EntityClassOpening(EntityType entity) { return string.Format( CultureInfo.InvariantCulture, "{0} {1}partial class {2}{3}", Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), _code.Escape(entity), _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)) ); } 

Change it as follows:

 public string EntityClassOpening(EntityType entity) { return string.Format( CultureInfo.InvariantCulture, "{0} " + Environment.NewLine + " {1} {2} partial class {3}{4}", "[Serializable]", Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), _code.Escape(entity), _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))); }
public string EntityClassOpening(EntityType entity) { return string.Format( CultureInfo.InvariantCulture, "{0} " + Environment.NewLine + " {1} {2} partial class {3}{4}", "[Serializable]", Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), _code.Escape(entity), _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType))); } 

Save the .tt file, and the entity class files get the [Serializable] Attribute. Done.

+3
source share

For EF 6, try to find the following code in the .tt file:

 public string EntityClassOpening(EntityType entity) { return string.Format( CultureInfo.InvariantCulture, "{0} {1}partial class {2}{3}", Accessibility.ForType(entity), _code.SpaceAfter(_code.AbstractOption(entity)), _code.Escape(entity), _code.StringBefore(" : ", _typeMapper.GetTypeName(entity.BaseType)) ); } 

It has a string placeholder ("{0} {1}partial class {2}{3}") , you can change it to whatever you want

0
source share

All Articles