与InsertOrReplaceWithChildren的SQLite.Net Extensions问题

时间:2015-03-18 14:58:06

标签: sqlite xamarin sqlite-net sqlite-net-extensions

我正在使用SQLite-Net PCL和SQLite-Net扩展来开发使用Xamarin的应用程序。

我有两个类AB定义如下:

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应该替换对象?

1 个答案:

答案 0 :(得分:1)

我检查了该行的提交,现在我记得:似乎AutoIncrement主键仅适用于Insert语句,而不适用于InsertOrReplace。因此,如果在InsertOrReplace主键设置为0的情况下调用AutoIncrement,它将始终使用ID 0覆盖数据库中的相同值,而不是正确添加新值。

通过执行此测试很容易检查此问题:

var a = new List<A> {
    new A("a1", null),
    new A("a2", null),
    new A("a3", null),
    new A("a4", null),
    new A("a5", null),
    new A("a6", null)
};

foreach (var element in a) {
    conn.InsertOrReplace(element);
}

// Will fail expecting 6 elements but returning only 1
Assert.AreEqual(a.Count, conn.Table<A>().ToList().Count);

要解决此问题,您必须放弃AutoIncrement主键或Unique约束检查才能使InsertOrReplace语句生效。

这些替换类应该按预期工作,放弃AutoIncrement主键:

public class A
{

    [PrimaryKey]
    public Guid Id { get; set; }

    [Unique]
    public string Name { get; set; }

    [OneToMany(CascadeOperations = CascadeOperation.All)]
    public List<B> Sons { get; set; }

    public A() {}
    public A(string name, List<B> sons)
    {
        Id = Guid.NewGuid();
        Name = name;
        Sons = sons;
    }
}

public class B
{

    [PrimaryKey]
    public Guid Id { get; set; }

    [Unique]
    public string Name { get; set; }

    public string LastModified { get; set; }

    [ForeignKey(typeof(A))]
    public Guid FatherId { get; set; }

    [ManyToOne]
    public A Father { get; set; }

    public B() {}

    public B(string name)
    {
        Id = Guid.NewGuid();
        Name = name;
    }
}

希望它有所帮助。

相关问题