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:
{
"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.
Recommended by LinkedIn
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:
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! 🚀
Senior QA Automation Engineer | SDET | Java | Selenium | Rest Assured | Robot Framework | Cypress | Appium
5moVery informative
.NET Developer | C# | TDD | Angular | Azure | SQL
5moInteresting Cássio Huggentobler!
FullStack Developer @ Itaú Digital Assets | Go | TS | Blockchain | Aws
5moVery helpful
Senior Software Engineer | Full Stack Developer | C# | .NET | .NET Core | React | Amazon Web Service (AWS)
5moVery informative
Software Engineer | Full Stack Developer | C# | React | Angular | Azure
5moGreat advice