SQLite.Net Extensions触发器未通过递归插入激活

时间:2015-03-17 12:08:54

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

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

我在两个类AB之间建立了一对多关系,定义如下:

   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(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(string name)
    {
        Name = name;
    }

}

我已经定义了一个触发器,用于在插入时自动设置LastModifiedA对象的B字段(稍后还会更新)。问题是,如果我使用InsertWithChildren,触发器不会被激活,而Insert则会发生,即使在这种情况下我们也没有递归插入。

复制问题的示例代码:

        var sons1 = new List<B>
        {
            new B("uno"),
            new B("due"),
            new B("tre"),
        };

        one = new A("padre", sons1);

        var sons2 = new List<B>
        {
            new B("uno2"),
            new B("due2"),
            new B("tre2"),
        };

        two = new A("padre2", sons2);

        using (var conn = DatabaseStore.GetConnection())
        {
            conn.DeleteAll<A>();
            conn.DeleteAll<B>();

            string query = @"CREATE TRIGGER  {0}_log AFTER INSERT ON {0} " +
                           "BEGIN " +
                           "UPDATE {0} SET LastModified = datetime('now') " +
                           "WHERE Id = NEW.Id; " +
                           "END;";
            conn.Execute(String.Format(query, "A"));
            conn.Execute(String.Format(query, "B"));
        }

        using (var conn = DatabaseStore.GetConnection())
        {
            conn.InsertWithChildren(one, true);
            conn.Insert(two);
        }

在此示例two中,使用Insert方法插入,可以正确更新LastModified列,而one不会发生这种情况,InsertWithChildren插入{ {1}}。我是以错误的方式做某事吗?

0 个答案:

没有答案