Entity Framework Core Overview
Entity Framework Core (EF Core) is a modern, cross-platform, extensible data access technology for .NET. It is a lightweight, high-performance, and open-source version of the popular Entity Framework.
What is Entity Framework Core?
EF Core enables .NET developers to:
- Work with a domain-specific language (DSL) for accessing data.
- Create LINQ queries that are automatically translated into SQL.
- Map .NET objects to relational database tables.
- Manage database schema migrations.
- Handle relationships between entities.
- Perform CRUD (Create, Read, Update, Delete) operations efficiently.
Key Features
EF Core offers a rich set of features designed to simplify data access:
- Object-Relational Mapper (ORM): Maps your .NET objects directly to database tables, abstracting away SQL.
- Cross-Platform: Runs on Windows, macOS, and Linux.
- High Performance: Optimized for speed and memory usage.
- Extensible: Supports custom providers, interceptors, and conventions.
- Model-First, Database-First, and Code-First: Flexible approaches to define your data model.
- Change Tracking: Automatically tracks changes to entities.
- Migrations: Manages database schema evolution over time.
- JSON Columns: Supports storing and querying JSON data.
- Owned Entities: Define complex types that are owned by another entity.
- Value Objects: Model immutable data structures.
Getting Started
To start using EF Core, you typically need to install the appropriate NuGet packages:
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer # Or your specific database provider
Then, you define your data model using C# classes and a DbContext
:
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Blogging;Trusted_Connection=True;");
}
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public virtual ICollection<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 virtual Blog Blog { get; set; }
}
EF Core can then generate the database schema based on your model (Code-First approach) or scaffold a model from an existing database (Database-First approach).
Learn More
For more in-depth information, explore the following resources:
Entity Framework Core
EF Core
ORM
.NET Data Access
Database Migration
LINQ to SQL
C# Data Access