Why is the serialization type "system. Data. entity. dynamicproxies. photos .... Circular references are detected when this object is present.

Source: Internet
Author: User

I encountered the following problem today. The kid stayed for your reference.

 

 

 

"/" Indicates a server error in the application.

 

Circular references are detected when the serialization type is "system. Data. entity. dynamicproxies. References.

 

The source code is as follows:

Public actionresult getalluserinfos () {// The data on the current page is returned to the foreground using the internal method: JSON: {Total: 30, rows: []} // The current table will automatically send an asynchronous request to the background, and then pass the parameter rows page var pagesize = request ["rows"] = NULL? 10: Int. parse (request ["rows"]); var pageindex = request ["page"] = NULL? 1: Int. parse (request ["page"]); // The object page of the current page var pagedata = dB. photos. orderby (u => U. PID ). skip (pagesize * (pageindex-1 )). take (pagesize); // var pagedata = dB. photos. tolist (); // Why is there this object: The foreground needs such an object, first assembles it into an anonymous class, and then serializes var result = new {Total = dB. photos. count (), rows = pagedata}; return JSON (result, jsonrequestbehavior. allowget );}

I checked the solution on Baidu and added this sentence.

DB. configuration. proxycreationenabled = false; I tried it, but I don't understand why this error occurs.

 

 

 

 

It seems I know. this table has a one-to-multiple relationship with another table. When table 1 is serialized, fields associated with table 2 will be found and serialized to another table 2, another table 2 also has a field associated with table 1. in this way. this error occurs during serialization! There are three solutions:

1. the simplest way is to start with Entity Framework and disable lazyloading and proxycreation. because after lazyloading is disabled. when the order object is parsed by. net, its attribute order_details returns NULL (not automatically loaded ). this problem is also avoided.

Of course, the disadvantage of this method will cause the convenience of lazyloading to be sacrificed when the program accesses the entity object in the future. You need to manually solve this problem.

 

1. db.Configuration.LazyLoadingEnabled =  false ; 2. db.Configuration.ProxyCreationEnabled =  false ; 3.  4. return  db.Orders.AsEnumerable();

 

2. Set JSON. Net to ignore loop reference

Through webapiconfig. CS of app_start, set config. formatters. jsonformatter. serializersettings. referenceloophandling = newtonsoft. JSON. referenceloophandling. Ignore;

When using this method, you must note that it only ignores the errors of the cyclic reference, but in fact it will automatically parse the attributes of the objects to be output, therefore, infinite loops may occur if data is dependent on each other.

 

3. Set JSON. Net to avoid loop reference

Through webapiconfig. CS of app_start, Set

Config. formatters. jsonformatter. serializersettings. referenceloophandling = newtonsoft. JSON. referenceloophandling. serialize;

Config. formatters. jsonformatter. serializersettings. preservereferenceshandling = newtonsoft. JSON. preservereferenceshandling. objects;

The difference between this approach and 2 is that it will replace repeated objects with a representative, such as the JSON format below
1: [{"$ id": "1", "category": {"$ id": "2", "Products": [{"$ id ": "3", "category": {"$ ref": "2"}, "ID": 2, "name": "yogurt" },{ "$ ref ": "1"}], "ID": 1, "name": "Diary"}, "ID": 1, "name": "whole milk "}, {"$ ref": "3"}]

Therefore, for data, this method will automatically parse the attributes of the objects to be output layer by layer, just to avoid outputting too much data.

 

4. Manual setting to avoid loop reference

As in the 3 mode, you can use the [jsonignore] and [jsonobject (isreference = true)] details to set each attribute to be output more accurately.

Disadvantage: 1. Complicated settings. 2. Only general settings are allowed. 3. settings cannot be separated from entity object class because they must be set directly or in the partial class mode.

 

The above practices have their own advantages and disadvantages, and there is no common mode, which must be determined by the requirements. This problem is the same as that of the early Ria service, however, a general mode is provided here, that is, method 1 + method 2 or 3.

Disable lazyloading, and use the program method (through the include method) to determine the attributes to be output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.