Json.Net adding $id to EF objects despite setting PreserveReferencesHandling to “None”

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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.

Here is my code:

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

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?

回答1:

The custom ContractResolver setting overrides the PreserveReferencesHandling setting.

In your implementation of DefaultContractResolver/IContractResolver, add this;

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

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!