Using EF Core with SQL Server
Let’s start with a relational database to store games and moves in multiple tables. We need to do the following:
- Create a class library project
- Create the EF Core context
- Customize the mapping of simple properties
- Create value conversions to map complex properties
- Define relations between games and moves
- Create shadow properties for the
Move
type - Implement the repository contract
- Configure the application model with SQL Server
- Configure the DI container with the minimal APIs project
Creating a data class library using SQL Server
To create the class library project, you can use the .NET CLI as shown or use the class library project template from Visual Studio:
dotnet new classlib --framework net8.0 -o Codebreaker.Data.SqlServer
To access SQL Server with EF Core, add the Microsoft.EntityFrameworkCore.SqlServer
NuGet package. This project also has a dependency on the Codebreaker.GameAPIs.Models...