I am using SQLite-Net PCL together with SQLite-Net extensions for the development of an application using Xamarin.
I have two classes A
and B
defined as follows:
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;
}
}
I am trying to use insert or replace in the following way:
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);
}
The problem is that the second InsertOrReplaceWithChildren
is throwing a Constraint
exception, that is not thrown if we remove the unique constraint on A.Name
. Isn't InsertOrReplaceWithChildren
supposed to replace the object if a unique constraint is violated?