Getting Started with Entity Framework
Entity Framework (EF) is a set of Microsoft technologies that enables .NET developers to work with relational data using domain-specific objects that are conceptually cleaner (i.e., persistent classes) than database tables.
EF is an Object-Relational Mapper (ORM). It allows developers to query and manipulate data using an object-oriented paradigm, rather than the data manipulation language (DML) statements typically required by most database systems.
Key Concepts
1. Code-First vs. Database-First vs. Model-First
EF provides three approaches to define your data model:
- Code-First: You define your domain classes in C# or VB.NET, and EF generates the database schema based on your classes. This is often the preferred approach for new projects.
- Database-First: You already have an existing database, and EF generates your domain classes and mapping configuration from the database schema.
- Model-First: You design your data model visually using an Entity Designer in Visual Studio, and EF generates both the domain classes and the database schema.
2. DbContext
The DbContext
class represents a session with the database and is the primary way to interact with data in Entity Framework. It allows you to query data from the database and save changes to it.
3. DbSet
A DbSet<TEntity>
property on your derived context class represents a table in the database. You use DbSet<TEntity>
to add, delete, and modify entities in the database.
Getting Started with Code-First
Step 1: Install Entity Framework Core
You can install EF Core using the NuGet Package Manager in Visual Studio or via the .NET CLI.
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
Step 2: Define Your Model Classes
Create C# classes to represent your database entities.
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; }
}
Step 3: Create Your DbContext
Create a class that derives from DbContext
and includes DbSet<TEntity>
properties for your entities.
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=BloggingEF;Trusted_Connection=True;");
}
}
Step 4: Migrations
Use EF Core Migrations to create and update your database schema.
dotnet ef migrations add InitialCreate
dotnet ef database update
Step 5: Use Your DbContext
Instantiate your context and start working with your data.
using var db = new BloggingContext();
// Add a new blog
db.Blogs.Add(new Blog { Url = "http://example.com" });
db.SaveChanges();
// Query blogs
var blogs = db.Blogs.OrderBy(b => b.Url).ToList();
foreach (var blog in blogs)
{
Console.WriteLine($"Blog URL: {blog.Url}");
}
DbContext
lifecycle.