0

多くのリンクされたテーブルを使用して大量の情報をシリアル化し、Web サービスを介して渡して ajax で呼び出すことができるようにしようとしています。私が抱えている問題は、1対多の関係と多対多の関係があり、これに慣れていないため、データベースの無限ループに続く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 でこれを行う簡単な方法の 1 つは、表示するフィールドのみを含む VIEW をデータベースに作成し、その VIEW を Entity Framework のエンティティにマップしてから、そのエンティティにクエリを実行することです。いずれにせよ、グリッドは読み取り専用であり (編集可能なグリッドは完全に不要であり、エンド ユーザーにとって非常に混乱することがよくあります)、この方法でデータの取得を簡素化します。

同様に、詳細ページや入力フォームに対しても、表示する必要のあるデータのみを含む VIEW を提供できます。入力フォームのドロップダウン リストを埋めるために、必要なエンティティの値を簡単なクエリで読み取ることができます。

次のリンクも参照してください。

幸運を!

于 2013-07-25T09:30:46.827 に答える