title: How to do the CRUD operations for a table with a foreign key with SQLiteNet-Extensions
body: <p>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 ?</p>
  <p>I tried to understand the documentation : <a href="https://bitbucket.org/twincoders/sqlite-net-extensions/src/master/" rel="nofollow noreferrer">https://bitbucket.org/twincoders/sqlite-net-extensions/src/master/</a></p>
  <p>Without any success, what i don't get is that the WithChildren methods doesn't seem to be recognized unless the <code>database = new SQLiteAsyncConnection(dbPath);</code> 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 ?</p>
  <p>Thank you, i am kinda lost with this.</p>
  <p><strong>Agenda.cs in the Models folder</strong></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Text;
  using SQLite;
  using Calculette.Database;
  using SQLiteNetExtensions.Attributes;
      [Table(&quot;Agenda&quot;)]
      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)]
  }
  </code></pre>
  <p><strong>Tasks.cs in Models folder</strong></p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using SQLite;
  using SQLiteNetExtensions.Attributes;
  [Table(&quot;Task&quot;)]
  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; }
      
  }
  </code></pre>
  <p><strong>AgendaDatabase.cs in the Database folder ( all my operation currently for the Agenda table)</strong></p>
  <pre><code>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&lt;Agenda&gt;().Wait();
  database.CreateTableAsync&lt;Tasks&gt;().Wait();
      
  
  
      }
  
      // Get all agenda
      public Task&lt;List&lt;Agenda&gt;&gt; GetAgendasAsync()
      {
  
          return database.Table&lt;Agenda&gt;().ToListAsync();
      }
  
      // Get specific agenda
      public Task&lt;Agenda&gt; GetAgendaAsync(int id)
      {
          return database.Table&lt;Agenda&gt;()
                          .Where(i =&gt; i.ID == id)
                          .FirstOrDefaultAsync();
      }
  
      // Insert new agenda (save)
      public Task&lt;int&gt; SaveAgendaAsync(Agenda agenda)
      {
          if (agenda.ID != 0)
          {
              return database.UpdateAsync(agenda);
          }
          else
          {
              return database.InsertAsync(agenda);
          }
      }
      
      //Delete specific agenda
      public Task&lt;int&gt; DeleteAgendaAsync(Agenda agenda)
      {
          return database.DeleteAsync(agenda);
      }
      public Task&lt;int&gt; AddAgendaAsync(Agenda agenda)
      {
          return database.InsertAsync(agenda);
      }
  }
  </code></pre>