1
Good evening, I have two classes to which one is an attribute of the other, Customer has Anamnese , when I search the database the return is the right customer, but the field Anamnese is null, and I want to recover this information.
Client class
public int Id { get; set; }
public Anamnese Anamnese { get; set; }
Anamnesis class
public int Id { get; set; }
my database is ok, client has his data and a foreign anamnesis key and can normally save a new client already with his anamnesis.
Method used to register for new anamnesis
public static void CadastrarAnamnese(Cliente clienteEntrada, Anamnese anamnese) {
try
{
using (ConsultorioContext ctx = new ConsultorioContext())
{
Cliente cliente = ctx.Clientes.Find(clienteEntrada.Id);
cliente.Anamnese = anamnese;
ctx.SaveChanges();
}
}
catch
{
}
}
How is recovering, used the include!?
– novic
No, I tried it now but I couldn’t use it, could you give me an example?
– Lucas Santos
var cliente = ctx.Clientes.Where(c => c.Id == id).Include(c => c.Anamnese).FirstOrDefault(); 

 Anamnese teste = cliente.Anamnese;
with these two lines got thank you– Lucas Santos