ScriptIgnore 元数据
我正在使用 JavaScriptSerializer 序列化 EF4 实体,并在尝试将它们序列化为 JSON 时获取循环引用。
我不想编辑生成的模型,因此我提供了一个元数据类,并将这些属性标记为 ScriptIgnore,但我猜 JavaScriptSerializer 不会利用元数据,因为我仍然收到相同的错误。
那么在这种情况下,你会如何将你的对象序列化为 json 呢?有没有一种方法可以在不使用 ViewModels 并且可能使用默认的 JavaScriptSerializer 的情况下执行此操作。
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, would 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过添加
JavaScriptConverter
像这样。请注意,我们不需要对正在序列化的实体进行任何特殊访问来执行此操作,但如果您有很多受此问题影响的实体,这可能会很痛苦。如果这是一个可能的问题,那么只编写一个可以更直接控制的 DTO 类(与模型孪生)可能会减少工作量。您不编辑生成的类是正确的,并且(令人沮丧)您不能使用
部分类
向在部分类
的另一部分中声明的成员添加属性。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 thepartial class
.