ASP.NET Monsters #57: Loading Related Data with Entity Framework Core

Sign in to queue

Description

Entity Framework Core doesn't support lazy loading yet which means you need to explicitly tell Entity Framework what related data to include when querying for an entity. In today's episode, Monster Dave shows us how to load related data using the .Include and .ThenInclude extension methods.

 

Embed

Download

Download this episode

The Discussion

  • User profile image
    pter

    Is this good practice to call context from Controller? Should we keep seperate model between repo and ui side?

  • User profile image
    David​Paquette

    @pter:Good question? It is generally not a great idea to pass the entity model directly to the view for binding but as always it depends on the situation. In very simple cases there is no benefit to introducing extra layers of separation.

    I don't have a problem with using the context directly in the controller for doing simple queries but there are a number of patterns that promote a better separation of concerns. We will keep evolving this example over a number of episodes to show some different options.

  • User profile image
    Siderite

    Will you ever fix the site to be at odds with the current standard for video web sites? Let me focus on the video, pause/play with Space, move backward/forward with the arrow keys. Not everybody is stuck with their hand on their mouse!

  • User profile image
    Siderite

    Oh, and when you comment on something the video stops and restarts because it uses a full page POST. Come on! This is a web site in 2016 that offers videos about software development.

  • User profile image
    Luis Forero

    Nice video,

    I would like to ask you how I can get to work the include Method. What I Mean is that when I'm building the query after the entity y press dot to get intellisense and I'm getting all kind of intellicense bud the include method. Any thoughts?

    I'm using asp.net core with EntityFramewor Template

  • User profile image
    David​Paquette

    @Luis Forero: Add using the following using statement to the top of your file

    using Microsoft.EntityFrameworkCore;

Add Your 2 Cents