Here we will install Entity Framework Core 7 in the .NET 7 (also known as .NET Core 7) console application. EF Core 7 is the latest version of Entity Framework as of this writing. You can install EF Core 6.0 which has a long-term support in the same way.
EF Core 6/7 is not included as a built-in package in .NET 7. We need to install it via the NuGet package.
The Microsoft.EntityFrameworkCore
is the base package for all the basic operations of EF Core.
However, you have to install a database provider package from NuGet for the database you use in your project.
For example, to use the MS SQL Server database, you need to install Microsoft.EntityFrameworkCore.SqlServer
package.
The database provider package of EF Core includes all the other dependency packages it needs. So, it includes Microsoft.EntityFrameworkCore
too. So, no need to install it separately.
EF Core supports the following database providers.
Database | DB Provider Package |
---|---|
MS SQL Server | Microsoft.EntityFrameworkCore.SqlServer |
SQLite | Microsoft.EntityFrameworkCore.Sqlite |
Cosmos | Microsoft.EntityFrameworkCore.Cosmos |
In-Memory | Microsoft.EntityFrameworkCore.InMemory |
MySQL | MySql.EntityFrameworkCore https://www.nuget.org/packages/MySql.EntityFrameworkCore/ |
PostgreSQL | Npgsql.EntityFrameworkCore.PostgreSQL |
Here we will use the local SQL Server database in the following .NET 7 console application, so we will install the Microsoft.EntityFrameworkCore.SqlServer
NuGet package. If you use a different database, then you can install the related NuGet package in the same way.
You can install the EF Core NuGet package in your project using the following methods:
To install EF Core 6/7 package using NuGet Package Manager, go to the Project menu and click on “Manage NuGet Packages..â€.
This will open NuGet Package Manager, as shown below.
Click on the Browse tab and search for "entityframeworkcore" in the search box. It will display all the packages related to EF Core, as shown below.
We are going to use SQL Server for our project, so select the Microsoft.EntityFrameworkCore.SqlServer
package and click on the Install button in the right pane.
Accept the license agreement to install the package in your project. Once installed, you will see the package is listed as a dependency package in the solution explorer, as shown below.
You can also see it in the .csproj file by double-clicking on your project name “EFCoreTutorialsConsoleâ€.
EF Core 6/7 can also be installed using Package Manager Console. To open the Package Manager Console. Go to Tools > NuGet Package Manager > Package Manager Console.
This will open the package manager console, as shown below.
Use the Install-Package command followed by the package name to install any NuGet package in your project. Make sure that it points to a project where you want to install the package.
To install the EF Core database provider for SQL Server, execute the following command:
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 7.0.11This will install the EF Core SQL Server provider and all its dependencies to your project.
To install EF Core using the .NET CLI (Command Line Interface), open your terminal or command prompt and navigate to your project directory. This should be the directory that contains your project's .csproj file.
Install the EF Core package for SQL Server using the following command.
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 7.0.11This will install the package in your project.
To install EF Core DB provider for SQL Server, open the PowerShell terminal in Visual Studio 2022 by right-clicking on the project and clicking “Open in Terminal†from the context menu. It will point to your project directory by default, as shown below.
You can use .NET CLI command here. Use the dotnet add package command to install any NuGet package in your project. To install EF Core DB provider for SQL Server, execute the following command.
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 7.0.11This will install the package in your project.
You can simply add the package reference in your .csproj file. Double-click on your project name in Visual Studio to open .csproj file.
Now, add an ItemGroup
element after PropertyGroup
. In the ItemGroup
element, add a PackageReference
element and specify the PackageId as "Microsoft.EntityFrameworkCore.SqlServer"
and specify the Version
attribute to the desired version, in this case "7.0.11", as shown below.
Save the file and you will see that the package is automatically added to your project dependencies.
Thus, you can install the EF Core package in different ways as per your convenience.
After installing EF Core DB provider for SQL Server, let’s create entities next.