MSDN

Microsoft Developer Network

Entity Framework Overview

Entity Framework (EF) is a set of data access technologies for .NET Framework that enables developers to work with data as objects by eliminating the need for most of the data-access code that developers traditionally need to write. EF supports the Model-First, Database-First, and Code-First approaches to starting development.

Table of Contents

Introduction to Entity Framework

Entity Framework is Microsoft's recommended Object-Relational Mapper (ORM) for the .NET platform. It allows developers to query and manipulate data in relational databases using domain-specific language (DSL) that is .NET language-specific. Developers can define their data schema and logic in C# or Visual Basic .NET, and EF generates the database and handles data persistence.

Key benefits of using EF include:

Key Features of Entity Framework

Entity Framework offers a rich set of features to facilitate data access:

Approaches to Development

Entity Framework supports three primary approaches to developing applications:

1. Code-First

In the Code-First approach, you start by defining your domain-specific classes (entities) in C# or VB.NET. EF then infers the database schema from these classes. This approach is ideal for developers who prefer to work with code first and let EF handle database creation.


public class Blog
{
    public int BlogId { get; set; }
    public string Name { 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; }
}

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

2. Database-First

With the Database-First approach, you start with an existing database. EF tools can then generate an Entity Data Model (EDM) and corresponding classes based on the database schema. This is useful when working with legacy databases.

You can use the EF Designer in Visual Studio or the EF Core Power Tools to reverse-engineer your database into model files.

Placeholder for a database diagram

3. Model-First

The Model-First approach allows you to design your data model visually using the EF Designer in Visual Studio. EF then generates both the database schema and the C# or VB.NET classes from this visual model. This approach is good for rapid prototyping and when a visual representation of the data model is preferred.

The Entity Data Model (EDM)

The Entity Data Model (EDM) is a conceptual model that represents the data for your application. It consists of:

The EDM abstracts the underlying database schema, allowing you to focus on your application's data domain.

EntityClient Provider

The EntityClient provider is a .NET data provider that allows you to query an EDM using Entity SQL. Entity SQL is a declarative, SQL-like language that operates on the EDM, not directly on tables. This provides a consistent way to query data regardless of the underlying database.


using System.Data.EntityClient;
using System.Data.Objects;

// ...

using (var connection = new EntityClientConnection("name=BloggingContext"))
{
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "SELECT VALUE blog FROM BloggingContext.Blogs AS blog WHERE blog.Name = 'My First Blog'";

    using (var reader = new ObjectQuery<Blog>(command.CommandText, ((IObjectContextAdapter)context).ObjectContext))
    {
        foreach (var blog in reader)
        {
            Console.WriteLine(blog.Name);
        }
    }
}
            

Getting Started with Entity Framework

To start using Entity Framework in your .NET project:

  1. Install the EF package: Use NuGet Package Manager to install the appropriate EF package for your project (e.g., Microsoft.EntityFrameworkCore for EF Core, or EntityFramework for older versions).
  2. Define your entities: Create C# or VB.NET classes to represent your data.
  3. Create a DbContext: Define a class that inherits from DbContext and includes DbSet<TEntity> properties for your entities.
  4. Configure the database connection: Provide connection string information in your application's configuration.
  5. Perform data operations: Use LINQ to query, add, update, and delete data.
Note: Entity Framework has evolved significantly. Entity Framework Core (EF Core) is the latest generation, designed for cross-platform use and performance improvements. For new projects, it is highly recommended to use EF Core.

Conclusion

Entity Framework empowers .NET developers to interact with databases in a more productive and object-oriented manner. By abstracting low-level data access details, EF allows developers to concentrate on business logic rather than plumbing. Whether you choose Code-First, Database-First, or Model-First, Entity Framework provides a robust and flexible solution for data persistence in your .NET applications.

For more detailed information, explore the following resources: