EF Core - Installation

Entity Framework Core (EF Core) is an essential library for .NET developers who want to interact with databases using an object-oriented approach. This guide will help you install EF Core in your .NET projects.


1. Prerequisites

Before installing EF Core, ensure you have the following prerequisites:


2. Creating a .NET Project

Start by creating a new .NET project. This example will use a console application, but you can use any .NET project type.

        
            
dotnet new console -n EFCoreDemo
        
    

This command creates a new console application named EFCoreDemo.


3. Installing EF Core Packages

EF Core requires several NuGet packages to function correctly. You can install them using the .NET CLI or the NuGet Package Manager in Visual Studio.

The essential packages are:

Use the following commands to install these packages via the .NET CLI:

        
            
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
        
    

4. Adding EF Core to Your Project

Once the packages are installed, you can start using EF Core by configuring a DbContext and defining your entity classes.

        
            
public class ApplicationDbContext : DbContext
{
    public DbSet<Product> Products { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("YourConnectionStringHere");
    }
}
        
    

5. Verifying the Installation

Verify that EF Core is installed and functioning correctly by running the following command, which lists installed packages:

        
            
dotnet list package
        
    

You should see the EF Core packages listed among the installed packages.


Summary

By following these steps, you have successfully installed EF Core in your .NET project. You are now ready to configure your data model and start interacting with your database using EF Core.