In this Video, I am going to discuss How to Install Entity Framework Core in Visual Studio step by step in different types of applications. Please read our previous Video where we discussed the introduction part of Entity Framework Core.
How to Install Entity Framework Core in Visual Studio. The Complete ASP.NET Core Developer Course 2023 [Videos].
Note: The Entity Framework Core can be used with .NET Core applications as well as .NET 4.6 based applications. Here, in this Video, I will show you how to install EF Core in .NET Core applications using Visual Studio 2017.
The Entity Framework Core is not a part of .NET Core and standard .NET framework. It is available as a NuGet package. Depending on how you have set your project, you may have already installed the Entity Framework Core.
Depending on the project complexity or requirement, you can develop a web application as:
- Single Layer Web Application
- Multi-Layer Web Application
Single Layer Web Application
When you develop a small project, then you may have the presentation, business and data access layer all in one project. So when we created a web application project using ASP.NET Core 2.1 or higher, then by default the Entity Framework Core already installed.
Entity Framework Core in ASP.NET Core Web Application Project
When we create an ASP.NET Core Web application using ASP.NET Core 2.1 or higher version, then by default the following NuGet package installed.
The above package is called a Meta package. A Meta package does not hold any content of its own rather it holds a list of other packages. You can find this Meta package, in the Solution Explorer under the NuGet section. When you expand the Meta package, then you can find all the dependencies i.e. other packages. In the dependencies, once you scrolled down then you will find the Entity Framework Core NuGet packages already installed as shown in the below image.
So the point that you need to remember is, when you create an ASP.Net Core web application using ASP.NET Core Version 2.1 or later, then by default you will have Entity Framework Core installed as part of the meta-package.
Multi-Layer Web Application:
When we want to develop a large application, we usually have at least the following 3 layers
- Presentation Layer
- Business Logic Layer
- Data Access Layer
These layers are implemented as separate projects. And the Entity Framework Core is usually required in the Data Access Layer project as this layer is going to interact with the database. The Data Access Layer project is usually a class library project and a class library project by default does not usually have the meta-package referenced. As it does not have the meta-package installed, so this means, the Entity Framework Core is not installed for the Data Access Layer project.
In order to install the Entity Framework Core to interact with the SQL server database, you need to install the following NuGet packages.
- Microsoft.EntityFrameworkCore.SqlServer has a dependency on Microsoft.EntityFrameworkCore.Relational package.
- Similarly, Microsoft.EntityFrameworkCore.Relational package has a dependency on Microsoft.EntityFrameworkCore package.
- Microsoft.EntityFrameworkCore package has a dependency on several other packages.
Note: When you install Microsoft.EntityFrameworkCore.SqlServer package, then it also automatically installs all the other dependency NuGet packages.
Installing Entity Framework Core in the Class Library Project:
Create a class library Project. Once you create the class library project expand the dependencies node as shown below.
As you can see in the above image, we dont have the meta-package installed and hence we dont have the entity framework core installed in our application.
In order to install entity framework core, Right-click on the “Dependencies“ node in Solution Explorer and then select “Manage NuGet Packages“ from the context menu as shown in the below image.
This will open the NuGet Package Manager interface. Click on the Browse tab and search for Microsoft.EntityFrameworkCore.SqlServer in the search box at the top left corner as shown in the below image. Based on the asp.net core version installed the entity framework core version compatibility with your .net core framework.
Note: We want to install Entity Framework Core to interact with the SQL Server database, so we installed the Microsoft.EntityFrameworkCore.SqlServer package. This package is usually called the Database provider package.
If you are using a different database other than SQL server, then you need to install that database provider-specific NuGet package. For example, if you want to use MySQL as your database, then install Pomelo.EntityFrameworkCore.MySql database provider package. Along the same lines, if you want to use PostgreSQL as your database, then use Npgsql.EntityFrameworkCore.PostgreSQL database provider package.
You can find all provider-specific NuGet packages on the following MSDN page.
When you are installing the entity framework it will ask you to accept the license terms associated with the packages that are going to be installed as shown in the image below. Accept the license.
Once the entity framework core installed, you will find the packages in the Meta package as shown in the below image.
Alternatively, you can also use the Package Manager Console to install the Entity Framework Core package. Go to Tools -> NuGet Package Manager -> Package Manager Console and execute the following command to install the SQL Server provider package.
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
In the next Video, I am going to discuss the DbContext class in Entity Framework Core. Here, in this Video, I try to explain how to install the entity framework core.