An Introduction to Entity Framework Core
A beginner's guide to Entity Framework Core 1.0, the new, cross-platform Object-Relational Mapper (ORM) for .NET Core. Learn how to use it to work with databases using .NET objects instead of raw SQL.
Now that .NET Core 1.0 is here, developers need a modern way to interact with databases on the new, cross-platform framework. The answer is Entity Framework Core (EF Core). EF Core is a lightweight, extensible, and cross-platform version of the popular Entity Framework data access technology.
It's an Object-Relational Mapper (ORM), which is a tool that enables you to query and manipulate data from a database using an object-oriented paradigm, rather than writing raw SQL statements.
What is an ORM?
An ORM bridges the gap between your object-oriented application code (e.g., your C# classes) and a relational database (e.g., SQL Server, PostgreSQL, SQLite). Instead of writing SELECT * FROM Products
, you can write a LINQ query like context.Products.ToList()
, and the ORM will translate it into the appropriate SQL for you.
Core Concepts of EF Core
To get started with EF Core, you need to understand a few key concepts.
The
DbContext
: This is the most important class in EF Core. An instance ofDbContext
represents a session with the database and is used to query and save data.The
DbSet<T>
: YourDbContext
class will contain aDbSet<T>
property for each table in your database you want to query. ADbSet<T>
is a collection that represents all the entities of a given type in the database.Entities: An entity is an instance of a C# class (often a POCO - Plain Old CLR Object) that represents a single row in a database table.
A Simple Example
Let's create a simple model for a blogging application.
The Model (Blog.cs
)
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
The Context (BloggingContext.cs
)
This class inherits from DbContext
and exposes a DbSet
for our Blog
entities.
using Microsoft.EntityFrameworkCore;
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// For this simple example, we'll use the SQLite provider.
optionsBuilder.UseSqlite("Data Source=blogging.db");
}
}
Querying and Saving Data
Now you can use your BloggingContext
to interact with the database.
using (var db = new BloggingContext())
{
// Create a new blog
db.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
// Save the changes to the database
db.SaveChanges();
// Query for all blogs
var blogs = db.Blogs.ToList();
}
Notice the call to SaveChanges()
. EF Core uses a Unit of Work pattern. It tracks all the changes you make to your entities, but it doesn't actually send any commands to the database until you call SaveChanges()
.
Migrations: Managing Your Schema
As your application evolves, your data model will change. EF Core Migrations is a feature that allows you to keep your database schema in sync with your EF Core model.
You can use the dotnet ef
command-line tool to manage migrations:
dotnet ef migrations add <MigrationName>
: This command scaffolds a new migration based on the changes you've made to your model.dotnet ef database update
: This command applies any pending migrations to the database, creating or updating the schema.
A New Foundation for Data Access
EF Core 1.0 is a complete rewrite and a new foundation for data access on .NET Core. While it doesn't yet have all the features of the full Entity Framework 6, it provides a modern, lightweight, and extensible ORM that works anywhere .NET Core works.
For developers building new applications on .NET Core, EF Core is the standard and recommended way to work with relational databases.