In this chapter, you will create the example project used throughout this part of the book. The project contains a simple data model, a client-side package for formatting HTML content, and a simple request pipeline.
Creating the Project
Open a new PowerShell command prompt from the Windows Start menu and run the commands shown in Listing 18-1.
You can download the example project for this chapter—and for all the other chapters in this book—from https://github.com/apress/pro-asp.net-core-3. See Chapter 1 for how to get help if you have problems running the examples.
Creating the Project
Adding a Data Model
A data model helps demonstrate the different ways that web applications can be built using ASP.NET Core, showing how complex responses can be composed and how data can be submitted by the user. In the sections that follow, I create a simple data model and use it to create the database schema that will be used to store the application's data.
Adding NuGet Packages to the Project
Adding Packages to the Project
If you are using Visual Studio, you can add the packages by selecting Project ➤ Manage NuGet Packages. Take care to choose the correct version of the packages to add to the project.
Installing a Global Tool Package
Creating the Data Model
The Contents of the Category.cs File in the Models Folder
The Contents of the Supplier.cs File in the Models Folder
The Contents of the Product.cs File in the Models Folder
Each of the three data model classes defines a key property whose value will be allocated by the database when new objects are stored. There are also navigation properties that will be used to query for related data so that it will be possible to query for all the products in a specific category, for example.
The Price property has been decorated with the Column attribute, which specifies the precision of the values that will be stored in the database. There isn’t a one-to-one mapping between C# and SQL numeric types, and the Column attribute tells Entity Framework Core which SQL type should be used in the database to store Price values. In this case, the decimal(8, 2) type will allow a total of eight digits, including two following the decimal point.
The Contents of the DataContext.cs File in the Models Folder
The context class defines properties that will be used to query the database for Product, Category, and Supplier data.
Preparing the Seed Data
The Contents of the SeedData.cs File in the Models Folder
The static SeedDatabase method ensures that all pending migrations have been applied to the database. If the database is empty, it is seeded with categories, suppliers, and products. Entity Framework Core will take care of mapping the objects into the tables in the database, and the key properties will be assigned automatically when the data is stored.
Configuring Entity Framework Core Services and Middleware
Preparing Services and Middleware in the Startup.cs File in the WebApp Folder
Defining a Connection String in the appsettings.json File in the WebApp Folder
In addition to the connection string, Listing 18-10 increases the logging detail for Entity Framework Core so that the SQL queries sent to the database are logged.
Creating and Applying the Migration
Creating an Entity Framework Core Migration
Applying the Migration to the Database
The logging messages displayed by the application will show the SQL commands that are sent to the database.
If you need to reset the database, then run the dotnet ef database drop --force command and then the command in Listing 18-12.
Adding the CSS Framework
Installing the LibMan Tool Package
Installing the Bootstrap CSS Framework
Configuring the Request Pipeline
The Contents of the TestMiddleware.cs File in the WebApp Folder
Adding a Middleware Component in the Startup.cs File in the WebApp Folder
Running the Example Application
Running the Example Application
Summary
In this chapter, I created the example application that is used throughout this part of the book. The project was created with the Empty template, contains a data model that relies on Entity Framework Core, and is configured with a request pipeline that contains a simple test middleware component. In the next chapter, I show you how to create web services using ASP.NET Core.