Milan
Milan

Reputation: 83

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 ?

Upvotes: 0

Views: 2301

Answers (2)

LiQuick.net
LiQuick.net

Reputation: 199

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

Upvotes: 0

Adomas.NET
Adomas.NET

Reputation: 126

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

Upvotes: 1

Related Questions