Entity Framework Core (EF Core) is an object–relational mapper (ORM) used in the .NET ecosystem, allowing database operations to be performed with an object-oriented approach. With EF Core, database tables can be managed through C# classes without writing raw SQL queries. In this article, we’ll cover the basics of EF Core, including DbContext, Entity definitions, and basic CRUD operations. (We will not dive into MS-SQL specifics, only the general EF Core approach.)


Advantages of EF Core

  • ORM Approach: Perform database operations using C# code instead of SQL.
  • Type Safety: LINQ queries are validated at compile time.
  • Portability: EF Core supports many database providers (SQLite, PostgreSQL, MySQL, etc.).
  • Code-First Development: Database tables can be created directly from C# classes.

Defining an Entity

In EF Core, a database table is usually represented by a C# class. Properties of the class map to table columns.


public class Product
{
    public int Id { get; set; }      // Primary key
    public string Name { get; set; } = string.Empty;
    public decimal Price { get; set; }
}

The DbContext Class

DbContext is the core of EF Core. It manages the database connection and interactions with tables.


using Microsoft.EntityFrameworkCore;

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

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // Configure the database provider here
        optionsBuilder.UseInMemoryDatabase("TestDb");
    }
}

Note: For simplicity, this example uses InMemoryDatabase.


Basic CRUD Operations

With EF Core, Create, Read, Update, Delete operations can be performed easily.


using (var db = new AppDbContext())
{
    // CREATE
    db.Products.Add(new Product { Name = "Laptop", Price = 1500 });
    db.SaveChanges();

    // READ
    var products = db.Products.ToList();
    foreach (var p in products)
        Console.WriteLine($"{p.Id} - {p.Name} - {p.Price}");

    // UPDATE
    var product = db.Products.First();
    product.Price = 1400;
    db.SaveChanges();

    // DELETE
    db.Products.Remove(product);
    db.SaveChanges();
}

Querying with LINQ

EF Core integrates seamlessly with LINQ (Language Integrated Query). This allows writing strong, type-safe queries without raw SQL.


// Select products more expensive than 1000
using (var db = new AppDbContext())
{
    var expensive = db.Products
                      .Where(p => p.Price > 1000)
                      .OrderBy(p => p.Name);

    foreach (var item in expensive)
        Console.WriteLine(item.Name);
}

The Concept of Migrations

EF Core provides Migrations to create or update the database schema directly from code. Code changes are tracked and applied to the database with migrations.


// Example commands (run from CLI)
// dotnet ef migrations add InitialCreate
// dotnet ef database update

TL;DR

  • EF Core simplifies database operations with the ORM approach.
  • DbContext: Manages the database connection and tables.
  • Entity: C# classes representing tables.
  • CRUD operations (Create, Read, Update, Delete) are straightforward.
  • LINQ provides type-safe queries.
  • Migrations allow managing the database schema from code.