简体   繁体   English

SQLite-Net-Extensions GetWithChildren()方法仅到达第一个子层

[英]SQLite-Net-Extensions GetWithChildren() Method only reaches first Child Layer

currently I am working with the NuGet SQLite-Net-Extensions in Xamarin Forms and I have encountered a problem for which I can't find a solution. 目前我正在使用Xamarin Forms中的NuGet SQLite-Net-Extensions,我遇到了一个无法找到解决方案的问题。

The Problem: When calling GetWithChildren(primaryKey, recursive: true), the returned object only contains the first child layer. 问题:当调用GetWithChildren(primaryKey,recursive:true)时,返回的对象只包含第一个子图层。 An example can be seen in the following. 以下是一个例子。

The database is built up like this: 数据库的构建如下: ER-型号

The equivalent code to this model is provided in the following: 以下提供了此模型的等效代码:

User 用户

namespace DatabaseTest
{
[Table("Users")]
public class User
{
    [PrimaryKey,AutoIncrement]
    public int Id { get; set; }

    public string Name { get; set; }

    [OneToMany]
    public List<Contact> Contacts { get; set; }
}
}

Contact 联系

namespace DatabaseTest
{
[Table("Contacts")]
public class Contact
{
    [PrimaryKey,AutoIncrement]
    public int Id { get; set; }

    public string Name { get; set; }

    [OneToMany]
    public List<Entry> Entries { get; set; }

    [ForeignKey(typeof(User))]
    public int UserID { get; set; }

    [ManyToOne]
    public User User { get; set; }
}
}

Entry 条目

namespace DatabaseTest
{
[Table("Entries")]
public class Entry
{
    [PrimaryKey,AutoIncrement]
    public int Id { get; set; }

    public float Amount { get; set; }

    [ForeignKey(typeof(Contact))]
    public int ContactId { get; set; }

    [ManyToOne]
    public Contact Contact { get; set; }
}
}

In my App.cs I am creating the database and use the CreateTable() Method for all three classes. 在App.cs中,我正在创建数据库,并对所有三个类都使用CreateTable()方法。 For the sake of this example, in MainPage.xaml.cs there is simply a button, which has a ButtonClicked Method. 为了这个例子,在MainPage.xaml.cs中只有一个按钮,它有一个ButtonClicked方法。

In the real Application a process could look like this: 在真正的应用程序中,进程可能如下所示:

User logs in --> Adds Contact --> At some Point User creates Entry to one of his contacts 用户登录->添加联系人->在某个点,用户创建了一个联系人的条目

To accomplish this procedure in my example, the ButtonClicked Method looks like this: 要在我的示例中完成此过程,ButtonClicked方法如下所示:

void ButtonClicked(object sender, EventArgs e)
    {
        try
        {
            User user = new User()
            {
                Name = "Test user"
            };
            Contact contact = new Contact()
            {
                Name = "First contact"
            };
            Entry entry1 = new Entry()
            {
                Amount = 10F
            };
            Entry entry2 = new Entry()
            {
                Amount = 20F
            };
            App.Database.Insert(user);
            if (user.Contacts==null)
            {
                user.Contacts = new List<Contact>();
            }
            App.Database.Insert(contact);
            user.Contacts.Add(contact);
            App.Database.UpdateWithChildren(user);
            if (contact.Entries==null)
            {
                contact.Entries = new List<Entry>();
            }
            App.Database.Insert(entry1);
            App.Database.Insert(entry2);
            contact.Entries.Add(entry1);
            contact.Entries.Add(entry2);
            App.Database.UpdateWithChildren(contact);
            App.Database.UpdateWithChildren(user);
            var test = App.Database.GetWithChildren<User>(user.Id, recursive: true);
            var test2 = App.Database.GetAllWithChildren<Contact>();
            var test3 = App.Database.GetAllWithChildren<Entry>();
        } catch (Exception ex)
        {
            Debug.Print(ex.Message);
        }
    }

I have set a breakpoint to the bracket that closes the try to inspect the result. 我已经在括号中设置了一个断点,以关闭检查结果的尝试。 In the End, my user looks like this: 在最后,我的用户看起来像这样:

用户

Which is absolutely perfect. 这绝对是完美的。

However, when I try to get this user from my database, the result looks like this: 但是,当我尝试从数据库中获取该用户时,结果如下所示:

在此输入图像描述

I don't know how to resolve this error and hope anyone can help me with this problem. 我不知道如何解决此错误,希望有人可以帮助我解决这个问题。 Since this post is very long already, I thank everyone who read this far in advance. 由于这篇文章已经很长了,我感谢所有提前阅读本文的人。

After many tries I finally solved my problem on my own. 经过多次尝试,我终于自己解决了问题。

在此输入图像描述

Solution: 解:

It could not have been any easier. 这可不容易。 In my User, Contact and Entry Class I provided my OneToMany and ManyToOne attributes with the CascadeOperation attribute. 在我的User,Contact和Entry Class中,我使用CascadeOperation属性提供了OneToMany和ManyToOne属性。 Example: 例:

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

[ManyToOne(CascadeOperations = CascadeOperation.All)]
    public User User { get; set; }

Even though I marked my GetWithChildren() Method as recursive: true, only by applying CascadeOperations it will work properly. 即使我将GetWithChildren()方法标记为递归:true,只有通过应用CascadeOperations它才能正常工作。 More information about SQLite-Net-Extensions and CascadeOperations can be found here: 有关SQLite-Net-Extensions和CascadeOperations的更多信息,请访问:

Source: TwinCoders SQLite-Net-Extensions 来源:TwinCoders SQLite-Net-Extensions

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

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