dyn*_*kaj 4 sqlite xamarin sqlite-net sqlite-net-extensions
是否可以使用 SQLite.Net.Async Extensions PCL 1.3.0 在一个实体中拥有多个 OneToOne 关系?
例子:
[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.
}
Run Code Online (Sandbox Code Playgroud)
我一直在尝试使用以下答案:
并以这些网站为灵感:
https://bitbucket.org/twincoders/sqlite-net-extensions/overview
不幸的是,到目前为止还没有运气。甚至有可能吗?
是的,这是完全可能的,不幸的是您不能依赖自动外键和反向关系发现,因此您需要手动指定它。
例如,对于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; }
}
Run Code Online (Sandbox Code Playgroud)
如果您的外键在Handobject中声明,则属性属性是等效的:
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; }
}
Run Code Online (Sandbox Code Playgroud)
并且如果需要,必须在两端inverseProperty的OneToOne属性的键中指定反向属性:
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; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
941 次 |
| 最近记录: |