Mitchelln11
Active member
- Joined
- Apr 10, 2020
- Messages
- 39
- Programming Experience
- Beginner
How do you populate a database with multiple records from a REST API call?
I'm trying to populate information from a REST API into my MS SQL database.
I'm actually rebuilding on .NET Core from an original Framework project.
Using the National Park Service REST endpoint.
I have a Park model and a model for the REST API results.
This worked in Framework, but not in Core.
This code should run through all listed parks, and add to my database one by one.
When it does run, it will add the first park that isn't already on the database. It fails on the second new park every time with the following message:
"SqlException: Cannot insert explicit value for identity column in table 'Parks' when IDENTITY_INSERT is set to OFF."
I've tried a few things in the if (uniqueParkCode == null) {} statement, but getting no further.
I've tried the following to turn the IDENTITY_INSERT ON: but that doesn't work either. (Identity Insert)
Apparently, ExecuteSqlCommand is obsolete now, so I tried ExecuteSqlRaw.Still not working. I get the same error after adding 1 new record.
I'm trying to populate information from a REST API into my MS SQL database.
I'm actually rebuilding on .NET Core from an original Framework project.
Using the National Park Service REST endpoint.
C#:
public async Task FetchParkApi()
{
string url = $"https://developer.nps.gov/api/v1/parks?q=National%20Park&limit=91&api_key=api-key-here";
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url);
string jsonresult = await response.Content.ReadAsStringAsync();
Park park = new Park(); // Instantiate a new park so you can use it in this method
if (response.IsSuccessStatusCode)
{
RestApiNationalParks parkInfo = JsonConvert.DeserializeObject<RestApiNationalParks>(jsonresult); // Run through entire JSON file
var parkList = parkInfo.data.Select(m => m).ToList(); // returns entire list of Parks, up to 150, this only has 90 based of the National Parks query
foreach (var singlePark in parkList)
{
if(singlePark.designation.Contains("National and State Parks") || singlePark.designation.Contains("National Park"))
{
park.ParkName = singlePark.fullName;
park.ParkState = singlePark.states;
park.ParkDescription = singlePark.description;
park.ParkLatitude = singlePark.latitude;
park.ParkLongitude = singlePark.longitude;
park.ParkCode = singlePark.parkCode;
var uniqueParkCode = _context.Parks.Where(c => c.ParkCode == singlePark.parkCode).FirstOrDefault();
if (uniqueParkCode == null)
{
_context.Parks.Add(park);
_context.SaveChanges(); // After running: "SqlException: Cannot insert explicit value for identity column in table 'Parks' when IDENTITY_INSERT is set to OFF."
}
await _context.SaveChangesAsync();
}
}
await _context.SaveChangesAsync();
}
}
This worked in Framework, but not in Core.
This code should run through all listed parks, and add to my database one by one.
When it does run, it will add the first park that isn't already on the database. It fails on the second new park every time with the following message:
"SqlException: Cannot insert explicit value for identity column in table 'Parks' when IDENTITY_INSERT is set to OFF."
I've tried a few things in the if (uniqueParkCode == null) {} statement, but getting no further.
I've tried the following to turn the IDENTITY_INSERT ON: but that doesn't work either. (Identity Insert)
C#:
try
{
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Customers ON");
context.SaveChanges();
context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT dbo.Customers OFF");
}
finally
{
context.Database.CloseConnection();
}
Last edited by a moderator: