public async Task<IActionResult> Create(RoomViewModel model)
{
if (ModelState.IsValid)
{
// Converts the model to a Room object.
var room = _converterHelper.ToRoom(model, true);
// Before creating the Room check if there's a room with the same VetId, nulls it if exists.
var previousRoom = _roomRepository.GetByVet((int)model.VetId).AsNoTracking().FirstOrDefault();
if (previousRoom != null)
{
previousRoom.VetId = null;
await _roomRepository.UpdateAsync(previousRoom);
}
// Creates Room
await _roomRepository.CreateAsync(room);
// Changes the room on the related Vet object if provided.
if(room.VetId != null)
{
Vet vet = await _vetRepository.GetByIdAsync((int)room.VetId);
await _vetRepository.UpdateAsync(vet); //Error occurs at this point.
}
return RedirectToAction(nameof(Index));
}
return View(model);
}
public async Task<IActionResult> Create(RoomViewModel model)
{
if (ModelState.IsValid)
{
// Converts the model to a Room object.
var room = _converterHelper.ToRoom(model, true);
// Before creating the Room check if there's a room with the same VetId, nulls it if exists.
var previousRoom = _roomRepository.GetByVet((int)model.VetId).AsNoTracking().FirstOrDefault();
if (previousRoom != null)
{
previousRoom.VetId = null;
await _roomRepository.UpdateAsync(previousRoom);
}
// Creates Room
await _roomRepository.CreateAsync(room);
// Changes the room on the related Vet object if provided.
if(room.VetId != null)
{
Vet vet = await _vetRepository.GetByIdAsync((int)room.VetId);
await _vetRepository.UpdateAsync(vet); //Error occurs at this point.
}
return RedirectToAction(nameof(Index));
}
return View(model);
}