0

我正在使用 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;
    }

}

我已经定义了一个触发器,用于在插入时(以及稍后在更新时)自动设置 the和对象的LastModified字段。问题是,如果我使用,触发器不会被激活,而它发生在,即使显然在这种情况下我们没有递归插入。ABInsertWithChildrenInsert

复制问题的示例代码:

        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插入,正确更新列,而使用 插入的不会发生这种情况。我做错事了吗?InsertLastModifiedoneInsertWithChildren

4

0 回答 0