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 default 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 dependent 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 different database, then you can install related NuGet package in the same way.
You can install EF Core NuGet package in your project using the following ways:
To install EF Core 6/7 package using NPM, go to the Project menu and click on “Manage NuGet Packages..â€.
This will open Nuget Package Manager, as shown below.
Click on the Browser 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 project.
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 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 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 EF Core package in different ways as per your convenience.
After installing EF Core DB provider for SQL Server, let’s create entities next.