I am using JavaScriptSerializer to serialize EF4 Entities, and getting circular reference while trying to serializing them to JSON.
I do no want to edit the generated models so I supplied a metadata class, and marked those properties as ScriptIgnore, but I guess JavaScriptSerializer does not utilise the metadata, because I am still getting the same error.
So how do you, wo开发者_JAVA百科uld you, serialize your objects to json in this case. Is there a way of doing this without using ViewModels and probably with the default JavaScriptSerializer.
By adding a JavaScriptConverter
like so. Note that we don't need any special access to the entities being serialized to do this, but it might be a huge pain if you have lots of entities that are affected by this issue. If that is a likely problem, it will probably be less work to just write a DTO class (that twins with your model) that you can control more directly.
You are right not to edit the generated classes, and (frustratingly) you can't use a partial class
to add attributes to members declared in another part of the partial class
.
精彩评论