gpt4 book ai didi

entity-framework - 使用显式包含时,即使在 ProxyCreation false 之后,也会出现 JSON 序列化 Entity Framework 自引用循环错误

转载 作者:行者123 更新时间:2023-12-04 18:44:45 24 4
gpt4 key购买 nike

JSON 序列化 (ASP.Net Web API) 由于自引用循环而失败(这是一个常见问题,原因:被请求的实体延迟加载子实体,并且每个子实体都有对父实体的反向引用)。

解决我发现的问题,但对我没有帮助:

  • Use [JsonIgnore] for navigation properties to be ignored:
    此解决方案有效,但不适用于我的情况。例如:要获取客户信息及其订单,我会快速将 [JsonIgnore] 添加到 Order 类中的 Customer 属性,但是当我想获取订单信息和 Customer 详细信息时,因为 Customer 属性上有 [JsonIgnore] ,它不会包括客户详细信息。
  • Change JSON.Net Serializer Settings to Preserve References :
    无法保留,因为我不需要循环引用数据。
  • Disable Proxy Creation at the Data Context and use explicit loading(this should ideally solve the problem) :
    禁用代理创建会停止延迟加载并无错误地返回数据,但是当我明确包含子实体时,我再次遇到了意外的自引用循环错误! 该错误位于对父实体的反向引用级别。

  • 有相同的经验/建议吗?

    最佳答案

    我尝试了所有建议的解决方案,但没有奏效。最终将 JSON.Net Serializer 的 DefaultContractResolver 重写为:

    public class FilterContractResolver : DefaultContractResolver
    {
    Dictionary<Type, List<string>> _propertiesToIgnore;

    public FilterContractResolver(Dictionary<Type, List<string>> propertiesToIgnore)
    {
    _propertiesToIgnore = propertiesToIgnore;
    }

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
    var property = base.CreateProperty(member, memberSerialization);
    List<string> toIgnore;
    property.Ignored |= ((_propertiesToIgnore.TryGetValue(member.DeclaringType, out toIgnore) || _propertiesToIgnore.TryGetValue(member.DeclaringType.BaseType, out toIgnore)) && toIgnore.Contains(property.PropertyName));
    return property;
    }
    }

    然后创建了一个静态类,它返回一个基于 Controller 被忽略的属性字典:
    public static class CriteriaDefination
    {
    private static Dictionary<string, Dictionary<Type, List<string>>> ToIgnore = new Dictionary<string, Dictionary<Type, List<string>>>
    {
    {
    "tblCustomer", new Dictionary<Type, List<string>>{
    {
    typeof(tblCustomer), new List<string>{
    //include all
    }
    },
    {
    typeof(tblOrder), new List<string>{
    "tblCustomer"//ignore back reference to tblCustomer
    }
    }
    }
    },
    {
    "tblOrder", new Dictionary<Type, List<string>>{
    {
    typeof(tblCustomer), new List<string>{
    "tblOrders"//ignore back reference to tblOrders
    }
    },
    {
    typeof(tblOrder), new List<string>{
    //include all
    }
    }
    }
    }
    };
    public static Dictionary<Type, List<string>> IgnoreList(string key)
    {
    return ToIgnore[key];
    }
    }

    在每个 Controller 中更改 JSON Formatter 如下:
    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new FilterContractResolver(CriteriaDefination.IgnoreList("tblCustomer"));

    关于entity-framework - 使用显式包含时,即使在 ProxyCreation false 之后,也会出现 JSON 序列化 Entity Framework 自引用循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16948827/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com