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. - Create a query for all categories that include their related products.
- Enumerate through the categories, outputting the name and number of products for each one.
static void QueryingCategories() { using (var db = new Northwind()) { WriteLine("Categories and how many products they have:"); ...
- Create an instance of the