Introduction to Entity Framework Core
Entity Framework Core (EF Core) is a modern, cross-platform, extensible, and high-performance Object-Relational Mapper (ORM) for .NET. It enables developers to work with a database using .NET objects, eliminating the need for most of the data-access code that developers traditionally need to write. EF Core works with many popular relational databases, including SQL Server, PostgreSQL, MySQL, SQLite, Oracle, and more.
- DbContext: Represents a session with the database and is the entry point to query and save data.
- DbSet<TEntity>: Represents a collection of all entities in the store for a given entity type.
- Entities: Plain Old CLR Objects (POCOs) that represent tables in your database.
- Model: Represents the structure of your data, including entities, relationships, and constraints.
Why Use Entity Framework Core?
EF Core simplifies database interactions in several ways:
- Productivity: Reduces the amount of boilerplate data access code you need to write.
- Maintainability: Easier to manage and refactor database interactions.
- Abstraction: Abstracts away many of the complexities of SQL and specific database providers.
- Type Safety: Allows you to work with data using strongly-typed .NET objects, catching errors at compile time rather than runtime.
- Cross-Platform: Works seamlessly on Windows, macOS, and Linux.
Getting Started
To start using EF Core, you'll typically need to install the appropriate NuGet packages for your database provider and EF Core itself. For example, to work with SQL Server:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
You'll then define your entities, create a DbContext
, and configure your database connection.
Example: A Simple Entity
Let's define a simple Product
entity:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
Example: A DbContext
And a DbContext
that includes a DbSet<Product>
:
using Microsoft.EntityFrameworkCore;
public class ApplicationDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Optional: Configure model further here
modelBuilder.Entity<Product>().Property(p => p.Price).HasColumnType("decimal(18, 2)");
}
}
DbContext
using dependency injection in ASP.NET Core applications.
Core Concepts Explained
The DbContext
The DbContext
is central to EF Core. It acts as a gateway to your database. You create instances of your DbContext
class to interact with your data. It manages the connection, tracks changes to entities, and allows you to perform operations like querying, adding, updating, and deleting data.
Entities
Entities are .NET classes that represent the tables in your database. They are typically POCOs (Plain Old CLR Objects) and don't need to inherit from a specific base class or implement interfaces. EF Core uses conventions to map these classes to database tables.
DbSets
A DbSet<TEntity>
on your DbContext
represents a collection of instances of a given entity type. When you query a DbSet
, EF Core translates your LINQ query into SQL and executes it against the database. You can add, remove, and modify entities through the DbSet
.
Model Configuration
EF Core uses conventions to map your .NET classes to your database schema. However, you can override these conventions and provide explicit configuration using data annotations or the Fluent API (via the OnModelCreating
method in your DbContext
). This allows you to specify table names, column names, data types, relationships, keys, and more.
Next Steps
This introduction provides a high-level overview of Entity Framework Core. To delve deeper, explore the following topics:
- EF Core Basics for hands-on setup and basic operations.
- CRUD Operations to learn how to create, read, update, and delete data.
- Migrations to manage schema changes over time.