[英]How do I serialize Entity Framework entities (EF 5.0)?
I have a problem serializing entities to ASP.NET ViewState because they are not marked as [Serializable]. 我在将实体序列化为ASP.NET ViewState时遇到问题,因为它们没有被标记为[可序列化]。 What is the recommended approach to solve this problem ?
建议使用什么方法解决此问题?
Is there any reason why you cannot mark entity as [Serializable]? 有什么原因不能将实体标记为[可序列化]? But its better not to put entity itself in ViewState, because the size of page could increase a lot as described here http://janheggernes.net/post/2011/02/23/Entity-Framework-with-Caching-and-ViewState.aspx Its better approach to create some simple class as model and copy only what you need from entity, then you can mark it [Serializable] and save in ViewState
但是最好不要将实体本身放在ViewState中,因为页面的大小可能会增加很多,如此处所述http://janheggernes.net/post/2011/02/23/Entity-Framework-with-Caching-and-ViewState .aspx更好的方法是创建一些简单的类作为模型并仅复制实体中需要的内容,然后可以将其标记为[Serializable]并保存在ViewState中
Adomas answer might be the best answer, but for me (writing Intranet applications on a single network location) size of the page wasn't an issue (writing understandable code for my collegue and me was, and we are old-school asp programmers). Adomas的答案可能是最好的答案,但对我来说(在单个网络位置上编写Intranet应用程序)不是页面大小的问题(为我和我的同事编写可理解的代码,而且我们是老式的ASP程序员) 。 I have proposed another solution on this page using the [Serializable] attribute marker: https://stackoverflow.com/a/28140970/1390342
我在此页面上使用[Serializable]属性标记提出了另一种解决方案: https : //stackoverflow.com/a/28140970/1390342
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.