How to do the CRUD operations for a table with a foreign key with SQLiteNet-Extensions

418 views Asked by At

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

There are 0 answers