David's Blog

A Deep Dive into Entity Framework

By David Li on 2025-01-08T08:09:42.000Z

A Deep Dive into Entity Framework

Introduction

Entity Framework (EF) is an Object-Relational Mapper (ORM) developed by Microsoft. It simplifies data access in applications by allowing developers to interact with databases using objects and LINQ queries instead of writing raw SQL queries. Entity Framework is a part of the larger .NET Framework, and it has evolved through various versions, the latest being Entity Framework Core (EF Core).

In this article, we will explore the key concepts of Entity Framework, its architecture, and how to use it in a typical .NET application.

Components of Entity Framework

Entity Framework can be broken down into three main components:

  1. DbContext: Represents a session with the database and is used to query and save instances of the entity classes to the database. It provides a high-level API to perform CRUD operations on the database.

  2. Entity Data Model (EDM): Represents the structure of your domain model, including entities, their properties, and relationships between entities. It is a conceptual model that maps the object-oriented domain model to the relational database schema.

  3. LINQ Provider: Translates LINQ queries written in C# or VB.NET to SQL queries that can be executed against the database.

Entity Framework Architecture

The architecture of Entity Framework can be divided into two layers:

  1. Runtime Components: This layer deals with the actual interaction between your application and the database. It includes the DbContext, EDM, and LINQ Provider.

  2. Design-time Components: This layer deals with the creation and maintenance of the EDM. It includes tools to create and update the EDM from an existing database, generate the database schema from the EDM, and validate the EDM.

The two layers work together to provide a seamless experience for developers working with data in .NET applications.

Using Entity Framework in a .NET Application

To demonstrate how to use Entity Framework in a .NET application, let’s create a simple console application.

Step 1: Install Entity Framework

First, we need to install the Entity Framework NuGet package. To do this, open the Package Manager Console in Visual Studio and run the following command:

Install-Package Microsoft.EntityFrameworkCore.SqlServer

Step 2: Create the Entity Classes

Next, we will create our domain model classes. In this example, we will create a simple model for a blog that has a collection of posts:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public 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 Blog Blog { get; set; }
}

Step 3: Create the DbContext

Now, we need to create a class that inherits from DbContext. This class will be used to interact with the database:

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

Step 4: Query and Save Data

Finally, we can use the BloggingContext class to query and save data:

static void Main(string[] args)
{
    using (var context = new BloggingContext())
    {
        // Create a new blog and save it to the database
        var blog = new Blog { Url = "https://example.com" };
        context.Blogs.Add(blog);
        context.SaveChanges();

        // Query the database for all blogs and display them
        var blogs = context.Blogs.ToList();
        foreach (var b in blogs)
        {
            Console.WriteLine($"Blog ID: {b.BlogId}, URL: {b.Url}");
        }
    }

    Console.ReadKey();
}

Conclusion

Entity Framework is a powerful ORM that simplifies data access in .NET applications. By using it, developers can focus on writing clean, maintainable code without worrying about the intricacies of raw SQL queries. With Entity Framework, you can interact with databases using familiar C# objects and LINQ queries, making it a popular choice for many developers in the .NET ecosystem.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-04-15.