MSDN Documentation

Entity Framework Core Overview

Entity Framework Core (EF Core) is a modern, cross-platform, open-source, and extensible version of the classic Entity Framework Data Access technology. EF Core is designed to be lightweight, performant, and easy to use, enabling developers to build data-driven applications more efficiently.

What is Entity Framework Core?

EF Core provides a set of APIs and tools that allow developers to interact with databases using .NET objects. It bridges the gap between object-oriented code and relational databases, abstracting away much of the complexity of SQL queries and database schema management.

Key Features of EF Core:

Getting Started with EF Core

To start using EF Core, you'll typically need to install the necessary NuGet packages:

Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer  # Or your specific database provider
Install-Package Microsoft.EntityFrameworkCore.Tools

Core Concepts:

1. DbContext

The DbContext is the primary class for interacting with the database. It represents a session with the database and is used to query and save data. You typically create a derived context class and add a DbSet<TEntity> property for each entity you want to query.

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=Blogging;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 for a given type. This, combined with a DbContext instance, allows you to query and save data for your entity.

3. Migrations

Migrations are a feature that allows you to incrementally update your database schema to match your entity model. EF Core keeps track of these changes, enabling you to evolve your database schema as your application changes.

Common migration commands:

4. LINQ to Entities

You can use LINQ queries to retrieve data from your database. EF Core translates these LINQ queries into SQL statements that are executed against your database.

using (var db = new BloggingContext())
{
    var blogs = db.Blogs
                  .Where(b => b.Url.Contains("example.com"))
                  .OrderBy(b => b.Url);

    foreach (var blog in blogs)
    {
        Console.WriteLine(blog.Url);
    }
}
Note: EF Core is the successor to Entity Framework 6. While EF6 is still supported, EF Core offers significant architectural improvements and is the recommended choice for new development.

Use Cases

EF Core is ideal for a wide range of .NET applications, including:

Learn More

This overview provides a glimpse into Entity Framework Core. For in-depth information, tutorials, and advanced topics, please refer to the official Microsoft documentation.