Time to Talk About Data: Introduction to Entity Framework 📊💻

Time to Talk About Data: Introduction to Entity Framework 📊💻

If you’re advancing in your .NET journey, sooner or later, you’ll encounter one of the most important pillars of software development: data. Data is at the core of essential features, like registrations, reports, and so much more.

To make our lives easier as developers, .NET offers a powerful tool: Entity Framework (EF). This tool simplifies (a lot!) how we work with databases, bridging the gap between code and data with practicality and efficiency.

Today, we’ll dive into how to get started with EF, configure it for the database you’ll use, and learn the first steps to define and manage your data classes.


What Is Entity Framework?

Entity Framework is an Object-Relational Mapper (ORM). It connects the object-oriented world of .NET with relational databases, allowing you to work directly with C# classes while EF handles the translation into tables, columns, and SQL commands behind the scenes.

One common approach in EF is creating the database structure directly from code. This means you define your data structure as classes, and EF automatically generates the corresponding tables in the database.


Setting Up Entity Framework

Before we start using EF, there are a few important configurations to make:

  1. Choose a Database Provider: EF needs to know which database you’re using (SQL Server, MySQL, PostgreSQL, etc.). For that, you install the corresponding NuGet package. For example:
  2. Configure the Connection String: The connection string tells EF where the database is and how to connect to it. Typically, this is configured in the appsettings.json file (for .NET Core projects):

{
    "ConnectionStrings": {
        "MyDatabase": "Server=localhost;Database=MyDatabase;User Id=yourUser;Password=yourPassword;"
    }
}        

3. Set Up the Context to Use the Connection:To access the connection string from the appsettings.json, configure your DbContext class using Dependency Injection and the IConfiguration service. Here’s how:

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

public class MyContext : DbContext
{
    private readonly IConfiguration _configuration;

    public MyContext(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public DbSet<Client> Clients { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var connectionString = _configuration.GetConnectionString("MyDatabase");
        optionsBuilder.UseSqlServer(connectionString);
    }
}        

With this setup, EF reads the connection string from the appsettings.json file, making it easy to manage and change without modifying the code.


Defining the Data Structure

Now that everything is configured, let’s create an entity called Client. This entity will be transformed into a database table by EF.

Defining the Class

public class Client
{
    public int Id { get; set; } // Primary key
    public string Name { get; set; } // Client's name
    public string Email { get; set; } // Client's email
    public DateTime RegistrationDate { get; set; } // Registration date
}        

Adding the Entity to the Context

The DbSet<Client> in the context informs EF that this class should be managed and translated into a database table:

public DbSet<Client> Clients { get; set; }        

With this setup, Entity Framework will create a Clients table in the database with columns based on the class properties.


Why Use Entity Framework?

EF brings several benefits to your development workflow:

  1. Less Manual Work: You don’t need to create tables, columns, or relationships manually—EF handles it all.
  2. Easy Evolution: Need to change the structure? Just adjust the class and apply the changes with migrations.
  3. Abstraction: You work with objects and classes, leaving the SQL management details to EF.
  4. Productivity: Less repetitive code, more focus on solving business problems.
  5. Multi-Platform: It supports various databases (SQL Server, MySQL, PostgreSQL, SQLite, etc.).


Next Steps

Now that you understand the basics, the next step is to run your application and let EF generate the database for you. From there, you can explore more advanced features like migrations, relationships between tables, and dynamic queries.

Have you used Entity Framework before? What challenges or insights did you have when starting out? Let’s discuss in the comments! 🚀


Daivid Simões

Senior QA Automation Engineer | SDET | Java | Selenium | Rest Assured | Robot Framework | Cypress | Appium

5mo

Very informative

Like
Reply
Lucas Wolff

.NET Developer | C# | TDD | Angular | Azure | SQL

5mo

Interesting Cássio Huggentobler!

Like
Reply
Jefferson Luiz

FullStack Developer @ Itaú Digital Assets | Go | TS | Blockchain | Aws

5mo

Very helpful

Like
Reply
Leandro Veiga

Senior Software Engineer | Full Stack Developer | C# | .NET | .NET Core | React | Amazon Web Service (AWS)

5mo

Very informative

Like
Reply
Paulo Henrique De Araujo Gerchon

Software Engineer | Full Stack Developer | C# | React | Angular | Azure

5mo

Great advice

Like
Reply

To view or add a comment, sign in

More articles by Cássio Huggentobler de Costa

Insights from the community

Others also viewed

Explore topics