title: C# Deserialize, then insert into SQLite database
body: <p>I have the following JSON for model Project, which I cannot change:</p>
  
  
  
  <pre><code>[
      {
          "id": 3,
          "description": "Project A"
      },
      {
          "id": 6,
          "description": "Project B",
          "updated_at": "2017-07-13T09:30:51+02:00",
          "created_at": "2017-07-13T09:30:51+02:00"
      }
  ]
  </code></pre>
  
  <p>I have the following JSON for model Task, which I cannot change:</p>
  
  
  
  <pre><code>[
      {
          "id": 3,
          "project": {
              "id": 3,
              "description": "Project A"
          },
          "description": "Task 1"
      },
      {
          "id": 6,
          "project": {
              "id": 3,
              "description": "Project A"
          },
          "description": "Task 2"
      },
      {
          "id": 9,
          "project": {
              "id": 3,
              "description": "Project A"
          },
          "description": "Task 3"
      },
      {
          "id": 12,
          "project": {
              "id": 6,
              "description": "Project B"
          },
          "description": "Task 4"
      }
  ]
  </code></pre>
  
  <p>I have the following 2 models:</p>
  
  
  
  <pre><code>using SQLite.Net.Attributes;
  using SQLiteNetExtensions.Attributes;
  
  namespace BettyMultiModel.Resources.Model
  {
      public class Models
      {
  
      }
  
      class Project : Models
      {
  
          public int Id { get; set; }
          public string Description { get; set; }
  
      }
  
      class Task : Models
      {
  
          public int Id { get; set; }
          public string Description { get; set; }   
  
      }
  }
  </code></pre>
  
  <p>I use this method to get the JSON into the SQLite database:</p>
  
  
  
  <pre><code>// Insert Records of type TType into database.
  private async Task&lt;string&gt; InsertRecords&lt;TType&gt;(HttpResponseMessage response)
          where TType : Models
  {
      try
      {
          DataBase db = new DataBase();
  
          string responseString = await response.Content.ReadAsStringAsync();
          var responseBody = JsonConvert.DeserializeObject&lt;List&lt;TType&gt;&gt;(responseString);
  
  
          foreach (TType item in responseBody)
          {
              bool resultInsert = db.Insert(item);
          }
  
          return ResponseToMessage(response, "Insert Records");
      }
      catch (Exception ex)
      {
          Log.Info("DeserializeObject", ex.Message);
          throw;
      }            
  
  }
  </code></pre>
  
  <p>This works just fine when I only want to have the Id and Description properties of both models. But I would also like to store the relation between the Project model and the Task model / project id at the Task model.</p>
  
  <p>When I parse the JSON with the tasks at <a href="http://json2csharp.com/" rel="nofollow noreferrer">http://json2csharp.com/</a>, I get the following suggestion for the models:</p>
  
  
  
  <pre><code>public class Project
  {
      public int id { get; set; }
      public string description { get; set; }
  }
  
  public class RootObject // Which is Task
  {
      public int id { get; set; }
      public Project project { get; set; }  // &lt;-------- Extra property
      public string description { get; set; }
  }
  </code></pre>
  
  <p>Using the models like this - with RootObject renamed to Task - results in a Exception: NotSupportedException: Don't know about BettyMultiModel.Resources.Model.Project</p>
  
  <p>So I changed the classes to: </p>
  
  
  
  <pre><code>using SQLite.Net.Attributes;
  using SQLiteNetExtensions.Attributes;
  
  namespace BettyMultiModel.Resources.Model
  {
      public class Models
      {
  
      }
  
      class Project : Models
      {
  
          [PrimaryKey]
          public int Id { get; set; }
          public string Description { get; set; }
  
      }
  
      class Task : Models
      {
  
          [PrimaryKey]
          public int Id { get; set; }
          public string Description { get; set; }
  
          [OneToMany]
          public Project project { get; set; }
  
      }
  }
  </code></pre>
  
  <p>The Exception is not thrown any longer, but when I have a look in the db file, there are still just the Id and Description columns and no reference to a relation between the 2 models.</p>
  
  <p>What am I missing here?</p>
  
  <p><strong>EDIT: Added method that creates the tables</strong></p>
  
  
  
  <pre><code>public void ClearTable&lt;TType&gt;()
          where TType : Models
  {
      try
      {
          Log.Info("Folder DB: ", dbPath);
  
          using (var connection = new SQLiteConnection(platform, dbPath))
          {
              connection.DropTable&lt;TType&gt;();
              connection.CreateTable&lt;TType&gt;();
          }
      }
      catch (SQLiteException ex)
      {
          Log.Info("SQLiteEx", ex.Message);
      }
  
  }
  </code></pre>
  
  <p><strong>EDIT: Almost there...</strong></p>
  
  <p>I also made a little mistake, a project has many tasks, so the attribute should be [ManyToOne]. The tables in the db look OK now. The project id is just not inserted in the ProjectId column, inside the Task model. Every row has value 0. I guess I need to figure out how to map the id of the nested project object to the ProjectId property of the class Task.</p>
  
  <p>I've tried using the Newtonsoft.Json namespace like this, which inserts 1 row in the Task model and then starts throwing errors because of record the uniqueness of the id. JsonProperty("id") identifies it as the Id of Task, not of Project:</p>
  
  
  
  <pre><code>[JsonProperty("id"), ForeignKey(typeof(Project))]
  public int ProjectId { get; set; }
  </code></pre>
  
  <p>These are the models now:</p>
  
  
  
  <pre><code>class Project : Models
  {
  
      [PrimaryKey]
      public int Id { get; set; }
  
      public string Description { get; set; }
  
  }
  
  class Task : Models
  {
  
      [PrimaryKey]
      public int Id { get; set; }
  
      public string Description { get; set; }
  
      [ForeignKey(typeof(Project))]
      public int ProjectId { get; set; }
  
      [ManyToOne]
      public Project Project { get; set; }
  
  }
  </code></pre>