尽管将 PreserveReferencesHandling 设置为“None",但 Json.Net 仍将

Json.Net adding $id to EF objects despite setting PreserveReferencesHandling to quot;Nonequot;(尽管将 PreserveReferencesHandling 设置为“None,但 Json.Net 仍将 $id 添加到 EF 对象)
本文介绍了尽管将 PreserveReferencesHandling 设置为“None",但 Json.Net 仍将 $id 添加到 EF 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经看过 如何在 JSON 序列化期间删除 $id 但给出的答案似乎对我不起作用,我希望有人能找出我做错了什么.

I've already looked at how to remove $id during JSON serialization but the answers given do not seem to be working for me and I hope someone can figure out what I am doing wrong.

这是我的代码:

return JsonConvert.SerializeObject(target, new JsonSerializerSettings {
    NullValueHandling = NullValueHandling.Include,
    PreserveReferencesHandling = PreserveReferencesHandling.None,
    ContractResolver = new CustomContractResolver(),
    Converters = CustomConverters
});

这个输出仍然带有 $id's,但是,仅在实体框架对象上,这是设计使然吗?如果是这样,有什么方法可以防止实体框架对象上的那些 $id ?

The output from this is still coming out with $id's, BUT, only on Entity Framework objects, is this by design? If so, is there any way to prevent those $id's on Entity Framework objects?

推荐答案

自定义 ContractResolver 设置会覆盖 PreserveReferencesHandling 设置.

The custom ContractResolver setting overrides the PreserveReferencesHandling setting.

DefaultContractResolver/IContractResolver 的实现中,添加这个;

In your implementation of DefaultContractResolver/IContractResolver, add this;

public override JsonContract ResolveContract(Type type) {
    var contract = base.ResolveContract(type);
    contract.IsReference = false;
    return contract;
}

这与没有自定义 ContractResolverPreserveReferencesHandling.None 设置类似.

This behaves similarly to the PreserveReferencesHandling.None setting without a custom ContractResolver.

这篇关于尽管将 PreserveReferencesHandling 设置为“None",但 Json.Net 仍将 $id 添加到 EF 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)