我正在使用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
1}}留到0.
任何的想法?我做错了什么?
亚历
答案 0 :(得分:0)
数据库架构必须首先与您尝试执行的操作兼容。 每个关系的一面必须是一个Key(唯一)属性或一组属性,所以不能让你自己拥有多对多的自我关系。但是如果您创建另一个表来保存相关的行,并且组合键包含来自您尝试创建关系的一个表中的键属性[s]的两个副本,那么您可以这样做。
表格密钥为myPK
,然后创建第二个名为myM2M
的表,其中包含一个包含属性myPK1
和myPK2
的两列主键,形成这个新表的pk,然后在第一个表中用myPK
列形成这两个列之间的关系。
你看过this吗?
答案 1 :(得分:0)
经过一些尝试,我设法实现了多对多的自我关系,我对同一个班级中有两个很多关系感到有些困惑:我想知道哪个是&#34;官方&#34;一。最终我明白两者都是!而兄弟姐妹的总和是两个列表的总和。
[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; }
}
答案 2 :(得分: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; }
}