Tuesday 1 January 2019

Install Entity Framework Core

Entity Framework Core can be used with .NET Core or .NET 4.6 based applications. Here, you will learn to install and use Entity Framework Core 2.0 in .NET Core Console application using Visual Studio 2017.
EF Core is not a part of .NET Core or standard .NET framework. It is available as a NuGet package. You need to install NuGet packages for the following two things to use EF Core in your application:
  1. EF Core DB provider
  2. EF Core tools
Let's install the above NuGet packages in the .NET Core console application in Visual Studio 2017.

Install EF Core DB Provider

As mentioned in the previous chapter, EF Core allows us to access databases via the provider model. There are different EF Core DB providers available for the different 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).
This will open NuGet Package Manager UI. Click on the Browse or the Updates tab and search for Microsoft.entityframeworkcore in the search box at the top left corner, as shown below.
Choose the provider package for the database you want to access. In this case select Microsoft.EntityFrameworkCore.SqlServer for MS SQL Server as shown above. (make sure that it has the .NET symbol and the Author is Microsoft). Click Install to start the installation.
The preview popup displays the list of packages it is going to install in your application. Review the changes and click OK.
Finally, accept the license terms associated with the packages that are going to be installed.
This will install the Microsoft.EntityFrameworkCore.SqlServer package. Verify it in Dependencies -> NuGet, as shown below.
Notice that the provider NuGet package also installed other dependent packages such as Microsoft.EntityFrameworkCore.Relational and System.Data.SqlClient.
Alternatively, you can also install provider's NuGet package using Package Manager Console. Go to Tools -> NuGet Package Manager -> Package Manager Console and execute the following command to install SQL Server provider package:
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer

Install EF Core Tools

Along with the DB provider package, you also need to install EF tools to execute EF Core commands. These make it easier to perform several EF Core-related tasks in your project at design time, such as migrations, scaffolding, etc.
EF Tools are available as NuGet packages. You can install NuGet package for EF tools depending on where you want to execute commands: either using Package Manager Console (PowerShell version of EF Core commands) or using dotnet CLI.

Install EF Core Tools for PMC

In order to execute EF Core commands from Package Manager Console, search for the Microsoft.EntityFrameworkCore.Tools package from NuGet UI and install it as shown below.
This will allow you to execute EF Core commands for scaffolding, migration etc. directly from Package Manager Console (PMC) within Visual Studio.

Install EF Core Tools for dotnet CLI

If you want to execute EF Core commands from .NET Core's CLI (Command Line Interface), first install the NuGet package Microsoft.EntityFrameworkCore.Tools.DotNet using NuGet UI.
After installing Microsoft.EntityFrameworkCore.Tools.DotNet package, edit the .csproj file by right clicking on the project in the Solution Explorer and select Edit <projectname>.csproj. Add <DotNetCliToolReference> node as shown below. This is an extra step you need to perform in order to execute EF Core 2.0 commands from dotnet CLI in VS2017.
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
  </ItemGroup>
</Project>
Now, open the command prompt (or terminal) from the root folder of your project and execute EF Core commands from CLI starting with dotnet ef, as shown below.

Thus, you can install required packages for EF Core 2.0 to get started.

No comments:

Post a Comment