簡體   English   中英

SQLite-Net 擴展 - GetAllWithChildrenAsync 沒有拉一切

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

我正在嘗試使用 SQLite-Net 擴展來創建關系數據庫。 嘗試從數據庫中提取 Term object 時遇到問題。 它成功地提取了相關的課程,但沒有提取與課程相關的評估和筆記。 我不確定問題是否在於我如何將對象插入數據庫,如何從數據庫中提取對象,或者我如何列出對象屬性。

我覺得 SQLite-Net Extensions 文檔非常有限,所以我什至不確定發生了什么。 我已經嘗試了許多不同的方法,包括添加 CascadeOperations,但這些方法似乎都沒有幫助。

這是我的對象的(簡化)代碼:

[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;
    }

培訓班

[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;
    }
}

評估

[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;
    }
}

筆記

[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;
    }
}

這是插入和獲取對象的代碼:插入

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;
        }
    }

得到

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

我知道這有點像代碼轉儲,但我不知道哪里或哪里出了問題。 如果有人可以提供任何有關可能出現問題的信息,那就太好了。 也許我只是在期待發生的事情實際上並不是它的工作方式。 我不知道。

此外,如果有人有任何比https://bitbucket.org/twincoders/sqlite-net-extensions/src/master/更好的文檔的鏈接,那就太棒了

編輯

我也嘗試使用 Cascading Options、CascadeRead、CascadeInsert 和 CascadeAll。 將 CascadeInsert 或 CascadeAll 與 _database.InsertWithChildrenAsync(term, true) 一起使用會導致崩潰。 崩潰沒有提供任何錯誤消息,甚至用 try catch 塊包裝 InsertWithChildren 也不起作用。 刪除遞歸 bool 導致程序不會崩潰,並且實際上最接近我正在尋找的內容。 評估和筆記不再是 null,但仍然是空的。 這是我更新的代碼:

保存和獲取:

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);
        }
    }

一對多關系:

//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; }

另外,我忘了包括上次我是如何填充表格的。

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;
    }

我終於弄清楚了導致崩潰的原因以及在 SQLite-Net Extensions 中造成普遍混亂的原因。

在我的評估 class 中,該屬性

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

使用遞歸時導致崩潰。 我已經搜索 web 兩個多星期來尋找解決這個問題的方法,但沒有找到類似的東西。 我提交了一份關於 SQLite-Net Extensions bitbucket 的錯誤報告。

如果有人知道為什么這條特定線路會導致問題,我很想聽聽您的意見。 在此之前,我將把這個問題標記為已回答並繼續在我的應用程序上工作。

感謝 @redent84 迄今為止在此問題上提供的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM