多对多 "self" 关系 SQLite:可能吗?

Many to Many "self"-relation SQLite: is it possible?


我正在使用 SQLite.net 和 SQLite-Net Extensions。我想创建多对多 self-relation.
在我的例子中,我有一个 child class 和一些兄弟姐妹,当然兄弟姐妹仍然是 child 类型。因此我尝试实现一个多对多关系:

Child Class

[ManyToMany(typeof(Brotherhood), CascadeOperations = CascadeOperation.All)]
public List<Child> Siblings_DB { get; set; }

兄弟会Class

class Brotherhood
{
    [ForeignKey(typeof(Child))]
    public int ID_child1 { get; set; }

    [ForeignKey(typeof(Child))]
    public int ID_child2 { get; set; }

}

这应该是所有的工作。
然后我创建一个 Child 并在 Siblings 列表中添加一个兄弟,但是当我尝试使用 InsertOrReplaceWithChildren(Child,true) 将 class 保存在数据库中时,只有 ID_child1 是已更新且 ID_child2 保持为 0。
任何的想法?我做错了什么?
亚历克斯

数据库架构必须首先与您要执行的操作兼容。 every 关系的一侧必须是一个键(唯一)属性或属性集,所以不,你不能单独拥有一个 many-to-many 自我关系。但是,如果您创建另一个 table 来保存相关行,并使用一个组合键包含您尝试在其上创建关系的 table 键属性 [s] 的两个副本,那么您可以做到。

假设 table 键是 myPK,然后创建第二个 table 命名,例如 myM2M 和包含属性 two-column 的主键12=] 和 myPK2,它们共同构成了这个新 table 的 pk,然后这两列与第一个 table 中的 myPK 列形成了关系。

你看过this了吗?

在我设法实现多对多自关系的一些尝试之后,我对在同一个中有两个多对多关系感到有点困惑class:我想知道哪个是"official"一。最终我明白两者都是!而兄弟姐妹的总和就是两个List的总和。

[ManyToMany(typeof(Brotherhood), "ChildID1", "Siblings_DB_support", CascadeOperations = CascadeOperation.All)]
public List<Child> Siblings_DB { get; set; }
[ManyToMany(typeof(Brotherhood), "ChildID2", "Siblings_DB", CascadeOperations = CascadeOperation.All)]
public List<Child> Siblings_DB_support { get; set; }

和关系table:

public class Brotherhood
{
    [ForeignKey(typeof(Child))]
    public int ChildID1 { get; set; }

    [ForeignKey(typeof(Child))]
    public int ChildID2 { get; set; }
}

在这种情况下,您必须在 属性 属性中明确指定外键和反向关系。

public class TwitterUser {
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public string Name { get; set; }

    [ManyToMany(typeof(FollowerLeaderRelationshipTable), "LeaderId", "Followers",
        CascadeOperations = CascadeOperation.All)]
    public List<TwitterUser> FollowingUsers { get; set; }

    // ReadOnly is required because we're not specifying the followers manually, but want to obtain them from database
    [ManyToMany(typeof(FollowerLeaderRelationshipTable), "FollowerId", "FollowingUsers",
        CascadeOperations = CascadeOperation.CascadeRead, ReadOnly = true)]
    public List<TwitterUser> Followers { get; set; }
}

// Intermediate class, not used directly anywhere in the code, only in ManyToMany attributes and table creation
public class FollowerLeaderRelationshipTable {
    public int LeaderId { get; set; }
    public int FollowerId { get; set; }
}

如果您的关系是 "brother-brother" 而不是 "parent-children",则您必须将这两种关系相加,例如:

public class Person {
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public string Name { get; set; }

    [ManyToMany(typeof(BrothersRelationshipTable), "OldBrotherId", "YoungerBrothers",
        CascadeOperations = CascadeOperation.All)]
    public List<Person> OlderBrothers { get; set; }

    [ManyToMany(typeof(BrothersRelationshipTable), "YoungBrotherId", "OlderBrothers",
        CascadeOperations = CascadeOperation.CascadeRead, ReadOnly = true)]
    public List<Brothers> YoungerBrothers { get; set; }

    [Ignore]
    publc List<TwitterUser> Brothers { 
        get { return YoungerBrothers.Concat(OlderBrothers).ToList() }
    }
}

// Intermediate class, not used directly anywhere in the code, only in ManyToMany attributes and table creation
public class BrothersRelationshipTable {
    public int OldBrotherId { get; set; }
    public int YoungBrotherId { get; set; }
}