DotNet Help

EF Core

Documentation/Resources

Official docs

Readme Presentation

YouTube Tutorial by Nick Chapsas

What is EF Core?

EF Core is a modern, open-source, and cross-platform Object-Relational Mapper (ORM) for .NET that eliminates the need to write complex SQL queries manually. It allows developers to work with databases using C# classes and LINQ instead of SQL.

EF Core acts as a bridge between .NET applications and databases, allowing developers to perform operations using object-oriented techniques.

Why EF Core?

1. Quick Story/Example

Imagine you're building a .NET application and need to interact with a database. How do you do it efficiently? You could write raw SQL queries, but that can be error-prone and hard to maintain. This is where an ORM (Object-Relational Mapper) like EF Core comes in handy.

2. How Do We Interact with Databases in .NET?

Before Entity Framework Core (EF Core), developers interacted with databases using ADO.NET, Dapper, or raw SQL queries. While these approaches provided control and performance, they often required writing a lot of boilerplate code for CRUD operations:

  • Open a database connection

  • Write SQL queries manually

  • Handle result mappings to objects

  • Manage transactions and exceptions explicitly

This process is repetitive and error-prone. This is where EF Core comes in.

3. Why Use EF Core?

a. Simplifies Data Access

EF Core abstracts database interactions, allowing developers to use C# objects instead of SQL queries.

b. Boosts Productivity
  • Eliminates the need to write repetitive SQL queries

  • Supports automatic migrations to handle database schema changes

  • Works seamlessly with LINQ queries for data retrieval

c. Supports Multiple Databases

EF Core is database-agnostic and supports multiple database providers, including:

  • SQL Server

  • PostgreSQL

  • MySQL

  • SQLite

  • Azure Cosmos DB

Setting Up EF Core

Step 1: Installing Database Provider

In Visual Studio, click on Tools → NuGet Package Manager and search for Microsoft.EntityFrameworkCore.InMemory and install it in your project.

This is an Entity Framework Core (EF Core) provider that allows you to use an in-memory database, mainly for testing or prototyping.

1753175412478.png

Limitations

  • No relational features (e.g., foreign keys, joins, transactions)

  • Doesn't always behave the same as real databases like SQL Server

  • Not suitable for production

We will later advance to use other database providers.

Step 2: Installing EF Core Diagnostics

Search for Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore and install it in your project.

It provides developer-friendly error pages for Entity Framework Core-related exceptions, especially during development, such as database errors, migration errors, and more.

This should only be used in development because it exposes detailed error information that could be a security risk in production.

1753175682543.png

When to Use

  • When you want better debugging support for EF Core issues during development

  • In educational or training environments, to help students understand why certain DB errors are happening

  • When you're building apps that heavily rely on EF Core and want improved developer feedback

We need to register our database provider in the Program.cs file, but first, let's learn about DbContext. 👇

What is DbContext?

DbContext is the main class in Entity Framework Core that manages database connections and is used to query and save data.

Think of this as a bridge between your C# code and your database.

What does it do?

  • Maps your C# classes (entities) to database tables

  • Allows you to query, insert, update, and delete data

  • Tracks changes to your data so it knows what to update in the database

Step 3: Create a DbContext Class

In the Models folder, add a new class and name it AppDbContext.cs

In the file, add the following:

using Microsoft.EntityFrameworkCore; namespace _0.EventApi.Models { public class AppDbContext : DbContext { public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } public DbSet<Event> Events => Set<Event>(); } }
1753176789869.png

Breakdown

The AppDbContext class is a central part of Entity Framework Core (EF Core). It represents a session with the database, allowing you to perform CRUD (Create, Read, Update, Delete) operations using C# classes.

Purpose

  • Connects the application to the database

  • Maps your models (e.g., Event) to database tables

  • Enables querying and saving of data

Term

Description

DbContext

Base class provided by EF Core. It handles database operations and change tracking.

AppDbContext

Our custom context class inheriting from DbContext. We define models here as DbSet<>.

AppDbContext(DbContextOptions <AppDbContext> options)

Constructor used to configure the context with settings like the connection string.

DbSet <Event> Events

Represents the Events table in the database. You use this to access and query event records.

Set <Event> ()

Built-in EF Core method that initializes the DbSet for a given model type.

Step 4: Register the Context in Program.cs

Add the following:

builder.Services.AddDbContext<AppDbContext>(options => options.UseInMemoryDatabase("EventDb")); builder.Services.AddDatabaseDeveloperPageExceptionFilter();
1753177804536.png

The first line registers the AppDbContext with the application's dependency injection (DI) container and configures it to use an in-memory database called "EventDb".

The second line adds a special developer-friendly error page that shows detailed database-related exceptions when running the app in development mode.

  • Helps you see detailed errors if something goes wrong with EF Core (e.g., migrations or invalid queries)

  • Makes debugging database issues easier during development

  • Only shows detailed errors when ASPNETCORE_ENVIRONMENT=Development

This usage is possible because it is made available by the Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore package.

DbContext Flow

1753177840347.png
07 August 2025