Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML serialization errors when trying to serialize Entity Framework objects

I have entities that I am getting via Entity Framework. I'm using Code-First so they're POCOs. When I try to XML Serialize them using XmlSerializer, I get the following error:

The type System.Data.Entity.DynamicProxies.Song_C59F4614EED1B7373D79AAB4E7263036C9CF6543274A9D62A9D8494FB01F2127 was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

Anybody got any ideas on how to get around this (short of creating a whole new object)?

like image 394
ajma Avatar asked Feb 06 '11 07:02

ajma


2 Answers

Just saying POCO doesn't really help (especially in this case since it looks like you're using proxies). Proxies come in handy in a lot of cases but make things like serialization more difficult since the actual object being serialized is not really your object but an instance of a proxy.

This blog post should give you your answer. http://blogs.msdn.com/b/adonet/archive/2010/01/05/poco-proxies-part-2-serializing-poco-proxies.aspx

like image 168
Shiv Kumar Avatar answered Oct 31 '22 22:10

Shiv Kumar


Sorry, I know I'm coming at this a bit late (a couple YEARS late), but if you don't need the proxy objects for lazy loading, you can do this:

Configuration.ProxyCreationEnabled = false;

in your Context. Worked like a charm for me. Shiv Kumar actually gives better insight into why, but this at least will get you back to work (again, assuming you don't need the proxies).

like image 22
Chris Simmons Avatar answered Oct 31 '22 21:10

Chris Simmons