0

I am using sqlite-net-pcl with the SqLiteNetExtensions for a foreign key. I have this two tables ( not sure if they are written correctly) an agenda can contain many Tasks, my question is how do i write the CRUD methods to add/delete/load my tasks in my agenda, i am not sure how this work when you have a foreign key ?

I tried to understand the documentation : https://bitbucket.org/twincoders/sqlite-net-extensions/src/master/

Without any success, what i don't get is that the WithChildren methods doesn't seem to be recognized unless the database = new SQLiteAsyncConnection(dbPath); doesn't work for the SqliteNetExtensions. Note that the CRUD methods for my agenda is working but i am trying to implement the CRUD methods for the tasks. Also in my agenda method called GetAgendaAsync() i am not sure if this will load the data with the tasks already or i have to do something to add the tasks to it when loaded ?

Thank you, i am kinda lost with this.

Agenda.cs in the Models folder

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using SQLite;
using Calculette.Database;
using SQLiteNetExtensions.Attributes;
    [Table("Agenda")]
    public class Agenda
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public string Topic { get; set; }
        public string Duration { get; set; } 
        public DateTime Date { get; set; }
        [OneToMany(CascadeOperations = CascadeOperation.All)]
}

Tasks.cs in Models folder

using System;
using System.Collections.Generic;
using System.Text;
using SQLite;
using SQLiteNetExtensions.Attributes;
[Table("Task")]
public class Tasks
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    
    [ForeignKey(typeof(Agenda))]
    public string Name { get; set; }
    public string Time { get; set; }
    [ManyToOne]
    public Agenda Agenda { get; set; }
    
}

AgendaDatabase.cs in the Database folder ( all my operation currently for the Agenda table)

using System;
using System.Collections.Generic;
using System.Text;
using SQLite;
using Calculette.Models;
using System.Threading.Tasks;
using Calculette.ViewModel;
using SQLiteNetExtensions.Attributes;

public class AgendaDatabase
{
    
    readonly SQLiteAsyncConnection database;

    public AgendaDatabase(string dbPath)
    {
       
        database = new SQLiteAsyncConnection(dbPath);
        database.CreateTableAsync<Agenda>().Wait();
database.CreateTableAsync<Tasks>().Wait();
    


    }

    // Get all agenda
    public Task<List<Agenda>> GetAgendasAsync()
    {

        return database.Table<Agenda>().ToListAsync();
    }

    // Get specific agenda
    public Task<Agenda> GetAgendaAsync(int id)
    {
        return database.Table<Agenda>()
                        .Where(i => i.ID == id)
                        .FirstOrDefaultAsync();
    }

    // Insert new agenda (save)
    public Task<int> SaveAgendaAsync(Agenda agenda)
    {
        if (agenda.ID != 0)
        {
            return database.UpdateAsync(agenda);
        }
        else
        {
            return database.InsertAsync(agenda);
        }
    }
    
    //Delete specific agenda
    public Task<int> DeleteAgendaAsync(Agenda agenda)
    {
        return database.DeleteAsync(agenda);
    }
    public Task<int> AddAgendaAsync(Agenda agenda)
    {
        return database.InsertAsync(agenda);
    }
}
codejourney
  • 319
  • 4
  • 15
  • Make sure to be using the Async version of SQLiteNetExtensions and importing the async namespace. Otherwise the extensions methods will not work because they are extending the standard synchronous version. – redent84 Jun 29 '20 at 18:12
  • what what is that namespace ? im not even sure it's written in the doc haha. Also would you mind helping out with a code example, im new to this and it's hard to understand with just words haha thanks ! – codejourney Jun 29 '20 at 18:21
  • 1
    Sure, there are some code samples in the test folder of the project: https://bitbucket.org/twincoders/sqlite-net-extensions/src/master/Tests/IntegrationTests/Tests/RecursiveReadAsyncTests.cs Or check other answers in this very same site like this one: https://stackoverflow.com/a/37811309/469218 – redent84 Jun 29 '20 at 19:05
  • Ok thanks adding this : using SQLiteNetExtensionsAsync.Extensions; made me able to use WithChildren. Now correct me if i am wrong but to intialize both of my tables i would leave the GetAgendasAsync() method up there ( it gets all the agendas), then i would create an other method to get all the taks but like that ? public Task> GetAllTasks() { return database.GetAllWithChildrenAsync(); } Thanks – codejourney Jun 29 '20 at 20:13
  • Yes, that would should do it. As a side note, I'm asuming your code is just a sample, but just in case: initializing the database in a class constructor is usually a bad practice. – redent84 Jul 01 '20 at 09:56

0 Answers0