简体   繁体   English

如何序列化实体框架实体(EF 5.0)?

[英]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.

相关问题 如何使用 Entity Framework 和 .NET 5.0 构建查询以在相关表中查找不匹配的实体? - How do I contruct a query to find unmatched entities in related table with Entity Framework and .NET 5.0? 如何在实体框架中将一个实体映射到许多实体? - How do I map one entity to many entities in Entity Framework? 如何在Entity Framework 5.0生成的所有实体中添加Serializable属性 - How to add Serializable attributes in all entities generated by Entity Framework 5.0 如何清除实体框架中的跟踪实体 - How do I clear tracked entities in entity framework 如何在 Entity Framework LINQ To Entities 中进行联合? - How can I do a Union all in Entity Framework LINQ To Entities? WebApi无法序列化实体框架实体 - WebApi can't serialize Entity Framework entities 如何使用AutoMapper更新具有嵌套实体的实体,并使用实体框架保存更新的实体? - How do I update an entity with nested entities with AutoMapper and save the updated Entity with Entity Framework? 如何合并EF实体集? - How do I merge sets of EF entities? 如何使用 Entity Framework Core 定义一个实体 class 作为一个集合在多个实体之间共享? - How do I define an entity class to be shared as a collection across multiple entities using Entity Framework Core? 如何使用Entity Framework Core 2.0为X实体创建链接的X实体 - How do I create linked X entities for a X entity using Entity Framework Core 2.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM