0% found this document useful (0 votes)
40 views

2.how To Install Entity Framework Core

The document discusses how to install Entity Framework Core in .NET Core applications using Visual Studio. It covers installing the EF Core database provider and tools packages via NuGet and their roles in enabling database access and migrations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

2.how To Install Entity Framework Core

The document discusses how to install Entity Framework Core in .NET Core applications using Visual Studio. It covers installing the EF Core database provider and tools packages via NuGet and their roles in enabling database access and migrations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

How to Install Entity Framework Core

 In this article, I will show you how to install Entity Framework Core in .NET
Core applications using Visual Studio 2022.
 Note: The steps that are going to be followed to Install Entity Framework
Core are going to be the same irrespective of the type of .NET Core
Application, such as Console, Class Library, MVC, Web API, etc.

 The Entity Framework Core is not a part of the .NET Core and standard .NET
framework. It is available as a NuGet package. We need to install the
following two NuGet packages to use EF Core in our application:

1. EF Core DB Provider (example => sqlserver)

2. EF Core Tools (example => Tools/Design)


Installing EF Core DB Provider

1. As discussed in our previous article, Entity Framework Core allows us to


access databases via the provider model.

2. Different Entity Framework Core Database providers are available for the
different databases. Some of them are as follows:

3. Ad
4. Ad
5. These providers are available as NuGet packages. So, we need to install the
NuGet Package for the database provider we want to access.

6. I will use Microsoft SQL Server as the backend database in this course. So,
we need to install Microsoft.EntityFrameworkCore.SqlServer package from
NuGet.

7. To Install Entity Framework Core using NuGet packages, select Tools ->
NuGet Package Manager -> Manage NuGet Packages for Solution from the
Visual Studio menus.
8. Once you select Manage NuGet Packages, it will open the NuGet Package
Manager UI, as shown in the image below. Select the Browse Tab, then
search for Microsoft.EntityFrameworkCore.SqlServer and then
select Microsoft.EntityFrameworkCore.SqlServer Package and select the
Framework Version that you want to Install. By default, the latest version
will be selected,

9. Once you click on the Install Button, the Preview Changes window will pop
up, showing the list of packages it will install in your application. Review the
changes and click the OK button,
10.Once you click on the OK button, it will open the License Acceptance pop-
up. So, finally, accept the license terms associated with the packages that
will be installed by clicking on the “I Accept” button
11.Once you click on the I Accept button, it will
install Microsoft.EntityFrameworkCore.SqlServer Package within the
Packages folder, which you can find inside the Dependencies folder of your
project, as shown in the below image. That means
Microsoft.EntityFrameworkCore.SqlServer Package is installed.
Alternatively, you can install the provider’s NuGet Package using the Package Manager
Console. Go to Tools -> NuGet Package Manager -> Package Manager Console and then
execute the following command to install the Entity Framework Core SQL Server
Provider package:
Ad
Ad
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer

Note: If you want to use a different database with your application, install that provider-
specific NuGet Package instead of Microsoft.EntityFrameworkCore.SqlServer database
provider package.

For example, if you want to use MySQL as your database, install


MySql.EntityFrameworkCore database provider package.

And , if you want to use PostgreSQL as your database, use


NDevart.Data.PostgreSql.EFCore database provider package. For the complete list of EF
Core Database Providers, please visit the following URL.

https://learn.microsoft.com/en-us/ef/core/providers/?tabs=dotnet-core-cli
Installing Entity Framework Core Tools

1. Along with the Entity Framework Database provider package, we must


install Entity Framework Core tools that provide a command line
and .NET CLI tools that assist developers in tasks related to the Entity
Framework.

2. These tools simplify tasks like creating migrations, updating the database
with migrations, querying the database, and more.

3. In the same way, we need to


install Microsoft.EntityFrameworkCore.Tools package. Go to NuGet
Package Manager UI and search
for Microsoft.EntityFrameworkCore.Tools package, then
select Microsoft.EntityFrameworkCore.Tools package. Select the latest
stable version and click the Install button in the image below
4. Ad

5. Once you click on the Install Button, the Preview Changes window will
pop up, showing the list of packages that will be installed. Simply click on
the OK button as shown in the below image.
6. Once you click on the OK button, it will open the License Acceptance
pop-up, and you need to click on the “I Accept” button, as shown in the
image below.
7. Ad
8. Once you click on the I Accept button, it will
install Microsoft.EntityFrameworkCore.Tools Package within the
Packages folder, which you can find inside the Dependencies folder of
your project. After successfully installing the packages, they can be
verified from Solution Explorer under the Dependencies => Packages, as
shown in the image below.
9.

Alternatively, you can install the EF Core Tool Package using the Package Manager
Console. Go to Tools -> NuGet Package Manager -> Package Manager Console and then
execute the following command to install the Entity Framework Core Tool package:

PM> Install-Package Microsoft.EntityFrameworkCore.Tools


What is the Role of EF Core => DB Provider?

The Entity Framework Core (EF Core) database provider serves as a mediator
between EF Core and the database management system (DBMS) being used. Its main
responsibilities include:

1. Query Translation: Converts LINQ queries into database-specific SQL queries


for execution.

2. Change Tracking: Monitors changes to entities and generates corresponding


SQL commands (INSERT, UPDATE, DELETE) based on entity state changes.

3. Connection Management: Handles opening, closing, and managing database


connections, as well as transactions.

4. Migrations: Manages database schema changes through EF Core's migration


feature, translating migration commands into database-specific SQL
statements.

5. Database-Specific Features: Provides access to DBMS-specific features to


ensure compatibility with EF Core.

6. Scaffolding: Generates code-first models by reverse-engineering an existing


database schema.

7. Type Mapping: Manages mapping between .NET types and DBMS data types.

In essence, the DB provider ensures that EF Core can communicate effectively with
the underlying database, handling translation of commands, managing connections,
and providing access to database-specific features.
What is the Role of EF Core => Tools?

Entity Framework Core (EF Core) Tools provide a set of command-line tools
and .NET CLI tools that assist developers in tasks related to the Entity
Framework.

These tools simplify tasks like creating migrations, updating the database with
migrations, querying the database, and more. The primary roles of EF Core
Tools are as follows:

Database Migrations:

 Creating Migrations: Generate code files that represent the model


changes, which can be applied to the database schema.
 Updating the Database: Apply migrations to update the database
schema to the latest version of the migration.
 Removing Migrations: Remove the latest migration, allowing you
to make additional changes before regenerating the migration.
 Generating SQL Scripts: Produce SQL scripts from migrations and
this is useful when direct database updates are not feasible (e.g.,
in strict production environments).

Database Scaffolding: EF Core Tools can create code-first model files from an
existing database. This is useful when we have an existing database and want
to represent it as a code-first model in our application.
Ad
View DbContext Model: EF Core Tools enable creating a visual representation
(DGML file) of DbContext to understand entity relationships and structure.

EF Core Tools are available as PowerShell commands (in the Visual Studio
Package Manager Console) and as .NET CLI commands.
Some commonly used commands are as follows:

PowerShell Commands (Package Manager Console in Visual Studio):


1. Add-Migration
2. Update-Database
3. Remove-Migration
4. Scaffold-DbContext

.NET CLI Commands:


1. dotnet ef migrations add
2. dotnet ef database update
3. dotnet ef migrations remove
4. dotnet ef dbcontext scaffold

To use EF Core Tools, developers typically


install Microsoft.EntityFrameworkCore.Tools NuGet package for PowerShell
commands

and Microsoft.EntityFrameworkCore.Design package for .NET CLI


commands.
Remember

Both Microsoft.EntityFrameworkCore.Design and Microsoft.EntityFrameworkCore.Tools


are used for generating migrations and scaffolding a DbContext,
but they are used in different environments.

is used in Visual Studio for design-time


Microsoft.EntityFrameworkCore.Design

operations, while Microsoft.EntityFrameworkCore.Tools is used with the .NET


CLI for command-line operations.

Example :

1. Package Manager Console (Visual Studio):

 Command: Add-Migration
 Package: Microsoft.EntityFrameworkCore.Design
 Example: Add-Migration InitialCreate

2. .NET CLI (Command Line):

 Command: dotnet ef migrations add


 Package: Microsoft.EntityFrameworkCore.Tools
 Example: dotnet ef migrations add InitialCreate

Both commands achieve the same result of adding a new migration


in Entity Framework Core. The difference is in the environment and
the syntax used to execute the command.
Certainly! Here are the commands with examples:

1. Add a Migration:

 PMC: Add-Migration InitialCreate


 .NET CLI: dotnet ef migrations add InitialCreate

2. Apply Migrations to the Database:

 PMC: Update-Database
 .NET CLI: dotnet ef database update

3. Remove the Last Migration:

 PMC: Remove-Migration
 .NET CLI: dotnet ef migrations remove

4. Reverse a Migration:

 PMC: Not directly supported


 .NET CLI: dotnet ef migrations remove

5. Generate a SQL Script from Migrations:

 PMC: Not directly supported


 .NET CLI: dotnet ef migrations script

6. Scaffold a DbContext from an Existing Database:

 PMC: Scaffold-DbContext
"Server=myServerAddress;Database=myDataBase;Trusted_Conne
ction=True;" Microsoft.EntityFrameworkCore.SqlServer -
OutputDir Models
 .NET CLI: dotnet ef dbcontext scaffold
"Server=myServerAddress;Database=myDataBase;Trusted_Conne
ction=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models

These examples demonstrate how to use the Package Manager


Console (PMC) commands in Visual Studio and the .NET CLI
commands to perform Entity Framework Core migrations and
database operations.

You might also like