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 12, Querying and Manipulating Data Using LINQ. For now, just write the code and view the results:
- Open
Program.cs
and import the following namespaces:using static System.Console; using Packt.Shared; using Microsoft.EntityFrameworkCore; using System.Linq;
- In
Program
, 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 as soon as possible so we will wrap it in ausing
statement. - Create a query for all categories that include their related products.
- Enumerate through the categories...
- Create an instance of the