简体   繁体   English

SQLite-Net 扩展 - GetAllWithChildrenAsync 没有拉一切

[英]SQLite-Net Extensions - GetAllWithChildrenAsync not pulling everything

I am trying to use SQLite-Net Extensions to create a Relational Database.我正在尝试使用 SQLite-Net 扩展来创建关系数据库。 I'm running into an issue when trying to pull the Term object from the database.尝试从数据库中提取 Term object 时遇到问题。 It successfully pulls over its associated courses, but not the courses associated assessments and notes.它成功地提取了相关的课程,但没有提取与课程相关的评估和笔记。 I'm not sure if the problem lies in how I insert the objects into the database, how I pull the objects from the database, or how I have the objects attributes listed.我不确定问题是否在于我如何将对象插入数据库,如何从数据库中提取对象,或者我如何列出对象属性。

I feel like the SQLite-Net Extensions documentation is extremely limited, so I'm not even sure what's going on.我觉得 SQLite-Net Extensions 文档非常有限,所以我什至不确定发生了什么。 I've tried it many different ways, including adding CascadeOperations, but non of those seemed to help.我已经尝试了许多不同的方法,包括添加 CascadeOperations,但这些方法似乎都没有帮助。

Here is the (simplified) code for my objects:这是我的对象的(简化)代码:

[Table("Terms")]
public class Term
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public string Name { get; set; }
    [OneToMany]
    public List<Course> Courses { get; set; }

    public Term() { }
    public Term(string name, List<Course> courses)
    {
        Name = name;
        Courses = courses;
    }

Courses培训班

[Table("Courses")]
public class Course
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    [ForeignKey(typeof(Term))]
    public int TermID { get; set; }
    public string Name { get; set; }
    [OneToMany]
    public List<Assessment> Assessments { get; set; }
    [OneToMany]
    public List<Note> Notes { get; set; }

    public Course() { }

    public Course(string name, List<Assessment> assessments, List<Note> notes)
    {
        Name = name;
        Assessments = assessments;
        Notes = notes;
    }
}

Assessments评估

[Table("Assessments")]
public class Assessment
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    [ForeignKey(typeof(Course))]
    public int CourseID { get; set; }
    public string Name { get; set; }

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

Notes笔记

[Table("Notes")]
public class Note
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    [ForeignKey(typeof(Course))]
    public int CourseID { get; set; }
    public string Name { get; set; }
    public string Text { get; set; }

    public Note() { }
    public Note(string name, string note)
    {
        Name = name;
        Text = note;
    }
}

And here is the code for inserting and getting objects: Inserting这是插入和获取对象的代码:插入

public bool SaveTermAsync(Term term)
    {
        if (term.ID != 0)
        {
            _database.UpdateWithChildrenAsync(term);
            return true;
        }
        else
        {
            foreach (var course in term.Courses)
            {
                foreach (var assessment in course.Assessments)
                {
                    _database.InsertAsync(assessment);
                }
                foreach (var note in course.Notes)
                {
                    _database.InsertAsync(note);
                }
                _database.InsertAsync(course);
            }
            _database.InsertAsync(term);
            _database.UpdateWithChildrenAsync(term);
            return false;
        }
    }

Getting得到

public Task<List<Term>> GetTermsAsync()
    {
        return _database.GetAllWithChildrenAsync<Term>();
    }

I know it's a bit of a code dump, but I have no idea where or what could be going wrong.我知道这有点像代码转储,但我不知道哪里或哪里出了问题。 If anyone could give any information about what is potentially going wrong, that would be awesome.如果有人可以提供任何有关可能出现问题的信息,那就太好了。 Perhaps I'm simply expecting something to happen that isn't actually how it works.也许我只是在期待发生的事情实际上并不是它的工作方式。 I don't know.我不知道。

Also, if anyone has any links to some better documentation than https://bitbucket.org/twincoders/sqlite-net-extensions/src/master/ that would be awesome此外,如果有人有任何比https://bitbucket.org/twincoders/sqlite-net-extensions/src/master/更好的文档的链接,那就太棒了

EDIT编辑

I tried using Cascading Options as well, CascadeRead, CascadeInsert, and CascadeAll.我也尝试使用 Cascading Options、CascadeRead、CascadeInsert 和 CascadeAll。 Using CascadeInsert or CascadeAll with _database.InsertWithChildrenAsync(term, true) resulted in a crash.将 CascadeInsert 或 CascadeAll 与 _database.InsertWithChildrenAsync(term, true) 一起使用会导致崩溃。 The crash does not provide any error messages, and even wrapping the InsertWithChildren with a try catch block didn't work.崩溃没有提供任何错误消息,甚至用 try catch 块包装 InsertWithChildren 也不起作用。 Removing the recursive bool caused the program not to crash, and actually get the closest to what I'm looking for.删除递归 bool 导致程序不会崩溃,并且实际上最接近我正在寻找的内容。 Assessments and Notes are no longer null, but are still empty.评估和笔记不再是 null,但仍然是空的。 Here's my updated code:这是我更新的代码:

Saving and Getting:保存和获取:

public async Task<List<Term>> GetTermsAsync()
    {
        return await _database.GetAllWithChildrenAsync<Term>(recursive: true);
    }

public async void SaveTermAsync(Term term)
    {
        if (term.ID != 0)
        {
            await _database.UpdateWithChildrenAsync(term);
        }
        else
        {
            //Trying this with recursion results in crash
            await _database.InsertWithChildrenAsync(term);
        }
    }

One-To-Many Relationships:一对多关系:

//In Term
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<Course> Courses { get; set; }

//In Courses
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<Assessment> Assessments { get; set; }
[OneToMany(CascadeOperations = CascadeOperation.All)]
public List<Note> Notes { get; set; }

Also, I forgot to include last time how I'm populating the tables in the first place.另外,我忘了包括上次我是如何填充表格的。

public bool CreateTables()
    {
        _database.CreateTableAsync<Term>().Wait();
        _database.CreateTableAsync<Course>().Wait();
        _database.CreateTableAsync<Assessment>().Wait();
        _database.CreateTableAsync<Note>().Wait();
        return true;
    }

    public Task<int> ClearTablesTest()
    {
        _database.DropTableAsync<Term>();
        _database.DropTableAsync<Course>();
        _database.DropTableAsync<Assessment>();
        return _database.DropTableAsync<Note>();
    }

async public Task<int> PopulateTestData()
    {
        await ClearTablesTest();
        CreateTables();

        Term term = new Term("Test Term", true, DateTime.Now, DateTime.Now.AddDays(10),
            new List<Course>
            {
                new Course("Test Course", CourseStatus.Completed, "Guys Name", "(999)-999-9999", "email@gmail.com", 6, DateTime.Now, DateTime.Now.AddDays(10),
                new List<Assessment>
                {
                    new Assessment("Test Assessment", AssessmentType.Objective, false, DateTime.Now, DateTime.Now.AddDays(10))
                },
                new List<Note>
                {
                    new Note("Test Note", "This is a test note.")
                })
            });
        App.Database.SaveTermAsync(term);
        return 0;
    }

I finally figured out what was causing the crash as well as causing general confusion within SQLite-Net Extensions.我终于弄清楚了导致崩溃的原因以及在 SQLite-Net Extensions 中造成普遍混乱的原因。

In my Assessment class, the property在我的评估 class 中,该属性

public string BackgroundColor
    {
        get { return IsComplete ? "#558f45" : "Gray"; }
        set { BackgroundColor = value; }
    }

was causing the crash when recursion was used.使用递归时导致崩溃。 I've been scouring the web for over two weeks looking for solutions to this issue, but haven't found anything similar to this.我已经搜索 web 两个多星期来寻找解决这个问题的方法,但没有找到类似的东西。 I submitted a bug report on the SQLite-Net Extensions bitbucket.我提交了一份关于 SQLite-Net Extensions bitbucket 的错误报告。

If anyone knows why this specific line would cause issues, I'd love to hear your input.如果有人知道为什么这条特定线路会导致问题,我很想听听您的意见。 Until then I'm going to mark this question as answered and continue work on my app.在此之前,我将把这个问题标记为已回答并继续在我的应用程序上工作。

Thanks @redent84 for your help thus far on this issue.感谢 @redent84 迄今为止在此问题上提供的帮助。

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

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