In this tutorial, we will reverse engineer an existing database to create an Entity Framework Core model in a .NET Core application,
using the .NET Command-line Interface (CLI).
Let's say that you have access to a Movies database consisting of the following tables: Movie, MovieCast, User, Genre, MovieGenre, and MovieRating.
You have been tasked with developing a .NET Core application to add, edit, and remove movies from the database using Entity Framework Core.
To do this, you would need to have a way to map entities or classes within your application to tables in the database. Creating the entities one by one is a time consuming task. This is where scaffolding comes into play.
Reverse engineering or scaffolding a database maps the tables in your database to entities or classes in your application without you having to manually port them over one by one.
This method grants you a lot of flexibility when getting started developing applications or APIs that connect to databases.
With a single command you will first scan your database schema, reading information about your tables, and then use that information to generate an Entity Framework Core model.
In the model, tables map to entity types (classes), columns to properties within those entities, and foreign keys to the relationships between the entities.
Note that only tables with primary keys can be reversed engineered through the process described in this tutorial.
To begin, let's make sure you have installed the current .NET Core SDK. You may get it from:
https://dotnet.microsoft.com/download
We will create a .NET Core 2.2 web application, add some required NuGet packages, then scaffold an existing database into the web app.
Note that you will need your database's connection string to connect to the database. You may skip steps 1 to 3 if you have already created an application.
Create a new directory for the project:
mkdir EFCoreScaffoldWebApp
Navigate to the directory:
cd EFCoreScaffoldWebApp
Create a new ASP.NET Core web app:
dotnet new webapp
Add the EF Core provider for your database schema.
For this project, we will use SQL Server. View all available providers here.
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
Add the required EF Core Design package, which contains the EF Core commands:
dotnet add package Microsoft.EntityFrameworkCore.Design
You may optionaly test to see if EF Core has been successfully installed by
using the --help (-h) command. This should display the help information for the EF tools installed:
dotnet ef -h
We will now use the DbContext Scaffold command to generate the EF Core model.
This single command will map your tables to entities in your application.
It requires two arguments: a connection string and a database provider.
The connection string is required to be able to communicate with your database, and scan its schema.
The database provider will be the same as the provider's NuGet package name from step 4.
The optional third argument: -o (or --output-dir) specifies the directory where the entities will be created.
By default, the entities and DbContext class are created in the project's root directory. I prefer to have them in the Models folder:
dotnet ef dbcontext scaffold “YOUR-CONNECTION-STRING“ Microsoft.EntityFrameworkCore.SqlServer -o Models
At this point you can see that within your Models folder, the entities have been created along with a DbContext class.
The DbContext class will take the name of your database plus the word "Context". You can optionally override this behavior
by using the -c or --context option:
dotnet ef dbcontext scaffold “YOUR-CONNECTION-STRING“ Microsoft.EntityFrameworkCore.SqlServer -o Models -c "CustomDbContext"
You can also limit which tables are reversed engineered.
The --table option can be used to include specific tables.
To include multiple tables, specify the option multiples times:
dotnet ef dbcontext scaffold “YOUR-CONNECTION-STRING“ Microsoft.EntityFrameworkCore.SqlServer -o Models -c "CustomDbContext"
-table Genre -table MovieGenre
The resulting model is configured using the Fluent API by default, which is the recommended approach.
The configurations are placed in the generated DbContext's OnModelCreating method.
Once your EF Core model has been generated, the recommended approach to update it according to database changes is to use Migrations.
However, if you want to re-scaffold the entire model, you can do so by specifying the --force option in the DbContext Scaffold command, for example:
dotnet ef dbcontext scaffold “YOUR-CONNECTION-STRING“ Microsoft.EntityFrameworkCore.SqlServer -o Models --force
For more information on all the scaffolding options available, visit:
https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#dotnet-ef-dbcontext-scaffold
In this tutorial we learned how to scaffold a database to create entities within a .NET Core web application.
We used CLI tools to create a new application from scratch, download the required NuGet packages for EF Core and the database provider, and ran the command to generate entities
in the application's Models folder, according to our database tables. This is an essential feature to help you quickly get started building applications that communicate with databases.
Tell us about your project. We will get back to you as soon as possible.