使用SQLite.Net.Async Extensions PCL 1.3.0在同一对象上有多个@OneTONE?
是否可以使用SQLite.Net.Async Extensions PCL 1.3.0在一个实体内拥有多个OneTONE关系 例如:使用SQLite.Net.Async Extensions PCL 1.3.0在同一对象上有多个@OneTONE?,sqlite,xamarin,sqlite-net,sqlite-net-extensions,Sqlite,Xamarin,Sqlite Net,Sqlite Net Extensions,是否可以使用SQLite.Net.Async Extensions PCL 1.3.0在一个实体内拥有多个OneTONE关系 例如: [Table("body")] public class Body { [OneToOne(CascadeOperations = CascadeOperation.All)] [Column ("left")] public Hand Left { get; set; } [OneToOne(CascadeOperations
[Table("body")]
public class Body
{
[OneToOne(CascadeOperations = CascadeOperation.All)]
[Column ("left")]
public Hand Left { get; set; }
[OneToOne(CascadeOperations = CascadeOperation.All)]
[Column ("right")]
public Hand Right { get; set; }
}
[Table("hand")]
public class Hand
{
// In this I do not need a reference back to Body.
}
我一直在尝试使用以下答案:
这些网站也为我们提供了灵感:
不幸的是,到目前为止没有运气。甚至可能吗?是的,这是完全可能的,不幸的是,您不能依赖自动外键和反向关系发现,因此您需要手动指定它 例如,对于同一类中声明为类似的
int
主键和外键:
public class Body
{
[OneToOne(foreignKey: "LeftId", CascadeOperations = CascadeOperation.All)]
public Hand Left { get; set; }
[OneToOne(foreignKey: "RightId", CascadeOperations = CascadeOperation.All)]
public Hand Right { get; set; }
// Foreign key for Left.Id
public int LeftId { get; set; }
// Foreign key for Right.Id
public int RightId { get; set; }
}
public class Hand
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
}
如果外键是在Hand
对象中声明的,则属性属性是等效的:
public class Body
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[OneToOne(foreignKey: "LeftId", CascadeOperations = CascadeOperation.All)]
public Hand Left { get; set; }
[OneToOne(foreignKey: "RightId", CascadeOperations = CascadeOperation.All)]
public Hand Right { get; set; }
}
public class Hand
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
// Foreign key for Body.Id where this object is Left
public int LeftId { get; set; }
// Foreign key for Body.Id where this object is Right
public int RightId { get; set; }
}
如果需要,必须在两端的OneToOne
属性的inverseProperty
键中指定反向属性:
public class Body
{
// Skipping foreign keys and primary key
[OneToOne(foreignKey: "LeftId", inverseProperty: "LeftBody", CascadeOperations = CascadeOperation.All)]
public Hand Left { get; set; }
[OneToOne(foreignKey: "RightId", inverseProperty: "RightBody", CascadeOperations = CascadeOperation.All)]
public Hand Right { get; set; }
}
public class Hand
{
// Skipping foreign keys and primary key
[OneToOne(foreignKey: "LeftId", inverseProperty: "Left", CascadeOperations = CascadeOperation.All)]
public Body LeftBody { get; set; }
[OneToOne(foreignKey: "RightId", inverseProperty: "Right", CascadeOperations = CascadeOperation.All)]
public Body RightBody { get; set; }
}
谢谢@redent84,我终于用你的例子成功了。在性能方面,这两种方法是否有任何不同之处,即在我的情况下,是否更倾向于将外键保留在身体或手上?实际上,我只需要主体知道两只手,我计划递归地查询它们,所以人们应该认为它更倾向于将外键保存在主体内-另外一个问题是,什么时候应该查询上述结构,然后在GetAllWithChildrenAsync中设置递归标志。是否可以指定要获取的对象或深度,即获取的深度?从性能角度来看,通过主键访问对象总是更好,因此,如果要从
Body
递归访问Hands
,最好将外键保留在Body
中。在这种特殊情况下,它也更有意义。对于递归,如果不需要,可以将级联操作更改为更保守的操作。目前无法指定运行时的深度,它仅基于关系属性的CascadeOperations
属性。“不幸的是,您不能依赖自动外键和反向关系发现”-就个人而言,我更喜欢这样,不仅是为了性能,但是为了减少猜测和依靠框架来尝试基于字符串的假设!