Serializing Entity Framework problems

后端 未结 5 1965
無奈伤痛
無奈伤痛 2020-12-16 05:15

Like several other people, I\'m having problems serializing Entity Framework objects, so that I can send the data over AJAX in a JSON format.

I\'ve got the following

5条回答
  •  不知归路
    2020-12-16 05:31

    Your error occured due to some "Reference" classes generated by EF for some entities with 1:1 relations and that the JavaScriptSerializer failed to serialize. I've used a workaround by adding a new condition :

        !p.Name.EndsWith("Reference")
    

    The code to get the complex properties looks like this :

        var complexProperties = from p in type.GetProperties()
                                        where p.CanWrite &&
                                              p.CanRead &&
                                              !p.Name.EndsWith("Reference") &&
                                              !_builtInTypes.Contains(p.PropertyType) &&
                                              !_processedObjects.Contains(p.GetValue(obj, null)
                                                == null
                                                ? 0
                                                : p.GetValue(obj, null).GetHashCode())
                                        select p;
    

    Hope this help you.

提交回复
热议问题