I'm trying to update multiple records using entity framework, but am not sure how to proceed.
Basic setup:
class Appointment
{
public int Id {get; set;}
public double Charge {get; set;}
public DateTime Time {get; set;}
}
The view presents a list of appointments, and then the call to controller Post action passes in an Ienumerable<Appointment>
.
public async Task<int> UpdateAppointments(IEnumerable<Appointment> appointments){
// code goes here
var appointmentsToUpdate = await _context
.Appointments
.Where(a => a.time > DateTime.Now).ToListAsync();
// what to do here??
// loop through appointmentsToUpdate and find the relevant
// record inside appointment, and then do an update?
// Seems like a merge would be more efficient.
}
What i want to do is merge appointments and appointmentsToUpdate and update the appointment time. In another scenario, with a different authorization, I want the administrator, for example, to only be able to change the appointment charge, so deleting all records and appending the new records isn't an option.
It seems like you can do this with pure sql statements, but then the appointments
parameter is passed in as an IEnumerable, not as a table already in the database as in this answer: Bulk Record Update with SQL
First of all, can you do this kind of update using Linq? Does it translate directly to entity framework (core)?