Entity Framework 4.0 Autogenerated Classes not marked as Serializable

爷,独闯天下 提交于 2019-12-12 13:45:24

问题


One strange thing i've got to see in Entity Framework 4.0 V2 Auto Generated Classes(tt) is that the classes are not marked as Serializable. Although they are having DataContract attribute for WCF.

Now the problem is, when I store the POCO object into viewstate it throws me an exception saying that the class is not serializable.

If I generate the classes without the t4 templates or using the defualt class generating scheme, what i see is that the generated classes are having the Serializable attribute on them.

But unfortunately i can not make use of the default Entity Generation Scheme. Since I want to accomodate some custom logic to the autogenerated class which is possible through t4 templates only.

Now what i want to know is:

1) Why the Serializable Attribute is not there in the autogenerated class or am I making any mistake or i am towards a wrong approach.

2) Is it a good idea to customize the EF 4.0 T4 template to accomodate Serializable attribute.

Looking for your valuable suggestion.

Thanks,

Burhan Ghee


回答1:


Yes you can modify the template. Look at Adding [DataMember] [DataContract] attributes in Entity Framework POCO Template for exactly what you need to do to fix your template. Look specifically at the WriteHeader function in the template.




回答2:


The purpose of T4 template is allow you to customize. Customize it fearlessly! You are not only encouraged to customize template, but also the edmx file that your template is based on. see this http://blogs.msdn.com/adonet/archive/2010/03/05/updated-data-model-designer-extension-starter-kit.aspx



来源:https://stackoverflow.com/questions/2756592/entity-framework-4-0-autogenerated-classes-not-marked-as-serializable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!