Skip to content

Instantly share code, notes, and snippets.

@0xced
Created December 13, 2021 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xced/85bf3539210329cc1062a6c4a8b11e88 to your computer and use it in GitHub Desktop.
Save 0xced/85bf3539210329cc1062a6c4a8b11e88 to your computer and use it in GitHub Desktop.
Scaffold an EF Core DbContex with nullable reference types

Scaffold an EF Core DbContex with nullable reference types

Requirement: .NET 6 (at least preview 5, where the Scaffold with nullable reference types feature landed and at least rc.2.21480.5 to work with the SQLPro Studio demo database)

Procedure

  1. Create a new dummy project (required to run the dotnet ef CLI tool)
mkdir efscaffold && cd efscaffold
dotnet new classlib
rm Class1.cs
dotnet add package Microsoft.EntityFrameworkCore.Design

Using the .NET 6.0 SDK, <Nullable>enable</Nullable> should be defined in the csproj file.

  1. Add the EF Core provider matching the database you want to scaffold. This example is for SQL Server:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
  1. Add the dotnet-ef CLI tool
dotnet new tool-manifest
dotnet tool install dotnet-ef

You can invoke the tool from this directory using the following commands: 'dotnet tool run dotnet-ef' or 'dotnet dotnet-ef'.

Tool 'dotnet-ef' (version '6.0.0') was successfully installed. Entry is added to the manifest file ~/efscaffold/.config/dotnet-tools.json.

  1. Scaffold your context and entities inside the Database directory
mkdir -p Database
dotnet ef dbcontext scaffold --verbose --force --output-dir Database --no-onconfiguring --context SqlProSampleContext "Server=sqlprosample.database.windows.net;Database=sqlprosample;User=sqlproro;Password=nh{Zd?*8ZU@Y}Bb#" Microsoft.EntityFrameworkCore.SqlServer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment