Hello,
Look at this very basic example.
Here are 2 Entity Framework models:
public class Voitures
{
public int id { get; set; }
public string nom_voiture { get; set; }
public int MarqueId { get; set; }
public Marques marque { get; set; }
}
public class Marques
{
public int id { get; set; }
public string nom_marque { get; set; }
}
And Here is a controller action method:
public IActionResult GetVoiture(int id)
{var voiture = bdd.voitures.FirstOrDefault(v => v.id == id);
return Json(voiture);
}
Here is the result i get:
{"id":2,"nom_voiture":"golf","marqueId":1,"marque":null}
As you can see, the object "marque" is null.
Now, if i just add a single line in my method controller, look at the result:
public IActionResult GetVoiture(int id)
{ var voiture = bdd.voitures.FirstOrDefault(v => v.id== id);
var m = bdd.marques.FirstOrDefault(f => f.id == voiture.MarqueId);
return Json(voiture);
}
You can see the marque object is not null.
{"id":2,"nom_voiture":"golf","marqueId":1,"marque":{"id":1,"nom_marque":"VW"}}
This is very strange for me because "m" variable is not assigned to nothing so i do not understand how framework can know and fetch marque object in memory...
Thanks