简体   繁体   English

SQLite-Net Extensions如何正确地递归更新对象

[英]SQLite-Net Extensions how to correctly update object recursively

I am using SQLite-Net PCL together with SQLite-Net extensions for the development of an application using Xamarin. 我正在使用SQLite-Net PCL和SQLite-Net扩展来使用Xamarin开发应用程序。

I have a one to many relationship between two classes A and B defined as follows: 我在两个类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()
    {
    }

    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()
    {
    }

    public B(string name)
    {
        Name = name;
    }

}

What I would like to do, is to retrieve an object of type A from the database, remove one of the Sons objects of type B and update the database accordingly. 我想做的是从数据库中检索类型为A的对象,删除类型为BSons对象之一,并相应地更新数据库。 This is what I have tried: 这是我尝试过的:

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

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

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

            conn.InsertWithChildren(one, true);

            A retrieved = conn.GetWithChildren<A>(one.Id);
            retrieved.Sons.RemoveAt(1);
        }

        using (var conn = DatabaseStore.GetConnection())
        {
            var retrieved = conn.GetWithChildren<A>(one.Id);
            retrieved.Sons.RemoveAt(1); //"due"

            //conn.UpdateWithChildren(retrieved);
            conn.InsertOrReplaceWithChildren(retrieved, true);
        }

The problem is that both with UpdateWithChildren and with InsertOrReplaceWithChildren the the object is not really removed from the database, but only it's foreign key nulled. 问题在于,无论使用UpdateWithChildren还是使用InsertOrReplaceWithChildren ,该对象都不会真正从数据库中删除,而只是将其外键置为空。 Is it possible to make it delete the son object? 是否可以删除son对象?

You're not really trying to delete any object at all. 您实际上并没有尝试删除任何对象。 You're just removing the relationship between the two objects, but nothing stops you from having more objects related to any of them, so deleting any is not correct as you may break other relationships. 您只是删除了两个对象之间的关系,但是没有什么阻止您拥有更多与它们相关的对象,因此删除任何对象都是不正确的,因为您可能会破坏其他关系。

It should be more like this: 应该更像这样:

using (var conn = DatabaseStore.GetConnection())
{
    var retrieved = conn.GetWithChildren<A>(one.Id);
    var due = retrieved.Sons[1];

    // This is not required if the foreign key is in the other end,
    // but it would be the usual way for any other scenario
    // retrieved.Sons.Remove(due);
    // conn.UpdateWithChildren(retrieved);

    // Then delete the object if it's no longer required to exist in the database
    conn.delete(due);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM