简体   繁体   English

将联结表映射到自身

[英]Mapping junction table to self

I'm struggling with mappings that uses a junction table that references the same table. 我正在努力使用使用引用同一表的联结表的映射。 The idea is to be able to track all links between the pages. 想法是能够跟踪页面之间的所有链接。

Junction table: 连接表:

CREATE TABLE WikiPageLinks (
    Page int NOT NULL, 
    LinkedPage int NOT NULL
)

Main table: 主表:

CREATE TABLE WikiPages (
    Id int NOT NULL IDENTITY, 
    PageName nvarchar(50) NOT NULL, 
    Title nvarchar(50) NOT NULL, 
CONSTRAINT PK_WikiPages PRIMARY KEY (Id)
)

And class: 和类:

public class WikiPage
{
    public virtual int Id { get; protected set; }
    public virtual IEnumerable<WikiPageLink> BackReferences {get;}
    public virtual IEnumerable<WikiPageLink> References {get; }
}
  • BackReferences = Current page is WikiPageLinks .LinkedPage BackReferences =当前页面是WikiPageLinks .LinkedPage
  • References = Current page is WikiPageLinks.Page 参考=当前页面是WikiPageLinks.Page

How should the mapping look like? 映射应如何显示?

if there are no additional columns in the link then i would cut it out 如果链接中没有其他列,那么我会删掉它

public class WikiPage
{
    public virtual int Id { get; protected set; }
    public virtual IEnumerable<WikiPage> BackReferences { get; }
    public virtual IEnumerable<WikiPage> References { get; }
}

// WikiPageMap : ClassMap<WikiPage>
public WikiPageMap()
{
    ...
    HasManyToMany(wp => wp.References)
        .Table("WikiPageLinks")
        .ParentKeyColumn("parent_page_id")
        .ChildKeyColumn("referenced_page_id");

    HasManyToMany(wp => wp.BackReferences)
        .Table("WikiPageLinks")
        .ParentKeyColumn("referenced_page_id")
        .ChildKeyColumn("parent_page_id")
        .Inverse();

    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM