Advertisement
Intermediate Entity Framework

Entity Framework Core Guide

Learn how to use Entity Framework Core to map C# classes to database tables, manage migrations, define relationships, and query data using LINQ.

Advertisement

What is EF Core?

Entity Framework Core is Microsoft's modern object-relational mapper for .NET. It enables developers to work with databases using C# classes instead of SQL scripts.

Code First Workflow

With Code First, you define C# model classes and EF Core generates the database schema from them. This is ideal for rapid development and domain-driven design.

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; } = string.Empty;
    public decimal Price { get; set; }
    public int CategoryId { get; set; }
    public Category? Category { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public string Title { get; set; } = string.Empty;
    public ICollection Products { get; set; } = new List();
}

DbContext

The DbContext is the bridge between your models and the database. Register it in Program.cs with a provider such as SQL Server or SQLite.

public class ShopContext : DbContext
{
    public ShopContext(DbContextOptions<ShopContext> options)
        : base(options)
    {
    }

    public DbSet<Product> Products { get; set; } = null!;
    public DbSet<Category> Categories { get; set; } = null!;
}

// Program.cs
builder.Services.AddDbContext<ShopContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

Migrations

EF Core migrations apply schema changes to the database safely. Use the CLI to add migrations and update the database.

dotnet ef migrations add InitialCreate
dotnet ef database update

Migrations are stored in code, so you can review the generated schema changes and keep them in source control.

Relationships

Define relationships with navigation properties and foreign keys. EF Core supports one-to-many, many-to-many, and one-to-one mappings.

modelBuilder.Entity<Product>()
    .HasOne(p => p.Category)
    .WithMany(c => c.Products)
    .HasForeignKey(p => p.CategoryId);

Querying

Use LINQ to query the database. EF Core translates LINQ expressions into SQL and materializes the results into model objects.

var products = await _context.Products
    .Include(p => p.Category)
    .Where(p => p.Price > 50)
    .OrderBy(p => p.Name)
    .ToListAsync();

Performance Tips

  • Use AsNoTracking() for read-only queries.
  • Use Include() only when needed to avoid over-fetching.
  • Apply filtering and paging on the database side.
  • Use explicit transactions for batch updates.

Summary

EF Core maps C# classes to database tables using the Code First approach.

DbContext is the central API for querying and saving data.

Migrations evolve your database schema in a repeatable way.

Proper relationships and querying patterns keep applications maintainable and efficient.

Advertisement
← Previous: JWT Authentication Back to Tutorials →

Related Tutorials

JWT Authentication in ASP.NET Core

Secure your Web API with JSON Web Tokens.

Building RESTful Web APIs

Design APIs with proper HTTP methods and status codes.

Middleware & Request Pipeline

Learn how ASP.NET Core processes requests through middleware.