在这种情况下,您必须在属性属性中明确指定外键和反向关系。
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; }
}