Entity Framework Core: DatabaseFirst
Hello friends! In my previous article, I provided information about Entity Framework Core, a data access technology, and LINQ. In this article, I focused on the Entity Framework Core Database First approach. I hope you find it an enjoyable read. Thank you in advance for reading.
Figure1(DatabaseFirst Approach Visual)
What is DatabaseFirst?
First of all, I believe it is necessary to provide the following information: Entity Framework Core establishes a relationship with the database in two ways:
Database First
Code First
Since these two approaches offer solutions to different problems, they are subjective and based on personal preference. In this article, I will explain one of these two approaches, which is the Database First approach. The first step in the Database First approach is to create the database and the tables associated with it, or an existing database can be used. For example, if there is already an existing project with a ready-made database, and Entity Framework Core is to be used in the project, the Database First approach is preferred. This does not mean that the Database First approach is not used in projects started from scratch. If the developer wishes, they can also use the Entity Framework Core Database First approach in a project started from scratch.
The second step is to build the project (app). In the project, objects/classes corresponding to the database and the tables within it are constructed. These objects/classes are referred to as Entity classes on the code side. Entity Framework Core has introduced Scaffolding commands to automate the construction between the project (app) and the database for the Database First approach. Thanks to these commands, everything desired is automatically available without touching the code. There is a very important issue that needs to be addressed in the Database First approach: Customizing the models. After the project is generated, that is, after the database scaffold commands are reflected on the code side, it may be desired to make changes to the generated classes.
When is DatabaseFirst Used?
The Database First approach is used in the following cases:
· In projects with pre-existing, already created databases.
· In long-lasting applications.
· In cases where decisions about database management, development processes, and design are not made by developers.
To reinforce what has been explained and to make it more memorable, I have provided an example below. Usually, the Database First approach is demonstrated using an existing database. In this example, I chose to explain using a SQL Server database server and a widely used Northwind database that developers can easily find SQL codes or the database file for when searched on the internet.
Now we can move on to our example. As you can see in the visual below, we have a database named NORTHWND in SQL Server.
Figure2(Microsoft SQL Server and NORTHWND Database)
There are tables in the database. You can explore these tables in more detail if you wish, but I didn’t want to go into too much detail to stay focused.
Figure3(NORTHWND Database Tables)
After adding the database to SQL Server and taking a look at the tables, we can move on to the code part of our project. I am using Rider as my IDE, and I recommend it because there is a free version available for students. However, you can also work comfortably with other IDEs like Visual Studio or Visual Studio Code. You can start creating your project by selecting “New Solution” from the visual below.
Figure4(Rider Project Creation or Opening Screen)
From the resulting screen, we choose “Class Library” under .Net Core. Actually, a Console application would be even better. The choice is yours.
Figure5(.Net/.Net Core Project Types)
Next, we need to enter the project name and solution name. These two names should be different. The solution name is more general, and the project name is more specific. For example, you can name the solution “EntityFrameworkCore” and the project “EntityFrameworkCore.DatabaseFirst.” I made this mistake before, so be sure to pay attention.
Figure6(.Net Core Solution Name and Project Name)
Since we created a Class Library project, a class is created along with the project by default. You can delete this class if you want, but I will delete it to avoid confusion.
Figure7(DatabaseFirst Project)
To be able to run EntityFramework Core Scaffold commands in the PM Console, the Microsoft.EntityFrameworkCore.Tools package must be added to the project. Otherwise, you won’t be able to run Scaffold commands in the PM Console.
Figure8(Microsoft.EntityFrameworkCore.Tools Package)
When installing packages for the project, be sure to check the .Net version of the project and the versions of the downloaded packages. If the .Net version of the project does not support the versions of the packages, you can upgrade or downgrade the package versions. So, be sure to research when downloading packages.
Figure9(Microsoft.EntityFrameworkCore.Tools Package Version)
I will be using SqlServer as the database server in the project, so I have added the EntityFramework Core SqlServer Provider package to the project. Remember to add the Provider that matches your database and the one you downloaded.
Figure10(Microsoft.EntityFrameworkCore.SqlServer Provider Package)
Figure11(Microsoft.EntityFrameworkCore.SqlServer Package Version)
It is also possible to run Scaffold commands through the .Net CLI, and of course, a package must be installed for this. This package is Microsoft.EntityFrameworkCore.Design.
Figure12(Microsoft.EntityFrameworkCore.Design Package)
Figure13(Microsoft.EntityFrameworkCore.Design Package Version)
I’m using the Rider IDE, so I don’t have a PM Console. Therefore, I will run Scaffold commands through the .Net CLI, but you can easily find both PMC and .Net CLI commands on the internet. I need to remind you that to be able to use Scaffold commands through the .Net CLI, you must be in the project directory; otherwise, you will get an error.
Get Ahmet Faruk Evkaya’s stories in your inbox
Join Medium for free to get updates from this writer.
In the visual below, I went to the project directory to run Scaffold commands. You can also get there by running the commands below, but your project name may be different, so be careful.
Figure14(.Net CLI Screen — Navigate to Project Directory)
We have downloaded the necessary packages and come to the project directory, so now we can start running Scaffold commands.
The “dotnet ef dbcontext scaffold “connection_string” <Provider>” command is the first Scaffold command we will discuss. It is the command that allows us to generate the first database to the code side.
The “dotnet ef” command is the term used to run commands in .Net CLI. The connection_string is the database connection string. If you don’t know the connection_string, you can find it by searching for “connection string” in your browser, depending on the database you are using. Of course, you need to change the necessary parameters. The Provider is the database provider you added to the project. Remember that the database you use and the downloaded provider must be the same. I have tried to show an example of these in the example below.
Figure15(Scaffold Commands — Generate Code from Database)
After running the Scaffold command, classes should be generated as shown below. If they don’t come as expected, there must be an error. I recommend checking.
Figure16(Entity Classes and Context Class)
The “dotnet ef dbcontext scaffold “connection_string” <Provider> — table Class1 –table Class2" command is used when you want to generate some tables from the database to the code side.
Figure17(Scaffold Commands — Generate Code for Table Command)
After running the command, you should get an output like the one below.
Figure18(Specific Entity Classes and Context Class)
The “dotnet ef dbcontext scaffold “connection_string” <Provider> — table Class1 –table Class2 –context NewContextName” command is the Scaffold command used when you want to change the name of the context class. I also provided a screenshot of this command below.
Figure19(Rename Context Class)
After running the command, you should get an output like the one below.
Figure20(Rename Specific Entity Classes and Context Class)
The “dotnet ef dbcontext scaffold “connection_string” <Provider> — table Class1 –table Class2 –context-dir NewDirectionContextClassName –output-dir NewDirectionEntityClassName” command is a Scaffold command that allows you to add the context class and entity classes to the folders named NewDirectionContextClassName and NewDirectionEntityClassName.
Figure21(Add Entity Classes and Context Class to a Folder)
After running the command, you should get an output like the one below.
Figure22(Add Entity Classes and Context Class to a Folder Visual)
The “dotnet ef dbcontext scaffold “connection_string” <Provider> — table Class1 –table Class2 –context-dir NewDirectionContextClassName –output-dir NewDirectionEntityClassName –namespace NewEntityNamespace –context-namespace NewContextNameSpace” command allows you to place the context class and entity classes under a specific namespace. This might be necessary for future partial classes.
Figure23(Change Namespace of Entity Classes and Context Class)
After running the command, you should get the visuals shown below.
Figure24(Add Entity Classes and Context Class to Folder and Namespace Visual — 1)
Figure25(Add Entity Classes and Context Class to Folder and Namespace Visual — 2)
Figure26(Add Entity Classes and Context Class to Folder and Namespace Visual — 3)
Figure27(Add Entity Classes and Context Class to Folder and Namespace Visual — 4)
Figure28(Add Entity Classes and Context Class to Folder and Namespace Visual — 5)
As the project progresses, there will be changes in the code side and the database. When you want to generate database changes into the code side and avoid conflicts, you can run the “dotnet ef dbcontext scaffold “connection_string” <Provider> — table Class1 –table Class2 –context-dir NewDirectionContextClassName –output-dir NewDirectionEntityClassName –namespace NewEntityNamespace –context-namespace NewContextNameSpace — force” command.
Figure29(Scaffold Commands — Force Command)
To prevent possible conflicts, partial classes have been introduced. If you notice, Scaffold commands add a “partial” prefix to the classes generated. This prefix separates classes in the code side and prevents conflicts.
Figure30(Partial Classes — Partial Folder)
Later-created partial classes must have the same name as the entity classes and be in the same namespace. I recommend carefully reviewing the visual below.
I hope this has been a useful article for you. Thank you for reading. See you in my next article. :)








