实体框架4.0自动生成类未标记为可序列化 [英] Entity Framework 4.0 Autogenerated Classes not marked as Serializable

查看:155
本文介绍了实体框架4.0自动生成类未标记为可序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Entity Framework 4.0 V2 Auto Generated Classes(tt)中看到的一件奇怪的事情是这些类没有被标记为Serializable。虽然它们具有用于WCF的DataContract属性。



现在的问题是,当我将POCO对象存储到viewstate中时,会抛出一个异常,表示该类不可序列化。



如果我生成没有t4模板的类或使用defualt类生成方案,我看到的是生成的类在它们上具有Seri​​alizable属性。 p>

但不幸的是,我无法使用默认的实体生成方案。因为我想将自定义的逻辑适应自动生成的类,只能通过t4模板来实现。



现在我想知道的是:



1)为什么可序列化属性不在自动生成的类中,或者我犯了错误,或者我错误的做法。



2)自定义EF 4.0 T4模板以适应Serializable属性是个好主意。



寻找您的宝贵意见。



谢谢,



Burhan Ghee

解决方案

是您可以修改模板。查看在Entity Framework POCO中添加[DataMember] [DataContract]属性模板,您需要做什么来修复您的模板。具体看模板中的WriteHeader函数。


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

解决方案

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.

这篇关于实体框架4.0自动生成类未标记为可序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆