ADO.NET Entity Framework Concepts

Welcome to the conceptual overview of ADO.NET Entity Framework (EF). This section aims to provide a solid understanding of the core principles, architecture, and fundamental components that make EF a powerful Object-Relational Mapper (ORM) for .NET developers.

What is Entity Framework?

Entity Framework is an object-relational mapper (OORM) that enables developers to work with relational data using domain-specific objects that are freely available as. It eliminates the need for most of the data-access code that developers need to write. For example, with Entity Framework, developers can query and save data to tables in a database as if they were dealing with ordinary objects, and such. This is accomplished by providing the developer with an Entity SQL or LINQ to Entities.

Key Concepts

1. Conceptual Model

The conceptual model represents your business domain entities and relationships. It's an abstraction that is independent of the underlying database schema. This model is typically defined using:

2. Storage Model

The storage model describes the structure of your database, including tables, columns, primary keys, foreign keys, and constraints. EF maps the conceptual model to this storage model.

3. Mappings

Mappings define how the properties and relationships in the conceptual model correspond to the tables and columns in the storage model. These mappings are crucial for translating operations on your domain objects into SQL queries that the database can execute.

4. DbContext

The DbContext class is the primary gateway to interacting with EF. It represents a session with the database and allows you to query and save data. Key responsibilities of DbContext include:

Example usage:


using (var context = new MyDbContext())
{
    var users = context.Users.Where(u => u.IsActive).ToList();
    foreach (var user in users)
    {
        Console.WriteLine(user.Name);
    }
}
            

5. DbSets

DbSet<TEntity> represents a collection of entities of a given type within the DbContext. You can think of it as a table in your database. EF uses DbSet properties on your DbContext to query and manipulate data.


public class MyDbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Order> Orders { get; set; }
}
            

6. LINQ to Entities

LINQ to Entities allows you to write queries against your conceptual model using Language Integrated Query (LINQ). EF translates these LINQ queries into SQL queries that are executed against the database.


var activeAdmins = context.Users
    .Where(u => u.Role == "Admin" && u.IsActive)
    .OrderBy(u => u.Name)
    .ToList();
            

7. Migrations

EF Migrations is a feature that allows you to evolve your database schema over time as your domain model changes. It enables you to track schema changes as code and apply them to your database.

Benefits of using Entity Framework

Explore the following sections for a deeper dive into specific features and advanced topics.