.Net CoreC#DevelopmentEntity Framework

Entity Framework Core – Database first


Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of Entity Framework data access technology. It’s rewritten version of Entity Framework Object-Relation-Mapper for .NET Core. It’s under .NET Foundation , hosted on GitHub, mainly developed by Microsoft, but totally open source. On short, you can use it everywhere: commercial apps, open source, you can propose changes and contribute to the development.

Permissions and licensing are under very very open and friendly .NET Core philosophy!!!!

EF enables two paradigms: Code-First and Database-First paradigm.

In this blog post I will show latter option. I will show how to scaffold or extract classes from existing relation database and us it in C# project.

Code-Fist approach means that you defined domain objects with all relations in your application and then database is generated based on your classes and relation between them. This option is generally better approach for greenfield projects – when you do your app from start.

Database-First approach means that you already have database and you need to generate domain object for your app. This approach is particular useful for developers who are more database-centric, or if database already exists (e.g. legacy systems) and we just need app to talk to this database.

As said, In this blog post I will show how to generate domain object from relation database and then later on use it in your app. For this, I will use only dotnet tool and console – no Visual Studio or some other fancy tools.

Scaffolding database structure into C# classes

Basic prerequisite is to install .NET Core 3.1 SDK. When you install this SDK you will get dotnet CLI tool. You can find more info about the tool here: https://docs.microsoft.com/en-us/dotnet/core/tools/.

For more Visual Studio type of developers: you can do everything with this. You can build, create project, add references to projects and a lot more. Furthermore, console based approach is very useful when you want to automatize some things. Therefore, I will use only console to do the job.

When I install this SDK I have dotnet tool available on my system.

You an find SDKs for different targets and platforms here: https://dotnet.microsoft.com/download/dotnet-core/3.1

dotnet tool version command

I can check which tools are registered in dot net core tool repository, by using dotnet list command:

dotnet tool list tool command

First, I need to install EF tool and register it globally.

dotnet tool install command

Now, that I have EF tool available in my arsenal…

dotnet tool list command

I can start my scaffolding process.

First, I create empty folder.

create new folder for test

Inside I create empty class library class to put my scaffolded classes in.

dotnet tool create empty class library project

Output: csproj with dummy Class1 class.

new csproj check

EF needs two NuGet references in order to work:

  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.SqlServer

So I put them in in my project. Of course via my dotnet tool!

dotnet tool adding package reference command
dotnet tool adding package reference command

Now, that everything is on place, the magic begins. I will scaffold my classes from existing SQL Server database. Let’s try

dotnet tool ef scaffold command

Models folder is created with all database mapped C# classes.

directory structure listing

Let’s check our database structure quickly

ssms project

and compare with scaffolded C# classes:

visual studio project

Everything is on place, classes in my C# project and DBContext class generated and ready to use in my application.

Command summary

Let’s quickly list commands I used for EF Database-First approach to generate my C# data classes.


If you are doing greenfield project you will most probably generate database from your code. In this case, you will used Code-First paradigm. But this approach is not necessary the best option.

Some developers (especially old-school ones 🙂 ) are more database-centic, so they are very comfortable with SQL/relational databases. In this case, you can use database first approach to use EF ORM mapping.

In this post I presented latter option. I presented how you can do EF Object-Relational mapping from existing database, generate business classes, DbContext and use it in your app. I did this by using dotnet tool and console.

Until the next time, happy coding.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.