【发布时间】:2017-07-03 06:20:57
【问题描述】:
我有以下课程:
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);
确实会导致 A 及其子 B 插入到数据库中,但是每个外键值 B.C_ID 始终是默认值 0,尽管它们具有值。
示例:
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);
当我选择记录 B 时:
B bDatabaseRecord = db.Get<B>(bRecord => bRecord.B_ID == bInstance.B_ID).First();
//bDatabaseRecord.C_ID is 0 <-- the problem
【问题讨论】:
标签: c# sqlite xamarin sqlite-net