I am trying to store data in database using EF my code run fine but its not storing data

if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); var jsonObject = JsonConvert.DeserializeObject<JToken>(json); if (jsonObject != null && jsonObject.HasValues) { List<Initiative_Goal> ahaInitiative = jsonObject["initiatives"]. Select(x => { Initiative_Goal initiative = new Initiative_Goal();
initiative.InitiativeID = x.Value<long>("value");
return initiative; }).ToList(); foreach (Initiative_Goal initiatives in ahaInitiative) { var existingRecord = await context.Initiative_Goal.FirstOrDefaultAsync(x => x.InitiativeID == initiatives.InitiativeID); if (existingRecord == null) { fullAPIList.Add(initiatives); } else { existingRecord.InitiativeID = initiatives.InitiativeID;
//existingRecord.Description = initiatives.Description; }
} await context.SaveChangesAsync(); } else { Console.WriteLine("No Aha data found in the response."); }
1 Reply
leowest
leowest9mo ago
oof why are u not using a proper class to deserialize your object to? have u considered using System.Text.Json its much faster also aside from that have u debugged your code to ensrue it enters the foreach and updates anything in the db entities for the db to need to perform an update? also u can use
var existingRecord = await context.Initiative_Goal.FindAsync(initiatives.InitiativeID);
var existingRecord = await context.Initiative_Goal.FindAsync(initiatives.InitiativeID);
and if the id does not exist you're not telling it to add it to the db either, did u mean to do that?
Want results from more Discord servers?
Add your server