我正在使用 SQLite-Net PCL 和 SQLite-Net 扩展来开发使用 Xamarin 的应用程序。
我在两个类之间有一对多的关系A
,B
定义如下:
public class A
{
[PrimaryKey, AutoIncrement]
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<B> Sons
{
get;
set;
}
public A()
{
}
public A(string name, List<B> sons)
{
Name = name;
Sons = sons;
}
}
public class B
{
[PrimaryKey, AutoIncrement]
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
[ForeignKey(typeof(A))]
public int FatherId
{
get;
set;
}
[ManyToOne]
public A Father
{
get;
set;
}
public B()
{
}
public B(string name)
{
Name = name;
}
}
我想做的是A
从数据库中检索一个类型的对象,删除一个Sons
类型的对象B
并相应地更新数据库。这是我尝试过的:
var sons = new List<B>
{
new B("uno"),
new B("due"),
new B("tre"),
};
one = new A("padre", sons);
using (var conn = DatabaseStore.GetConnection())
{
conn.DeleteAll<A>();
conn.DeleteAll<B>();
conn.InsertWithChildren(one, true);
A retrieved = conn.GetWithChildren<A>(one.Id);
retrieved.Sons.RemoveAt(1);
}
using (var conn = DatabaseStore.GetConnection())
{
var retrieved = conn.GetWithChildren<A>(one.Id);
retrieved.Sons.RemoveAt(1); //"due"
//conn.UpdateWithChildren(retrieved);
conn.InsertOrReplaceWithChildren(retrieved, true);
}
问题是对象UpdateWithChildren
并InsertOrReplaceWithChildren
没有真正从数据库中删除,而只是外键为空。是否可以使其删除son
对象?