Say there is a job record that belongs to a person, in the first foreach statement we are just updating the info we always already have - this works fine and as intended.
For the second foreach statement, JobTasks
is something we may already have info on or not, so the intended behaviour is to check if the ID is present, if not add it like it's being created for the first time.
private void UpdateAnswers(Job job)
{
foreach (var item in job.JobInfo)
{
_db.Entry(item).State = EntityState.Modified;
}
foreach (var item in job.JobTasks)
{
_db.Entry(item).State = item.JobID == 0 ?
EntityState.Added :
EntityState.Modified;
}
}
When I proceed further trying to add it for the first time it does not add the record in the table JobTasks
. Any help?
question from:
https://stackoverflow.com/questions/65907800/update-item-or-create-new-if-does-not-exist-c-sharp 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…