Entity Framework Core
Entity Framework Core (EF Core) is a modern object‑relational mapper (ORM) for .NET. It enables .NET developers to work with a database using .NET objects, eliminating most of the data‑access code they usually need to write.
Quick start
Install the EF Core package and create a simple model.
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
// Model
public class Blog
{
public int BlogId { get; set; }
public string? Url { get; set; }
public List<Post> Posts { get; set; } = new();
}
// DbContext
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;");
}
Key concepts
- DbContext – a session with the database.
- Models – POCO classes that represent tables.
- Migrations – version‑control for schema changes.
- LINQ queries – strongly typed queries against the DbSet.