Entity framework, code first. Child objects not populating when called

后端 未结 2 2047
遥遥无期
遥遥无期 2021-02-04 09:53

I\'m getting to grips with EF code first. My domain model design doesn\'t seem to support the auto \'populating\' child of objects when I call them in code.

Mod

2条回答
  •  迷失自我
    2021-02-04 10:17

    The reason why you don't have the Coordinates is because it's not included in the query. There are multiple ways to include it in the result:

    1. _context.Cars.Include(car => car.Coordinates).ToList(); --- it'll fetch cars with coordinates in one query
    2. If you don't need Coordinates for all the cars, you can do the following: make the Coordinates property virtual, then when you'll get cars, you can get Coordinates for only subset of them if you need and the separate calls will be made to the database for each property "get" access. You'll also see in the debugger that EF created dynamic classes for you, so that's why you had to make it virtual
      Also see this answer for more details on this.

提交回复
热议问题