我有以下课程:
var objects = {};
objects.first = function(){
console.log("First one is executed");
}
objects.second = function(){
console.log("second one is executed");
}
objects.third = function(){
console.log("third one is executed");
}
当我有一个A实例,子实例为B时,执行class A
{
[PrimaryKey]
public int A_ID { get; set; }
[OneToMany(CascadeOperations=CascadeOperation.All)]
public List<B> Children { get; set; }
}
class B
{
[PrimaryKey]
public int B_ID { get; set; }
[ForeignKey(typeof(C))]
public int C_ID { get; set; }
}
class C
{
[PrimaryKey]
public int C_ID { get; set; }
}
确实会导致A和它的子B被插入到数据库中,但是每个外键值db.InsertOrReplaceWithChildren(aInstance, recursive: true);
始终是默认值0,尽管它们具有值。
示例:
B.C_ID
当我选择记录B时:
A aInstance = new A();
aInstance.A_ID = 1;
B bInstance = new B();
bInstance.B_ID = 1;
C cInstance = new C();
cInstance.C_ID = 1;
bInstance.C_ID = cInstance.C_ID; //assigning the many-to-one foreign key of B to C
aInstance.Children.Add(bInstance); //assigning the one-to-many child record of A to B
//do the insert
db.InsertOrReplaceWithChildren(aInstance, recursive:true);
答案 0 :(得分:1)
我遇到了这个问题,它是通过在B类中识别C类对象(作为外键对象)并将关系标记为ReadOnly = true来解决的。 所以代码应该是
class A
{
[PrimaryKey]
public int A_ID { get; set; }
[OneToMany(CascadeOperations=CascadeOperation.All)]
public List<B> Children { get; set; }
}
class B
{
[PrimaryKey]
public int B_ID { get; set; }
[OneToOne(ReadOnly=true)]
public C C_Obj { get; set; }
[ForeignKey(typeof(C))]
public int C_ID { get; set; }
}
class C
{
[PrimaryKey]
public int C_ID { get; set; }
}
我希望这个解决方案适合你:)