简体   繁体   中英

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

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). I have proposed another solution on this page using the [Serializable] attribute marker: https://stackoverflow.com/a/28140970/1390342

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM