Entity Framework Core API
Entity Framework Core (EF Core) is a modern object-relational mapper (ORM) for .NET. It allows developers to work with a database using .NET objects that represent their data, eliminating the need for most of the data-access code they typically need to write. EF Core is a lightweight and extensible version of the familiar Entity Framework.
Core Concepts
DbContext
Represents a session with the database and allows you to query and save data.
DbSet<TEntity>
Represents a collection of all entities in the context, or that can be queried from the database.
Migrations
Helps manage incremental changes to your database schema over time.
LINQ to Entities
Allows you to query your database using Language Integrated Query (LINQ).
Key Features & Functionality
EF Core provides a rich set of features for interacting with your database. Here are some highlights:
Querying Data
EF Core integrates seamlessly with LINQ to provide powerful and expressive querying capabilities. You can perform complex filtering, sorting, and projection directly in your C# code.
// Example: Fetching all active users
var activeUsers = await context.Users
.Where(u => u.IsActive)
.OrderBy(u => u.LastName)
.ToListAsync();
Saving Data
Adding, updating, and deleting entities is straightforward. EF Core tracks changes and generates the appropriate SQL commands.
// Example: Adding a new product
var newProduct = new Product { Name = "Super Widget", Price = 19.99m };
context.Products.Add(newProduct);
await context.SaveChangesAsync();
Relationships
Define and manage relationships between your entities (one-to-one, one-to-many, many-to-many) using navigation properties.
// Example: One-to-many relationship between Author and Books
public class Author {
public int Id { get; set; }
public string Name { get; set; }
public List<Book> Books { get; set; } = new List<Book>();
}
public class Book {
public int Id { get; set; }
public string Title { get; set; }
public int AuthorId { get; set; }
public Author Author { get; set; }
}
Performance Optimization
EF Core offers features like:
- Lazy Loading: Automatically loads related data when a navigation property is accessed.
- Eager Loading: Explicitly loads related data using
Include()
orThenInclude()
. - Projection: Select only the data you need into anonymous types or DTOs.
- Change Tracking: Efficiently tracks changes to entities.
// Example: Eager Loading a blog and its posts
var blogWithPosts = await context.Blogs
.Include(b => b.Posts)
.FirstOrDefaultAsync(b => b.Id == 1);
Providers & Configuration
EF Core supports various database providers, including:
- SQL Server:
Microsoft.EntityFrameworkCore.SqlServer
- SQLite:
Microsoft.EntityFrameworkCore.Sqlite
- PostgreSQL:
Npgsql.EntityFrameworkCore.PostgreSQL
- MySQL:
Pomelo.EntityFrameworkCore.MySql
- Cosmos DB:
Microsoft.EntityFrameworkCore.Cosmos
Configuration is typically done in your Startup.cs
or Program.cs
file using dependency injection.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));