我正在尝试使用大量链接表序列化大量信息,以便我可以通过Web服务传递它并使用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;
}
}
答案 0 :(得分:0)
数据库就是您存储信息的方式。它与您向用户呈现该信息的方式没有多大关系。
没有必要让事情变得复杂得多。你向用户展示了什么?可能只是网格和细节页面,也许是输入表单,对吧?
因此,请提供仅读取您需要显示的数据的方法,并仅更新您需要更新的数据。
以下是一个示例:假设您有一个必须填充数据的网格。而不是通过关系等获取数据,只需获取行列表并将该列表绑定到网格。使用Entity Framework执行此操作的一种简单方法是在数据库中创建仅包含要显示的字段的VIEW,然后将该VIEW映射到实体框架中的实体,然后查询该实体。无论如何,网格都是只读的(可编辑的网格是完全没必要的,并且通常对最终用户来说非常混乱),并且您可以通过这种方式简化数据检索。
同样,对于详细信息页面甚至输入表单,您可以提供仅包含您需要显示的数据的VIEW。要填充输入表单中的下拉列表,您只需使用简单查询即可读取所需实体的值。
另见以下链接:
祝你好运!