Fundamentals of Entity Framework: Simplifying Data Access in .NET

fabrcio_marcondessantos

Fabrício Marcondes Santos

Posted on July 17, 2024

Fundamentals of Entity Framework: Simplifying Data Access in .NET

Introduction
Imagine you are building a large library of books. Organizing, cataloging, and finding information quickly can be a challenge. Now, think of Entity Framework (EF) as an intelligent librarian that makes this process easy for your .NET applications.

Entity Framework is an Object-Relational Mapper (ORM) that allows developers to work with a database using .NET objects, eliminating the need to write most of the data access code. Let's explore the fundamentals of Entity Framework and understand why it is such a powerful tool.

What is Entity Framework?
Entity Framework is a Microsoft technology for .NET that automates the mapping between domain objects and relational database tables. With it, you can:

  • Define the data model as C# classes.
  • Query and manipulate data using LINQ (Language Integrated Query).
  • Manage database versioning through migrations.

Key Components of Entity Framework

  1. DbContext: It is the main point of interaction with Entity Framework. It manages the connection to the database and is used to query and save data.
public class LibraryContext : DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }
}
Enter fullscreen mode Exit fullscreen mode
  1. DbSet: Represents a collection of entities in the context, i.e., a table in the database.
public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public int AuthorId { get; set; }
    public Author Author { get; set; }
}
Enter fullscreen mode Exit fullscreen mode
  1. Entity Models: These are classes that represent database tables. They are mapped to columns in the database.

Basic Configuration
To start using Entity Framework, you need to configure the DbContext. Here is an example of how to configure the database connection:

public class LibraryContext : DbContext
{
    public LibraryContext(DbContextOptions<LibraryContext> options)
        : base(options)
    {
    }

    public DbSet<Book> Books { get; set; }
    public DbSet<Author> Authors { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

And in the configuration file:

{
  "ConnectionStrings": {
    "LibraryDatabase": "Server=(localdb)\\mssqllocaldb;Database=LibraryDb;Trusted_Connection=True;"
  }
}
Enter fullscreen mode Exit fullscreen mode

Querying Data
With Entity Framework, you can query data using LINQ, which is a powerful and expressive way to write queries:

using (var context = new LibraryContext())
{
    var books = context.Books
        .Include(b => b.Author)
        .Where(b => b.Title.Contains("C#"))
        .ToList();
}
Enter fullscreen mode Exit fullscreen mode

Migrations
Migrations allow you to manage changes to the database schema over time. You can create a migration using the command:

dotnet ef migrations add InitialCreate
Enter fullscreen mode Exit fullscreen mode

And then apply the migrations to the database:

dotnet ef database update
Enter fullscreen mode Exit fullscreen mode

Conclusion
Entity Framework is an essential tool for .NET developers who want to simplify data access and focus more on the business logic of their applications. With its mapping power, LINQ queries, and database migrations, it transforms application development into a more efficient and enjoyable task.

💖 💪 🙅 🚩
fabrcio_marcondessantos
Fabrício Marcondes Santos

Posted on July 17, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related