title: How to save data from API to database?
body: <p>I wrote a program which consumes external API and it works great. I went through SO and other resources but can't solve my problem. I'm quite new to c#. I created model <code>GetEquipment</code> where I store data returned from API and <code>EquipmentDb</code> which is a SQL table where I want to insert data. I tried to save it this way:</p>
  <p><strong>Consume API:</strong></p>
  <pre><code>    public async Task&lt;IEnumerable&lt;GetEquipment&gt;&gt; GetDataFromAPI()
      {
           try
           {
               var token = await _accountService.GetToken(
                    _configuration[&quot;CredentialsXY:client_id&quot;],
                    _configuration[&quot;CredentialsXY:client_secret&quot;]);
  
               var equipment = await _callAPIService.GetData(token);
  
               return equipment;
  
           }
           catch (Exception e)
           {
               Debug.Write(e);
               return Enumerable.Empty&lt;GetEquipment&gt;();
           }
       }
  </code></pre>
  <p><strong>Model - SQL table:</strong></p>
  <pre><code>    public partial class EquipmentDb
      {
          public string SerialNo { get; set; }
          public double? Latitude { get; set; }
          public double? Longitude { get; set; }
      }
  </code></pre>
  <p><strong>Model - data returned from API:</strong></p>
  <pre><code>        public class GetEquipment
          {
              public Item[] items { get; set; }
          }
  
          public class Item
          {
              public string serial_number { get; set; }
              public Location location { get; set; }
          }
          public class Location
          {
              public float longitude { get; set; }
              public float latitude { get; set; }
          }
  </code></pre>
  <p><strong>Save data to db:</strong></p>
  <pre><code>        public async Task Index()
          {
              var result = await GetDataFromAPI();  // result from API
  
              using (var ctx = new HUDAContext())
              {
                  var equipment = new EquipmentDb(); // SQL table
  
                      foreach(var item in result)
                      {
                          equipment.SerialNo = item.items.serial_number,  // here I iterate through 'result' where I store data from API but it does not work, 'items' is highlighted
                          equipment.Latitude = item.items.location.latitude,
                          equipment.Latitude = item.items.location.longitude
                      };
  
                  ctx.EquipmentDbs.Add(equipment);
                  await ctx.SaveChangesAsync();
              }                                            
          }   
  </code></pre>
  <p>Inserting data is not working, I try to assign data from <code>result</code> to each field in my SQL table but it doesn't work out.</p>