What is Entity Framework Core?
Entity Framework Core (EF Core) is a modern, cross-platform, extensible, and lightweight version of the popular Microsoft Entity Framework data access technology. It allows developers to work with a conceptual model, rather than the raw database structures, making data access more intuitive and less error-prone.
EF Core enables you to interact with your database using .NET objects, abstracting away the complexities of SQL and database-specific syntax. This promotes a more object-oriented approach to data persistence.
Key Features
- Cross-Platform: Runs on Windows, macOS, and Linux.
- Modern & Extensible: Designed for modern application development with a modular architecture.
- Lightweight: Reduced dependencies and smaller footprint.
- LINQ Support: Query your database using Language Integrated Query (LINQ).
- Migrations: Manage database schema changes systematically.
- Performance Improvements: Optimized for speed and efficiency.
- Multiple Database Providers: Supports SQL Server, SQLite, PostgreSQL, MySQL, Cosmos DB, and more.
Getting Started
To start using EF Core, you'll typically need to install the necessary NuGet packages. The core package is
Microsoft.EntityFrameworkCore
. You'll also need a provider for your specific database.
For example, to add SQL Server support:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
And to manage migrations:
Install-Package Microsoft.EntityFrameworkCore.Tools
Core Concepts
Understanding these core concepts is crucial for effective use of EF Core:
1. DbContext
The DbContext
is the primary class for interacting with the database. It represents a session
with the database and can be used to query and save data. You typically create a class that inherits
from DbContext
and includes DbSet<TEntity>
properties for your entities.
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("YourConnectionString");
}
}
2. DbSet<TEntity>
A DbSet<TEntity>
represents a collection of a given entity type in the database.
It's used to query and interact with tables.
3. Migrations
Migrations allow you to evolve your database schema over time as your application's data model changes. You can create a migration from your C# code model, and EF Core will generate the SQL to update your database.
To add a new migration:
Add-Migration InitialCreate
To apply migrations to the database:
Update-Database
4. Database Providers
EF Core uses providers to map its operations to the specific database system you are using. You must install and configure the appropriate provider for your database.
Querying Data
You can query data using LINQ. EF Core translates these LINQ queries into SQL that your database can understand.
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Where(b => b.Rating > 3)
.OrderBy(b => b.Url)
.ToList();
foreach (var blog in blogs)
{
Console.WriteLine($"Blog URL: {blog.Url}");
}
}
Saving Data
To save data, you add, modify, or remove entities from your DbContext
and then call
SaveChanges()
.
using (var context = new BloggingContext())
{
var newBlog = new Blog { Url = "http://example.com/blog", Rating = 5 };
context.Blogs.Add(newBlog);
context.SaveChanges();
var existingBlog = context.Blogs.Find(1);
if (existingBlog != null)
{
existingBlog.Rating = 4;
context.SaveChanges();
}
}