0

我正在尝试使用大量链接表序列化一大堆信息,以便我可以通过 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;
        }
    }
4

1 回答 1

0

数据库就是您存储信息的方式。这与您如何向用户呈现该信息无关。

没有必要让事情变得比它们必须的更复杂。你向用户展示了什么?可能只是网格和详细信息页面,也可能是输入表单,对吧?

所以提供方法来只读取你需要显示的数据,并且只更新你需要更新的数据。

这是一个示例:假设您有一个必须用数据填充的网格。而不是获取具有关系等的数据,只需获取行列表并将该列表绑定到网格。使用 Entity Framework 执行此操作的一种简单方法是在数据库中创建一个仅包含您要显示的字段的 VIEW,然后将该 VIEW 映射到 Entity Framework 中的一个实体,然后查询该实体。无论如何,网格都是只读的(可编辑的网格是完全没有必要的,而且对于最终用户来说通常非常混乱),并且您可以通过这种方式简化数据检索。

同样,对于详细信息页面甚至输入表单,您可以提供只包含您需要显示的数据的视图。要填写输入表单中的下拉列表,您只需通过简单查询即可读取所需实体的值。

另请参阅以下链接:

祝你好运!

于 2013-07-25T09:30:46.827 回答