private async Task <IResource> CreatePatient(IResource input) { var visiPatient = _resourceMapper.MapViSiPatient(input); await _visiContext.Patient.AddAsync(visiPatient); await _visiContext.SaveChangesAsync(); // return the new resource as it was stored by this server return(_resourceMapper.MapPatient(_visiContext.Patient.Last())); }
private async Task <IResource> CreatePatient(IResource input) { var visiPatient = _resourceMapper.MapViSiPatient(input); await _visiContext.Patient.AddAsync(visiPatient); if (visiPatient.Id < 0) //No Id is assigned yet, let the database create one. { await _visiContext.SaveChangesAsync(); } else { using (var transaction = await _visiContext.Database.BeginTransactionAsync()) { try { //It can happen that in the database you target, an IDENTITY column is used. You can use a provided id //by setting IDENTITY_INSERT to ON temporarily. await _visiContext.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.Patient ON"); await _visiContext.SaveChangesAsync(); await _visiContext.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.Patient OFF"); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw ex; } } } // return the new resource as it was stored by this server return(_resourceMapper.MapPatient(_visiContext.Patient.Last())); }