How to Install Entity Framework Core in .NET Core Application
In this article, I will discuss How to Install Entity Framework Core using Visual Studio in .NET Core Application step by step with different approaches. Please read our previous article, which discussed Introduction to Entity Framework Core. The Entity Framework Core can be used with .NET Core applications and .NET 4.6-Based (.NET Framework) Applications.
How to Install Entity Framework Core?
In this article, I will show you how to install Entity Framework Core in .NET Core applications using Visual Studio 2022. Once we understand the Entity Framework Core Basic Concepts, I will show you How to use Entity Framework Code First and Database First Approach in ASP.NET Core MVC Application.
Note: The steps that are going to be followed to Install Entity Framework Core are going to be the same irrespective of the type of .NET Core Application, such as Console, Class Library, MVC, Web API, etc.
The Entity Framework Core is not a part of the .NET Core and standard .NET framework. It is available as a NuGet package. We need to install the following two NuGet packages to use EF Core in our application:
- EF Core DB Provider
- EF Core Tools
Let’s install the above two NuGet Packages in the .NET Core Console application using Visual Studio 2022. First, open Visual Studio 2022 and click “Create a new Project”, as shown in the image below, to create a new .NET Console Application.
Then select the Console App, which is targeting the .NET Core, and then click on the Next button, as shown in the below image. As I am using Windows OS, I have filtered the language as C#, OS as Windows, and Application type as Console.
Once you click on the Next button, it will open the following Configure Your New Project window. Here, you need to provide the Project Name (I am providing the project name as EFCoreCodeFirstDemo), the location (I am creating in D:\EFCoreProjects) where you want to create the project, and the solution name (I am keeping the same as the project name) and then click on the Next button as shown in the below image.
Once you click on the Next button, it will open the Additional Information window. Here, select the Target .NET Framework. I am selecting .NET 6, and I don’t want to use the top-level statements, so I am checking the Do not use top-level statement checkbox and finally clicking on the Create button, as shown in the image below.
Once you click the Create button, it will create the Console Application using .NET 6 with the following structure.
As you can see, Entity Framework Core is not installed by default. So, let us proceed and try to understand How to Install Entity Framework Core in .NET Core Application.
Installing Entity Framework Core in .NET Core Application:
Entity Framework Core is not part of the .NET Core Framework. So, we need to install Entity Framework Core using NuGet packages to use Entity Framework. We need to install the following two NuGet packages to use EF Core in our .NET Core Application:
- EF Core DB Provider
- EF Core Tools
Installing EF Core DB Provider:
As discussed in our previous article, Entity Framework Core allows us to access databases via the provider model. Different Entity Framework Core Database providers are available for the different databases. Some of them are as follows:
Azure SQL and SQL Server 2012 onwards: Microsoft.EntityFrameworkCore.SqlServer
SQLite 3.7 onwards: Microsoft.EntityFrameworkCore.Sqlite
EF Core in-memory database: Microsoft.EntityFrameworkCore.InMemory
Oracle DB 11.2 onwards: Oracle.EntityFrameworkCore
For the complete list of EF Core Database Providers, please visit the following URL.
These providers are available as NuGet packages. So, we need to install the NuGet Package for the database provider we want to access. I will use Microsoft SQL Server as the backend database in this course. So, we need to install Microsoft.EntityFrameworkCore.SqlServer package from NuGet.
To Install Entity Framework Core using NuGet packages, Right click on the Project and then click on the Manage NuGet Packages option from the context menu, as shown in the image below. Alternatively, select Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution from the Visual Studio menus.
Once you select Manage NuGet Packages, it will open the NuGet Package Manager UI, as shown in the image below. Select the Browse Tab, then search for Microsoft.EntityFrameworkCore.SqlServer and then select Microsoft.EntityFrameworkCore.SqlServer Package and select the Framework Version that you want to Install. By default, the latest version will be selected, and currently, the latest version of Microsoft.EntityFrameworkCore.SqlServer package is 7.0.9 (it might be changed while you read this article). finally, click on the Install button as shown in the below image.
Once you click on the Install Button, the Preview Changes window will pop up, showing the list of packages it will install in your application. Review the changes and click the OK button, as shown in the image below.
Once you click on the OK button, it will open the License Acceptance pop-up. So, finally, accept the license terms associated with the packages that will be installed by clicking on the “I Accept” button, as shown in the image below.
Once you click on the I Accept button, it will install Microsoft.EntityFrameworkCore.SqlServer Package within the Packages folder, which you can find inside the Dependencies folder of your project, as shown in the below image. That means Microsoft.EntityFrameworkCore.SqlServer Package is installed.
Alternatively, you can install the provider’s NuGet Package using the Package Manager Console. Go to Tools -> NuGet Package Manager -> Package Manager Console and then execute the following command to install the Entity Framework Core SQL Server Provider package:
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
Note: If you want to use a different database with your application, install that provider-specific NuGet Package instead of Microsoft.EntityFrameworkCore.SqlServer database provider package.
For example, if you want to use MySQL as your database, install MySql.EntityFrameworkCore database provider package. Along the same lines, if you want to use PostgreSQL as your database, use NDevart.Data.PostgreSql.EFCore database provider package. For the complete list of EF Core Database Providers, please visit the following URL.
What is the Role of EF Core DB Provider?
When you work with Entity Framework Core (EF Core), the database provider acts as a bridge between the EF Core API and a particular database management system (DBMS). The primary role of the DB provider is to convert the operations you define in EF Core into commands that the underlying DBMS can understand because each DBMS has its own set of features, rules, syntax, and configurations. The following tasks are going to be performed by EF Core DB Provider.
- Query Translation: When you execute a LINQ query using EF Core, it doesn’t run as a LINQ query on objects in memory. Instead, EF Core converts the query to SQL (or another suitable query language for the selected DBMS). The DB provider is responsible for translating the query to ensure that the generated SQL is going to be understood and executed by the corresponding DBMS.
- Change Tracking: EF Core monitors entity changes to entities retrieved from the database and then generates appropriate SQL commands such as INSERT, UPDATE, and DELETE for persistence based on the Entity State. If the entity stated is Added, it will generate an INSERT SQL Statement, if the entity state is Modified, it will generate an UPDATE SQL Statement and if the entity state is Deleted, then it will generate the DELETE SQL Statement.
- Connection Management: The DB Provider also manages tasks involving database connections, including opening and closing connections as well as managing transactions.
- Migrations: With the Migrations feature of EF Core, we can easily manage and version our database schema. The database provider will translate the migration commands into SQL to modify the database schema ensuring that the generated SQL is suitable for the target database. If you are using the SQL Server Database Provider, then it will generate SQL Statements which are going to be understood by the SQL Server Database. Similarly, If you are using the MySQL Database Provider, then it will generate SQL Statements that are going to be understood by the MySQL Database.
- Database-Specific Features: Some databases have some features that aren’t common across all DBMSs. For example, the SQL Statement we write to get the TOP recorded in the SQL Server database is not the same as in the MySQL Database. So, to guarantee smooth integration with the wider EF Core API, the DB provider can give access to these exceptional features to EF Core.
- Scaffolding: EF Core can generate a code-first model by reverse-engineering a database. The DB provider reads the schema and data of the existing database to inform EF Core of its structure. So, basically, we need to use Scaffolding when we are going to work with the Entity Framework Core Database First Approach.
- Type Mapping: The EF Core DB provider manages the mapping between .NET types and the corresponding DBMS data types. For example, it will handle how a .NET DateTime is stored in a specific database’s date/time column type.
Installing Entity Framework Core Tools
Along with the Entity Framework Database provider package, we must install Entity Framework Core tools that provide a command line and .NET CLI tools that assist developers in tasks related to the Entity Framework. These tools simplify tasks like creating migrations, updating the database with migrations, querying the database, and more. If this is unclear now, don’t worry; we will understand these things practically in our upcoming articles.
In the same way, we need to install Microsoft.EntityFrameworkCore.Tools package. Go to NuGet Package Manager UI and search for Microsoft.EntityFrameworkCore.Tools package, then select Microsoft.EntityFrameworkCore.Tools package. Select the latest stable version and click the Install button in the image below. The latest stable package at the time of creating this content is 7.0.9.
Once you click on the Install Button, the Preview Changes window will pop up, showing the list of packages that will be installed. Simply click on the OK button as shown in the below image.
Once you click on the OK button, it will open the License Acceptance pop-up, and you need to click on the “I Accept” button, as shown in the image below.
Once you click on the I Accept button, it will install Microsoft.EntityFrameworkCore.Tools Package within the Packages folder, which you can find inside the Dependencies folder of your project. After successfully installing the packages, they can be verified from Solution Explorer under the Dependencies => Packages, as shown in the image below.
Alternatively, you can install the EF Core Tool Package using the Package Manager Console. Go to Tools -> NuGet Package Manager -> Package Manager Console and then execute the following command to install the Entity Framework Core Tool package:
PM> Install-Package Microsoft.EntityFrameworkCore.Tools
What is the Role of EF Core Tools?
Entity Framework Core (EF Core) Tools provide a set of command-line tools and .NET CLI tools that assist developers in tasks related to the Entity Framework. These tools simplify tasks like creating migrations, updating the database with migrations, querying the database, and more. The primary roles of EF Core Tools are as follows:
- Creating Migrations: Generate code files that represent the model changes, which can be applied to the database schema.
- Updating the Database: Apply migrations to update the database schema to the latest version of the migration.
- Removing Migrations: Remove the latest migration, allowing you to make additional changes before regenerating the migration.
- Generating SQL Scripts: Produce SQL scripts from migrations and this is useful when direct database updates are not feasible (e.g., in strict production environments).
Database Scaffolding: EF Core Tools can create code-first model files from an existing database. This is useful when we have an existing database and want to represent it as a code-first model in our application.
View DbContext Model: EF Core Tools enable creating a visual representation (DGML file) of DbContext to understand entity relationships and structure.
EF Core Tools are available as PowerShell commands (in the Visual Studio Package Manager Console) and as .NET CLI commands. Some commonly used commands are as follows:
PowerShell Commands (Package Manager Console in Visual Studio):
.NET CLI Commands:
- dotnet ef migrations add
- dotnet ef database update
- dotnet ef migrations remove
- dotnet ef dbcontext scaffold
To use EF Core Tools, developers typically install Microsoft.EntityFrameworkCore.Tools NuGet package for PowerShell commands and Microsoft.EntityFrameworkCore.Design package for .NET CLI commands.
So, we have installed the Required Entity Framework Core Packages into our .NET Core Console Application. Next, we need to understand the DbContext class.
In the next article, I will discuss the DbContext Class in Entity Framework Core with Examples. Here, in this article, I try to explain How to Install the Entity Framework Core in .NET Core Application using Visual Studio 2022. I hope you enjoy this Install the Entity Framework Core in the .NET Core Application article.
About the Author: Pranaya Rout
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.