Learn Minimal APIs from start to finish. Perfect for beginners.

Using EF Core navigation properties with Include()

David Grace David Grace

You can use Include() in Entity Framework Core to populate navigation properties when using queries.

You can learn more about why your navigation properties are returning null, or watch the video.

Download the code example

You'll need to fill out the code example form. We will send you an email where you can download the code example.

Software

This is the software that will need to be installed onto your machine.

Import the database

This application integrates with a SQL Server database. Inside the Database folder, there is RoundTheCode_EFInclude.bacpac and RoundTheCode_EFInclude.bak. Use either of these database backups to import the database to your local SQL Server database server.

The application

Inside src/Api/appsettings.Development.json, find ConnectionStrings.EFIncludeDbContext and point the connection string to your imported database.

Open up RoundTheCode.EFInclude.slnx, and run the application. The Swagger documentation will load at https://localhost:7253/swagger with these GET endpoints:

  • /api/blogs/{id}/with-posts - Returns the blog with the posts that belong to it. Try it with {id} of 1.

  • /api/blogs/{id}/with-metadata - Returns the blog with the metadata that belong to it. Try it with {id} of 1.

  • /api/blogs/{id}/with-post-count - Returns the blog ID, blog name and the number of posts in that blog. Try it with {id} of 1.

  • /api/blogs/{id}/with-posts-and-subscribers - Returns the blog with the posts and subscribers that belong to it. Try it with {id} of 1.

  • /api/posts - Returns all the posts with the blog that it relates to.

  • /api/posts/{id} - Returns the post and the blog it relates to. Try it with {id} of 1.

Download this code example

Enter your email and we'll send you a download link.

Download this code example

Enter your email and we'll send you a download link.