是的,这完全有可能,不幸的是你不能依赖自动外键和反向关系发现,所以你需要手动指定它。
例如,对于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
object 中声明,则属性属性是等效的:
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; }
}
而逆属性,如果需要,必须在两端属性的inverseProperty
key中指定: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; }
}