Я пытаюсь сериализовать целую кучу информации с большим количеством связанных таблиц, чтобы я мог передавать ее через веб-службу и называть ее с помощью ajax. Проблема, с которой я сталкиваюсь, заключается в том, что между некоторыми отношениями есть несколько отношений, а некоторые отношения многие-многие, и поскольку я новичок в этом, я не уверен, как остановить json после бесконечных циклов через мою базу данных.Циркулярная ссылка при попытке сериализации базы данных
Есть ли что-то, что я пропускаю, сценарий игнорирует что-то или я должен пытаться найти совершенно другое решение?
мне нужны все данные в 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; }
}
Фактический вызов:
[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;
}
}
Сначала введите необходимые данные в DTO ...? –
Мне нужно все, кроме материала SiteData, нет ли лучшего способа? – Kayra