Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have two tables in my database, one of departments and the other of employees, and an employee can only belong to one department so

I have two tables in my database, one of departments and the other of employees, and an employee can only belong to one department so there is a departmentId in the employee that works as a foreign key of the department table referencing the id of that table, but when my api adds an employee when filling in departmentId, the value I filled in changes and the next value that has available in the id of the departments table is placed
the code i was using in the api was:
public async Task>> CreateFuncionario(Funcionario novoFuncionario)
{
ServiceResponse> serviceResponse = new ServiceResponse>();
try
{
if (novoFuncionario == null)
{
serviceResponse.Dados = null;
serviceResponse.Mensagem = "Informar Dados";
serviceResponse.Sucesso = false;
return serviceResponse;
}
_context.Add(novoFuncionario);
await _context.SaveChangesAsync();
novoFuncionario = await _context.Funcionarios.FindAsync(novoFuncionario.Id);
serviceResponse.Dados =_context.Funcionarios.ToList();
}
catch (Exception ex)
{
serviceResponse.Mensagem = ex.Message;
serviceResponse.Sucesso = false;
}
return serviceResponse;
}
i was inserting in api using:
{
"id": 0,
"nome": "Joo",
"foto": "string",
"rg": 453454353,
"departamentoId": 2,
"departamento": {
"id": 0,
"nome": "string",
"sigla": "string"
}
}
and the response was:
Response body
{
"dados": [
{
"id": 1,
"nome": "Joo",
"foto": "string",
"rg": 453454353,
"departamentoId": 8,
"departamento": {
"id": 8,
"nome": "string",
"sigla": "string"
}
}
],
"mensagem": "",
"sucesso": true
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions