EF Core creating an async call for HTTP PUT

199 Views Asked by At

Can I get some guidance on how to make these functions async? Run table record delete, then, insert new data EF Core 2.2 - I am running into concurrency issues.

  1. Routing_Tool_SO4 is the main table that only gets updates
  2. Routing_Tool_Services records are erased and new items are added every time.

RoutingToolController:

[HttpPut("updateSO4")]
public IActionResult UpdateSOJ4([FromBody] Routing_Tool_SO4 routing_Tool_SO4)
{
    Routing_Tool_SO4 request = new Routing_Tool_SO4();
    request.Id = routing_Tool_SO4.Id;
    request.Routing_Tool_Services = routing_Tool_SO4.Routing_Tool_Services;
    request.When = routing_Tool_SO4.When;
    request.Where = routing_Tool_SO4.Where;
    
    // needs to run first to erase secondary table 
    _repository.UpdateServiceSOJ4(request.Id);

    // then update main table
    _repository.UpdateSO4(request);

    return Ok(request);
}

IRoutingToolRepo:

void UpdateServiceSO4(object routing_Tool_Service);

RoutingTool.cs:

// run first to erase records on secondary table
 public void UpdateServiceSO4(object routing_Tool_Service)
 {
     var Id = new SqlParameter("Id", routing_Tool_Service);
     var sql = _context.Routing_Tool_Service.FromSql("SELECT * FROM dbo.Routing_Tool_Service WHERE Routing_Tool_SO4Id = @Id", Id).ToList();

     if (sql != null)
     {
        _context.Routing_Tool_Service.RemoveRange(sql);
        _context.SaveChanges();
      }
}
    
// run to update only the records on the main table  
public void UpdateSO4(object routing_Tool_SO4)
{ 
      _context.Update(routing_Tool_SO4).State = EntityState.Modified;
        _context.SaveChanges();
}
0

There are 0 best solutions below