Skip to main content

Entity Framework Overview

Entity Framework (EF) is an Object-Relational Mapping (ORM) framework developed by Microsoft.

Introduction

Entity Framework (EF) is an Object-Relational Mapping (ORM) framework developed by Microsoft. It enables developers to work with databases using strongly-typed objects, eliminating the need for writing complex SQL queries. EF abstracts the underlying database, allowing developers to focus on application logic rather than database operations.

In this tutorial, we will explore the history, features, and examples of Entity Framework.

History

Entity Framework was first released in 2008 as part of the .NET Framework 3.5 Service Pack 1. It was developed to simplify data access for .NET applications and provide a consistent way to interact with various database systems.

Over the years, Entity Framework has evolved significantly, introducing new features and improvements. Some notable versions include EF 4.0, EF 5.0, EF 6.0, and EF Core (a cross-platform version of Entity Framework).

Features

  1. Entity Data Model: Entity Framework uses an Entity Data Model (EDM) to represent the structure of a database. The EDM consists of entity classes that map to database tables, relationships between entities, and metadata about the database schema. It provides a conceptual view of the data, enabling developers to work with entities rather than database tables directly.

    Example:

    public class Product
    {
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    }
  2. LINQ Support: Entity Framework integrates with Language Integrated Query (LINQ), allowing developers to write queries using strongly-typed syntax. LINQ queries are translated into SQL queries by Entity Framework, enabling efficient data retrieval from the database.

    Example:

    var expensiveProducts = dbContext.Products.Where(p => p.Price > 100);
  3. Lazy Loading: Entity Framework supports lazy loading, which means related entities are loaded from the database only when accessed. This can improve performance by reducing unnecessary database queries.

    Example:

    var product = dbContext.Products.First();
    var category = product.Category; // Category entity is loaded on-demand
  4. Code First: Entity Framework Code First enables developers to define the database schema using C# or VB.NET code. It automatically generates the database based on the entity classes and their relationships.

    Example:

    public class Product
    {
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }

    public int CategoryId { get; set; }
    public Category Category { get; set; }
    }

    public class Category
    {
    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<Product> Products { get; set; }
    }
  5. Database Migrations: Entity Framework supports database migrations, making it easy to evolve the database schema over time. Migrations enable developers to apply incremental changes to the database without losing existing data.

    Example:

    PM> Add-Migration AddDiscountColumn
    PM> Update-Database
  6. Database Providers: Entity Framework supports multiple database providers, including SQL Server, MySQL, Oracle, and SQLite. Developers can choose the provider that best suits their application's requirements.

    Example:

    services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

Examples

  1. Querying Data:

    var dbContext = new ApplicationDbContext();

    var products = dbContext.Products
    .Where(p => p.Price > 50)
    .OrderByDescending(p => p.Price)
    .ToList();

    foreach (var product in products)
    {
    Console.WriteLine(product.Name);
    Console.WriteLine(product.Price);
    }

    Output:

    Product A
    99.99
    Product B
    79.99
  2. Inserting Data:

    var dbContext = new ApplicationDbContext();

    var newProduct = new Product
    {
    Name = "New Product",
    Price = 49.99
    };

    dbContext.Products.Add(newProduct);
    dbContext.SaveChanges();
  3. Updating Data:

    var dbContext = new ApplicationDbContext();

    var product = dbContext.Products.Find(1);
    product.Price = 59.99;

    dbContext.SaveChanges();
  4. Deleting Data:

    var dbContext = new ApplicationDbContext();

    var product = dbContext.Products.Find(1);
    dbContext.Products.Remove(product);

    dbContext.SaveChanges();

Conclusion

Entity Framework is a powerful ORM framework that simplifies data access in .NET applications. It provides a rich set of features, including entity mapping, LINQ support, lazy loading, code-first development, database migrations, and support for multiple database providers. By using Entity Framework, developers can focus on application logic rather than low-level database operations.

For more information, you can visit the official Entity Framework documentation: Entity Framework Official Site