Entity Framework Core in ASP.NET Core

This learn path guides you through using Entity Framework Core (EF Core) to interact with your database in ASP.NET Core applications. EF Core is a modern object-relational mapper (ORM) that enables .NET developers to work with databases using domain-specific objects and LINQ.

Introduction to Entity Framework Core

Entity Framework Core is a lightweight, extensible data access technology that enables the development of functional domain models using a relational database. It allows you to query a database and work with the results as strongly typed .NET objects.

Learn More

Setting up EF Core

This section covers the initial setup of EF Core in your ASP.NET Core project, including installing necessary NuGet packages and configuring the DbContext.

Installing EF Core Packages

You'll need to install the appropriate EF Core provider for your database (e.g., SQL Server, PostgreSQL, SQLite).

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

For SQL Server support. Replace SqlServer with your chosen database provider.

Configuring DbContext

Registering your DbContext in the Startup.cs (or equivalent) file and configuring the database connection string.

// In Startup.cs or Program.cs services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
Learn More

Working with Models and Migrations

Define your data models as Plain Old CLR Objects (POCOs) and use EF Core Migrations to manage your database schema changes.

Defining Entities

Create classes that represent your database tables.

public class Product { public int ProductId { get; set; } public string Name { get; set; } public decimal Price { get; set; } }

Code-First Migrations

Use migrations to create and update your database schema based on your entity models.

dotnet ef migrations add InitialCreate
dotnet ef database update
Learn More

Querying Data with LINQ

Learn how to retrieve data from your database using Language Integrated Query (LINQ).

Basic Queries

var products = await _context.Products .Where(p => p.Price > 10.00m) .OrderBy(p => p.Name) .ToListAsync();

Including Related Data (Eager Loading)

var productsWithCategory = await _context.Products .Include(p => p.Category) .ToListAsync();
Learn More

Saving Data

Understand how to add, update, and delete data in your database.

Adding an Entity

var newProduct = new Product { Name = "New Gadget", Price = 49.99m }; _context.Products.Add(newProduct); await _context.SaveChangesAsync();

Updating an Entity

var productToUpdate = await _context.Products.FindAsync(productId); if (productToUpdate != null) { productToUpdate.Price = 55.00m; await _context.SaveChangesAsync(); }

Deleting an Entity

var productToDelete = await _context.Products.FindAsync(productId); if (productToDelete != null) { _context.Products.Remove(productToDelete); await _context.SaveChangesAsync(); }
Learn More

Advanced Topics

Explore more advanced features and best practices for using EF Core.

Learn More