Saturday, February 16, 2019

When a Task in C# is not a Task of something but just a Task...

Well, such a task is a Task of void. A method like so...

public void WorkshopNcoaIdUpdate(int id, string nocaId)
{
   foreach (var workshop in _context.Workshops.Where(w => w.Id == id))
   {
      workshop.NcoaId = nocaId;
   }
   _context.SaveChanges();
}

 
 

...may be rewritten like so and doing so allows the method to run asynchronously.

public async Task WorkshopNcoaIdUpdate(int id, string nocaId)
{
   foreach (var workshop in _context.Workshops.Where(w => w.Id == id))
   {
      workshop.NcoaId = nocaId;
   }
   await _context.SaveChangesAsync();
}

No comments:

Post a Comment