0


我正在使用 SQLite.net 和SQLite-Net Extensions。我想创建一个多对多的自我关系。
就我而言,我有一个带有一些兄弟姐妹的子班,当然兄弟姐妹仍然是儿童类型。因此我尝试实现多对多关系:

子类

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

兄弟会班

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)仅使用ID_child1更新并ID_child2保持为 0。
知道吗?我究竟做错了什么?
亚历克斯

4

3 回答 3

0

数据库模式必须首先与您尝试执行的操作兼容。每个关系的一侧必须是一个键(唯一)属性或一组属性,所以不,你不能自己拥有多对多的自我关系。但是,如果您创建另一个表来保存相关行,并且组合键包含您尝试在其上创建关系的一个表中的键属性 [s] 的两个副本,那么您可以这样做。

假设表键是myPK,然后创建第二个表,命名为myM2M包含属性的双列主键myPK1和,它们共同构成这个新表的 pk,然后在这两个列之间与第一个列中的myPK2列形成关系myPK桌子。

你看过这个吗?

于 2017-02-17T14:50:31.933 回答
0

经过一些尝试,我设法实现了多对多自我关系,我对在同一个类中有两个多对多关系有点困惑:我想知道哪个是“官方”的。终于我明白了,两者都是!兄弟姐妹的总和是两个列表的总和。

[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; }

以及关系表:

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

    [ForeignKey(typeof(Child))]
    public int ChildID2 { get; set; }
}
于 2017-02-20T11:32:43.930 回答
0

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

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; }
}

如果您的关系是“兄弟”而不是“父子”,则必须将两种关系相加,例如:

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; }
}
于 2017-02-24T14:26:31.000 回答