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