Javatpoint Logo
Javatpoint Logo

Entity Framework Core

Entity Framework Core can be used with .NET core or .NET 4.6 based applications. Here we will learn how to install and use the Entity Framework core 2.0 in .NET Core applications using visual studio 2017.

Entity Framework Core is not a part of the .NET core and standard .NET framework. It is available as a NuGet package. Here we need to install the NuGet packages for the following two things to use Entity Framework Core in our application.

  1. Entity Framework Core DB provider
  2. Entity Framework Core tools

Let us install the above NuGet packages in the .NET Core Console application in Visual Studio 2017.

Install Entity Framework Core DB provider

Entity Framework Core allows us to access the database via the provider model. There are different Entity Framework Core DB providers available for the various databases. These providers are available as NuGet packages.

First, we need to install the NuGet package for the provider of the database we want to access. Here we want to access MS SQL Server Database, so we need to install Microsoft.EntityFrameworkCore.SqlServer NuGet package.

To install the DB provider NuGet package, right-click on the project in the Solution Explorer in Visual Studio and select Manage NuGet Packages.. (or Select on the Menu: Tools-> NuGet Package Manager->Manage NuGet Packages For Solution).

Entity Framework Core

This will open the UI of NuGet Package Manager. Now we have to click on the Browse or the Update tabs and search for the Microsoft.entityframeworkcore in the search box at the top left corner.

Entity Framework Core

Choose the provider package for the database that we want to access. In this case, select Microsoft.EntityFrameworkCore.SqlServer for MS SQL Server. We have to be sure that it has .NET symbol, and the author is Microsoft. Click on the install button to start the installation.

Popup will display the list of packages that we are going to install in our application. Now review the changes and click on ok.

Entity Framework Core

In the end, accept the terms of the license associated with the package that we are going to install.

Entity Framework Core

This will install the Microsoft.EntityFrameworkCore.SqlServer package. We will verify it in the Dependencies->NuGet, as shown in the below screenshot.

Entity Framework Core

Here we will also see that provider NuGet package also installed another package such as Microsoft.EntityFrameworkCore.Relational and System.Data.SqlClient.

Note: We can also install the provider's NuGet package using the Package Manager Console. For that, we have to follow the following steps:

Go to Tools->NuGet Package Manager->Package Manager Console and execute the following commands to install the SQL Server Provider Package.

Install Entity Framework Core Tools

Along with the Database provider package, we also need to install Entity Framework tools to execute the Entity Framework Core commands. This makes it easier to perform different Entity Framework Core-related tasks in our project at the time of the design, such as scaffolding, migrations, etc.

Entity Framework tools are available as NuGet packages. We can install the NuGet package for Entity Framework tools, which depend on where we want to execute the commands: either by using the Package Manager Console (Power Shell version of Entity Framework Core command) or using the dotnet CLI.

Install Entity Framework Core tools for PMC

In order to execute the Entity Framework command from Package Manager Console, search for the Microsoft.EntityFrameworkCore.Tools package from NuGet UI and install it as shown in the below screenshot.

Entity Framework Core

This will allow us to execute the Entity Framework Core commands for scaffolding, migration, etc. from the Package Manager Console (PMC) within the Visual Studio.

Install Entity Framework Core tools for Dot net CLI

If we want to execute the Entity Framework Core command from .NET Core's CLI (Command Line Interface), first install the NuGet package Microsoft.EntityFrameworkCore.Tools.Dotnet using NuGet UI. After the installation of Microsoft.EntityFrameworkCore.Tools.DotNet package, now we will edit the .csproj file by right-clicking on the project in the solution explorer and select Edit .csproj. Now add the node as shown below. This is an extra step we need to perform to execute the Entity Framework Core 2.0 commands from DotNet CLI in Visual Studio 2017.

Now we will open the command prompt(or terminal) from the root folder or our project and execute the entity framework commands from the CLI where we will start from the dotnet ef as shown in the below screenshot.

Entity Framework Core




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA