title: How to Deseriazile JSON and save to database using Entity Framework
body: <p>I've pulled some JSON data from an API and now I'm trying to deserialize it into an Object and save these Objects to my SQL Server database which has been created using Entity Framework. The following piece of code won't let me add the data to the <code>Customers</code> table because the database won't accept a list.</p>
  <pre><code>string apiResponse = await response.Content.ReadAsStringAsync();
  var data = JsonConvert.DeserializeObject&lt;List&lt;Root&gt;&gt;(apiResponse);
  
  db.Customers.AddRange(data);
  db.SaveChanges();
  </code></pre>
  <p>Also the following code won't work. I'm thinking it might have something to do with using a list of <code>Customers</code> and not a list of <code>&lt;Root&gt;</code>. But I don't want to create a <code>Root</code> table in my database, I'd rather only have a table of <code>Customers</code> without the <code>Root</code> table.</p>
  <pre><code>List&lt;Customers&gt; customers = new List&lt;Customers&gt;();
  
  string apiResponse = await response.Content.ReadAsStringAsync();
  var data = JsonConvert.DeserializeObject&lt;Customers&gt;(apiResponse);
  customers.Add(data);
  
  db.Customers.AddRange(data);
  db.SaveChanges();
  </code></pre>
  <p><strong>Json</strong></p>
  <pre><code>[
    {
      &quot;id&quot;: &quot;1asda3&quot;,
      &quot;name&quot;: &quot;Customer1&quot;
    },
    {
      &quot;id&quot;: &quot;asda2&quot;,
      &quot;name&quot;: &quot;Customer2&quot;
    },
    {
      &quot;id&quot;: &quot;gfh234&quot;,
      &quot;name&quot;: &quot;Customer3&quot;
    },
    {
      &quot;id&quot;: &quot;fdg312&quot;,
      &quot;name&quot;: &quot;Customer4&quot;
    }
  ]
  </code></pre>
  <p>Model:</p>
  <pre><code>public class Root
  {
      public List&lt;Customers&gt; Customers { get; set; }
  }
  
  public class Customers
  {
      [JsonProperty(&quot;id&quot;)]
      public string id { get; set; }
      [JsonProperty(&quot;name&quot;)]
      public string name { get; set; }
  }
  </code></pre>
  <p>DbContext:</p>
  <pre><code>public DbSet&lt;Customers&gt; Customers { get; set; }
  public DbSet&lt;Root&gt; Root { get; set; }
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p>I solved my issue by simply removing the public class Root from my model. And by Using:</p>
  <pre><code>string apiResponse = await response.Content.ReadAsStringAsync();
  var data = JsonConvert.DeserializeObject&lt;List&lt;Customers&gt;&gt;(apiResponse);
  
  db.Customers.AddRange(data);
  db.SaveChanges();
  </code></pre>