5 Setting up a project and database with Entity Framework Core

 

This chapter covers

  • Refactoring a legacy codebase to be clean and secure
  • Using Entity Framework Core to query a database
  • Implementing the repository/service pattern
  • Creating a new .NET 5 solution and project using the command line

The time has finally come. You are probably eager to fix some of the issues we saw in chapters 3 and 4, and now we’ll get to do that. First things first, let’s come up with a game plan on how to tackle this refactoring. We already know a couple of things that we need to do differently:

  • In chapter 3 we were told to use .NET 5 instead of the .NET Framework for the new version of the Flying Dutchman Airlines service.
  • We need to rewrite the endpoints to be clean code (in particular, adhering to the DRY principle).
  • We need to fix the security vulnerability—a hardcoded connection string.
  • The object names do not match the database column names. We should fix that to ensure a perfect isomorphic relationship between the codebase and the database.
  • We need to adhere to the OpenAPI file discussed in chapter 3 and shown in appendix D.

Although not necessarily part of the requirements, we would like to include some additional deliverables to improve the quality of the job, thus ensuring a job well done:

5.1 Creating a .NET 5 solution and project

5.2 Setting up and configuring a web service

5.2.1 Configuring a .NET 5 web service

5.2.2 Creating and using HostBuilder

5.2.3 Implementing the Startup class

5.2.4 Using the repository/service pattern for our web service architecture

5.3 Implementing the database access layer

5.3.1 Entity Framework Core and reverse-engineering

5.3.2 DbSet and the Entity Framework Core workflow

5.3.3 Configuration methods and environment variables