Entity Framework Concepts

This section delves into the core concepts that underpin the Entity Framework (EF), a powerful object-relational mapper (ORM) for .NET that enables developers to work with relational databases using .NET objects.

Key Concepts

1. DbContext

The DbContext is the primary class representing a session with the database. It is a gateway to querying and saving data. You can think of it as a collection of objects that represent data from your database. A DbContext instance is short-lived and is typically created for a single unit of work.

2. DbSet<TEntity>

A DbSet<TEntity> represents a table in your database. Each property of your DbContext that is of type DbSet<TEntity> corresponds to a table in the database. You use DbSet<TEntity> to query and retrieve entities from the database and to add, modify, or remove entities.


public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
}
        

3. Entities

Entities are your .NET objects that represent data in your database. They are typically POCOs (Plain Old CLR Objects) that map directly to database tables. Entity Framework uses these entities to interact with the database.


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; }
}
        

4. Migrations

Entity Framework Migrations is a feature that allows you to evolve your database schema over time as your application's data model changes. It helps keep your database schema in sync with your code. Migrations generate SQL scripts to apply changes to the database schema.

5. LINQ to Entities

LINQ to Entities allows you to write queries against your entities using Language Integrated Query (LINQ) syntax. EF translates these LINQ queries into SQL queries that are executed against the database. This provides a type-safe and expressive way to query data.


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

    foreach (var blog in blogs)
    {
        Console.WriteLine(blog.Url);
    }
}
        

6. Change Tracking

Entity Framework automatically tracks changes made to entities that are being tracked by a DbContext. When you call SaveChanges(), EF determines which entities have been added, modified, or deleted and generates the appropriate SQL statements to persist these changes to the database.

7. Lazy Loading and Eager Loading


// Eager Loading Example
var blogWithPosts = db.Blogs
                        .Include(b => b.Posts)
                        .FirstOrDefault(b => b.BlogId == 1);
        
Important: Understanding these core concepts is crucial for effectively using Entity Framework to build robust and efficient data-driven applications.