Get Started with Entity Framework Core

Entity Framework Core (EF Core) is a modern object-relational mapper (ORM) for .NET that allows developers to work with databases using .NET objects and LINQ. It eliminates the need for most of the data-access code that developers traditionally need to write.

1. Installation

To get started, you'll need to install the necessary EF Core NuGet packages. The most common ones are:

dotnet add package Microsoft.EntityFrameworkCore

If you are using a specific database provider, install its corresponding package:

For tooling support (like migrations), you'll also need:

dotnet add package Microsoft.EntityFrameworkCore.Tools

2. Creating Your First Model and Context

EF Core works by mapping your .NET classes (entities) to database tables. You define these entities and then create a DbContext class to represent your database session.

Example Entity:

public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } }

Example DbContext:

using Microsoft.EntityFrameworkCore; public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { // Replace with your actual connection string optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=BloggingEFCore;Trusted_Connection=True;"); } }
Tip: The DbContextOptionsBuilder can also be configured using dependency injection in ASP.NET Core applications, which is the recommended approach.

3. Core Concepts

Migrations

Migrations are a way to incrementally update your database schema to match your EF Core model. EF Core can generate SQL scripts to perform these updates.

To add a new migration:

dotnet ef migrations add InitialCreate

To apply the migration to your database:

dotnet ef database update

Migrations are crucial for managing database changes over time in a consistent and reproducible manner.

Queries

You can query your data using LINQ to Objects syntax. EF Core translates these LINQ queries into SQL.

using var db = new BloggingContext(); var blogs = db.Blogs .Where(b => b.Url.Contains("microsoft.com")) .OrderBy(b => b.Url); foreach (var blog in blogs) { Console.WriteLine($" - {blog.Url}"); }

Relationships

EF Core supports various relationships, including one-to-many and many-to-many. The example above shows a one-to-many relationship between Blog and Post.

You can configure relationships more explicitly in the OnModelCreating method of your DbContext if needed.

4. Advanced Topics

EF Core offers a rich set of features for more complex scenarios, including:

5. API Reference

For a comprehensive list of EF Core classes and methods, please refer to the official .NET API Browser.

This guide provides a basic introduction. For detailed information and more advanced scenarios, consult the full EF Core documentation.