Introduction to Entity Framework Core
Entity Framework Core (EF Core) is a modern, cross-platform, open-source version of the Entity Framework data access technology for .NET. It is a lightweight, extensible, and high-performance object-relational mapper (ORM) that enables .NET developers to work with databases using domain-specific objects and eliminates the need for most of the data-access code that developers traditionally need to write.
What is Entity Framework Core?
EF Core allows you to:
- Query your data as strongly-typed objects.
- Persist your domain models to a relational database.
- Use LINQ (Language Integrated Query) to write queries in C# or other .NET languages.
- Leverage database migrations to manage schema changes.
- Support for various database providers (SQL Server, PostgreSQL, MySQL, SQLite, etc.).
Key Concepts in EF Core
EF Core revolves around several core concepts that you'll frequently use:
1. DbContext
The DbContext
is the primary class that represents a session with the database and
allows you to query and save data. It acts as a gateway to your data.
using Microsoft.EntityFrameworkCore;
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=BloggingEFCore;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 (e.g., all
blogs in the database) and can be used to query and save data for that entity type.
3. Model
The EF Core model represents the structure of your data. It's typically defined by your
entity classes and the relationships between them. EF Core can infer much of the model
from your entity classes (convention-over-configuration), or you can configure it explicitly
using the ModelBuilder API in OnModelCreating
.
4. Migrations
Migrations are a feature of EF Core that allow you to incrementally update your database schema to match your evolving domain model. They can be automatically generated based on changes to your model or written manually.
Getting Started
To start using EF Core, you'll typically need to:
- Install the necessary EF Core NuGet packages (e.g.,
Microsoft.EntityFrameworkCore.SqlServer
). - Define your entity classes.
- Create a
DbContext
class. - Configure your
DbContext
(e.g., connection string, database provider). - Scaffold a database from an existing database or use migrations to create it.
EF Core provides a powerful and flexible way to interact with databases in your .NET applications, simplifying data access and improving developer productivity.
Next Steps
Continue exploring the following topics to deepen your understanding of EF Core: