I was trying trying to serialize a domain model and ran into an issue where I need to convert a dynamic proxy into a POCO. The issue I ran into was that circular references exist by way of virtual properties in the model. Although I attempted to use [ScriptIgnore] in order to have the serializer not parse those properties, it still does. I believe that this is because the objects are dynamic proxies and there is still some remnants in the properties which cause the parser to enter (which in turn causes a recursion error “circular reference” – I tried limiting the recursion to 3 steps but I got an error of “Recursive steps exceeded”).
How can I convert an object from a dynamic proxy to a POCO so that it can be serialized?
Edit: Simple example
public class One : BaseViewModel
{
public int OneId { get; set; }
public virtual ICollection<Two> Two { get; set; }
}
public class Two
{
public int TwoId { get; set; }
public int OneId { get; set; }
[ScriptIgnore]
public virtual One One { get; set; }
}
public abstract class BaseViewModel
{
public string AsJson()
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(this);
}
}
This is a known issue
(so presumably you have to wait for a following preview release or the final release)
http://connect.microsoft.com/VisualStudio/feedback/details/723060/ef-4-2-code-first-property-attributes-not-honoured
that is fixed in .NET 4.5
From the comments on that issue, it looks like you can work around by using NonSerializedAttribute instead of ScriptIgnoreAttribute if you are using the current version of JSON.Net