c# - Circular reference when trying to serialize database -
i'm trying serialize whole bunch of information lot of linked tables can pass through web service , call ajax. problem i'm having there 1 many relationships, , many many relationships , i'm new i'm not sure how stop json following infinite loops through database.
is there missing script ignore on or should trying find different solution?
i need data in pagedata
public class sitedata { public sitedata() { this.urlresponse = new list<response>(); } public sitedata(string url, string robots, string sitemap, bool googleverification, bool bingverification, list<response> urlresponse) { this.domainurl = url; this.robots = robots; this.sitemap = sitemap; this.googleverification = googleverification; this.bingverification = bingverification; this.urlresponse = urlresponse; } public sitedata(string url) { // todo: complete member initialization this.domainurl = url; } [key] public int id { get; set; } [required] public string domainurl { get; set; } public string robots { get; set; } public string sitemap { get; set; } public bool googleverification { get; set; } public bool bingverification { get; set; } public list<response> urlresponse { get; set; } [scriptignore] public virtual icollection<pagedata> pagedatas { get; set; } } public class pagedata { [key] [required] public int id { get; set; } [required] public string pageurl { get; set; } public string analytics { get; set; } public bool paginated { get; set; } public bool flash { get; set; } public bool iframe { get; set; } public bool noindexfollow { get; set; } public bool schematag { get; set; } public virtual icollection<platform> platforms { get; set; } public virtual icollection<alttag> alttags { get; set; } public virtual icollection<canonical> canonicals { get; set; } public virtual icollection<metatitle> metatitles { get; set; } public virtual icollection<metadesc> metadescs { get; set; } public virtual icollection<bloglocation> bloglocations { get; set; } public virtual icollection<h1> h1s { get; set; } public virtual icollection<h2> h2s { get; set; } public virtual icollection<h3> h3s { get; set; } public virtual icollection<viewstate> viewstates { get; set; } //[foreignkey("domainurl")] public sitedata domainurl { get; set; } //public virtual icollection<sitedata> sitedata { get; set; } } public class platform { public platform() { } [key] public int keyid { get; set; } public string platformextension { get; set; } public int responsecode { get; set; } [foreignkey("pagedata")] public int id { get; set; } [scriptignore] public virtual pagedata pagedata { get; set; } } public class alttag { public alttag() { } public alttag(int id, string alttag) { this.id = id; this.alttagstring = alttag; } [key] public int keyid { get; set; } public string alttagstring { get; set; } [foreignkey("pagedata")] public int id { get; set; } [scriptignore] public virtual pagedata pagedata { get; set; } } public class canonical { public canonical() { } public canonical(int id, string alttag) { this.id = id; this.canonicalstring = alttag; } [key] public int keyid { get; set; } public string canonicalstring { get; set; } [foreignkey("pagedata")] public int id { get; set; } [scriptignore] public virtual pagedata pagedata { get; set; } } public class metatitle { public metatitle() { } public metatitle(int id, string metatitle) { this.id = id; this.metatitlestring = metatitle; } [key] public int keyid { get; set; } public string metatitlestring { get; set; } [foreignkey("pagedata")] public int id { get; set; } [scriptignore] public virtual pagedata pagedata { get; set; } } public class metadesc { public metadesc() { } public metadesc(int id, string metadesc) { this.id = id; this.metadescstring = metadesc; } [key] public int keyid { get; set; } public string metadescstring { get; set; } [foreignkey("pagedata")] public int id { get; set; } [scriptignore] public virtual pagedata pagedata { get; set; } } public class bloglocation { public bloglocation() { } public bloglocation(int id, string blog) { this.id = id; this.blogloc = blog; } [key] public int keyid { get; set; } public string blogloc { get; set; } [foreignkey("pagedata")] public int id { get; set; } [scriptignore] public virtual pagedata pagedata { get; set; } } public class h1 { public h1() { } public h1(int id, string h1) { this.id = id; this.h1string = h1; } [key] public int keyid { get; set; } public string h1string { get; set; } [foreignkey("pagedata")] public int id { get; set; } [scriptignore] public virtual pagedata pagedata { get; set; } } public class h2 { public h2() { } public h2(int id, string h2) { this.id = id; this.h2string = h2; } [key] public int keyid { get; set; } public string h2string { get; set; } [foreignkey("pagedata")] public int id { get; set; } [scriptignore] public virtual pagedata pagedata { get; set; } } public class h3 { public h3() { } public h3(int id, string h3) { this.id = id; this.h3string = h3; } [key] public int keyid { get; set; } public string h3string { get; set; } [foreignkey("pagedata")] public int id { get; set; } [scriptignore] public virtual pagedata pagedata { get; set; } } public class viewstate { public viewstate() { this.existance = new bool(); } [key] public int keyid { get; set; } public bool existance { get; set; } public int size { get; set; } [foreignkey("pagedata")] public int id { get; set; } [scriptignore] public virtual pagedata pagedata { get; set; } }
the actual call:
[webmethod] public string getpage(string pageid) { using (var db = new datacontext()) { pagedata page = db.pagedatas.find(int32.parse(pageid)); string json = null; javascriptserializer jss = new javascriptserializer(); json = jss.serialize(page); return json; } }
a database how store information. has not how present information user.
there's no need make things more complex have be. show user? grids , detail pages, maybe input forms, right?
so provide ways read data need show, , update data need update.
here's example: have grid must fill data. rather fetch data relationships etc., fetch list of rows , bind list grid. 1 easy way entity framework create view in database containing fields want show, map view entity in entity framework, , querying entity. grids read-only anyway (editable grids unnecessary , confusing end users) , simplify data retrieval way.
similarly, detail pages , input forms can provide views contain data need show. filling drop-down lists in input forms can read required entity's values simple query.
see following links:
- entity framework , sql server view
- http://social.msdn.microsoft.com/forums/en-us/5d834520-8643-4ada-955a-8173178880f4/generate-views-in-entity-framework-50-code-first
- http://blogs.u2u.be/diederik/post/2011/01/31/building-an-entity-framework-40-model-on-views-practical-tips.aspx
good luck!
Comments
Post a Comment