The problem of serializing an object into a JSON string after using EntityFramework to cause a circular reference

Source: Internet
Author: User

First look at a model entity class generated by a T4 template

1 copyright belongs to the author. 2 Commercial Reprint please contact the author for authorization, non-commercial reprint please specify the source. 3 Roll Cat4Link: http://ANNEKE.CN/ARTICLEINFO/DETIAL/155 Source: anneke.cn6 7 //------------------------------------------------------------------------------8 //<auto-generated>9 //This code has been generated from the template. Ten // One //manually changing this file may cause unexpected behavior in your application.  A //If you regenerate the code, the manual changes to this file are overwritten.  - //</auto-generated> - //------------------------------------------------------------------------------ the   - namespaceMyblog.model - { -     usingNewtonsoft.json; +     usingSystem; -     usingSystem.Collections.Generic; +      A      Public Partial classArticleType at     { -[System.Diagnostics.CodeAnalysis.SuppressMessage ("Microsoft.Usage","ca2214:donotcalloverridablemethodsinconstructors")] -          PublicArticleType () -         { -              This. Articleinfo =NewHashset<articleinfo>(); -         } in      -          Public intId {Get;Set; } to          Public stringTitle {Get;Set; } +          Public intParentID {Get;Set; } -      the[System.Diagnostics.CodeAnalysis.SuppressMessage ("Microsoft.Usage","ca2227:collectionpropertiesshouldbereadonly")] * [Jsonignore] $          Public VirtualIcollection<articleinfo> Articleinfo {Get;Set; }Panax Notoginseng     } -}

This is an article classification entity, with the article entity, is a one-to-many relationship (a category can have many articles, and an article only a category)

Here is a navigation property that causes circular references when the object is serialized as a JSON string in our controller.

copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source. Volume Cat Link: http://ANNEKE.CN/ARTICLEINFO/DETIAL/15 Source: anneke.cn Public Virtualgetset; }

Workaround: We use Json.net-newtonsoft to serialize first to add an attribute to the navigation property, which indicates that the navigation property is ignored when serializing

copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source. Volume Cat Link: http://ANNEKE.CN/ARTICLEINFO/DETIAL/15 Source: Anneke.cn[jsonignore]public  virtualgetset; }

You can then serialize the operation.

copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source. Volume Cat Link: http://ANNEKE.CN/ARTICLEINFO/DETIAL/15 Source: anneke.cnstring Json=jsonconvert.serializeobject (ArticleType);

The problem of serializing an object into a JSON string after using EntityFramework to cause a circular reference

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.