ScriptIgnore 元数据

发布于 2024-10-01 23:33:58 字数 277 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百变从容 2024-10-08 23:33:58

通过添加 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 the partial class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文