【发布时间】:2015-03-18 14:58:05
【问题描述】:
我正在使用 SQLite-Net PCL 和 SQLite-Net 扩展来开发使用 Xamarin 的应用程序。
我有两个类 A 和 B 定义如下:
public class A
{
[PrimaryKey, AutoIncrement]
public int Id
{
get;
set;
}
[Unique]
public string Name
{
get;
set;
}
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<B> Sons
{
get;
set;
}
public A(string name, List<B> sons)
{
Name = name;
Sons = sons;
}
}
public class B
{
[PrimaryKey, AutoIncrement]
public int Id
{
get;
set;
}
[Unique]
public string Name
{
get;
set;
}
public string LastModified
{
get;
set;
}
[ForeignKey(typeof(A))]
public int FatherId
{
get;
set;
}
[ManyToOne]
public A Father
{
get;
set;
}
public B()
{
}
public B(string name)
{
Name = name;
}
}
我正在尝试通过以下方式使用插入或替换:
var sons1 = new List<B>
{
new B("uno"),
};
var a1 = new A("padre", sons1);
var sons2 = new List<B>
{
new B("uno2"),
};
var a2 = new A("padre", sons2);
using (var conn = DatabaseStore.GetConnection())
{
conn.DeleteAll<A>();
conn.DeleteAll<B>();
}
using (var conn = DatabaseStore.GetConnection())
{
conn.InsertOrReplaceWithChildren(a1, true);
conn.InsertOrReplaceWithChildren(a2, true);
}
问题是第二个InsertOrReplaceWithChildren 抛出了Constraint 异常,如果我们移除A.Name 上的唯一约束,则不会抛出该异常。如果违反唯一约束,InsertOrReplaceWithChildren 不应该替换对象吗?
【问题讨论】:
-
听起来像一个错误。大概在这里:bitbucket.org/twincoders/sqlite-net-extensions/src/…
标签: sqlite xamarin sqlite-net sqlite-net-extensions