Using EF Core navigation properties with Include()
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.
Visual Studio 2026. Version 18.0.0 or above. It will work with the free community version.
.NET 10 SDK. Version 10.0.0 or above.
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}of1./api/blogs/{id}/with-metadata- Returns the blog with the metadata that belong to it. Try it with{id}of1./api/blogs/{id}/with-post-count- Returns the blog ID, blog name and the number of posts in that blog. Try it with{id}of1./api/blogs/{id}/with-posts-and-subscribers- Returns the blog with the posts and subscribers that belong to it. Try it with{id}of1./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}of1.
Download this code example
Enter your email and we'll send you a download link.
Related code examples
Download a .NET 10 Web API integrated with Minimal APIs and SQL Server alongside Entity Framework Core and FluentValidation.
Get started with EF Core includes creating a DbContext, adding a migration, CRUD operations, the Include method, raw SQL queries and stored procedures.
Download this code example
Enter your email and we'll send you a download link.