Entity Framework Core Overview
Entity Framework Core (EF Core) is a modern, cross-platform, open-source, and extensible version of the classic Entity Framework Data Access technology. EF Core is designed to be lightweight, performant, and easy to use, enabling developers to build data-driven applications more efficiently.
What is Entity Framework Core?
EF Core provides a set of APIs and tools that allow developers to interact with databases using .NET objects. It bridges the gap between object-oriented code and relational databases, abstracting away much of the complexity of SQL queries and database schema management.
Key Features of EF Core:
- Cross-Platform Support: Runs on Windows, macOS, and Linux.
- Performance Improvements: Optimized for speed and efficiency.
- Lightweight and Extensible: Designed to be modular and easy to customize.
- LINQ to Entities: Allows you to query your database using Language Integrated Query (LINQ).
- Change Tracking: Automatically detects changes made to entities.
- Migrations: Manages database schema changes over time.
- Support for Various Databases: Works with SQL Server, PostgreSQL, MySQL, SQLite, Oracle, and more through providers.
- Dependency Injection Integration: Seamlessly integrates with .NET's built-in dependency injection system.
Getting Started with EF Core
To start using EF Core, you'll typically need to install the necessary NuGet packages:
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer # Or your specific database provider
Install-Package Microsoft.EntityFrameworkCore.Tools
Core Concepts:
1. DbContext
The DbContext
is the primary class for interacting with the database. It represents a session with the database and is used to query and save data. You typically create a derived context class and add a DbSet<TEntity>
property for each entity you want to query.
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; }
}
2. DbSet<TEntity>
A DbSet<TEntity>
represents a collection of all entities in the store for a given type. This, combined with a DbContext
instance, allows you to query and save data for your entity.
3. Migrations
Migrations are a feature that allows you to incrementally update your database schema to match your entity model. EF Core keeps track of these changes, enabling you to evolve your database schema as your application changes.
Common migration commands:
Add-Migration InitialCreate
: Creates a new migration.Update-Database
: Applies pending migrations to the database.Script-Migration
: Generates a SQL script for a migration.
4. LINQ to Entities
You can use LINQ queries to retrieve data from your database. EF Core translates these LINQ queries into SQL statements that are executed against your database.
using (var db = new BloggingContext())
{
var blogs = db.Blogs
.Where(b => b.Url.Contains("example.com"))
.OrderBy(b => b.Url);
foreach (var blog in blogs)
{
Console.WriteLine(blog.Url);
}
}
Use Cases
EF Core is ideal for a wide range of .NET applications, including:
- Web applications (ASP.NET Core)
- Desktop applications (WPF, WinForms)
- Cloud-native applications
- Mobile applications (via Xamarin)
Learn More
This overview provides a glimpse into Entity Framework Core. For in-depth information, tutorials, and advanced topics, please refer to the official Microsoft documentation.