From Microsoft Learn / Entity Framework / Entity Framework Core

Abstract

Entity Framework (EF) Core is a data access technology that can serve as an object-relational mapper (OR/M). It supports various database providers.

Data access is performed using a model that includes entity classes and a context object that represents a session with a database.

An entity is a database.
An entity set is a table in the database.

Model development approaches

  1. Generate a model from an existing database
  2. Hand code a model to match a database
  3. Use EF Migrations to create a database from a model

Installing

Install EF Core w/SQL Server Database Provider

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Install .NET Core CLI tools

dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design

Install Package Manager Console tools

dotnet add package Microsoft.EntityFrameworkCore.Tools

Performing Migrations

via .NET Core CLI

dotnet-ef migrations add InitialCreate
dotnet-ef database update

via Package Manager Console

Add-Migration InitialCreate
Update-Database

This creates:

  • Migrations/EmployeeManagerDbContextSnapshot — a snapshot of the current database state
  • Migrations/###_InitialCreate.cs — contains:
    • Up method — creates the database tables
    • Down method — drops the tables

Confirm the database exists

Visual Studio > View > SQL Server Object Explorer

Database Providers

Database SystemPackage
SQL Server & SQL AzureMicrosoft.EntityFrameworkCore.SqlServer
SQLiteMicrosoft.EntityFrameworkCore.Sqlite
Azure Cosmos DBMicrosoft.EntityFrameworkCore.Cosmos
PostgreSQLMicrosoft.EntityFrameworkCore.PostgreSQL
MySQLMicrosoft.EntityFrameworkCore.MySql
In-memory databaseMicrosoft.EntityFrameworkCore.InMemory