Querying EF Core models
Now that we have a model that maps to the Northwind database and two of its tables, we can write some simple LINQ queries to fetch data. You will learn much more about writing LINQ queries in Chapter 11, Querying and Manipulating Data Using LINQ.
For now, just write the code and view the results:
- At the top of
Program.cs
, import the main EF Core namespace to enable the use of theInclude
extension method to prefetch from a related table:using Microsoft.EntityFrameworkCore; // Include extension method
- At the bottom of
Program.cs
, define aQueryingCategories
method, and add statements to do these tasks, as shown in the following code:- Create an instance of the
Northwind
class that will manage the database. Database context instances are designed for short lifetimes in a unit of work. They should be disposed of as soon as possible so we will wrap it in ausing
statement. In Chapter 14, Building Websites Using ASP...
- Create an instance of the