🌱 Getting Started with Entity Framework Core: The Modern Way to Work with Databases in .NET

 

If you've ever felt bogged down by writing raw SQL or struggled to map database records to your .NET objects, Entity Framework Core (EF Core) is the tool you need. It's Microsoft's modern, open-source Object-Relational Mapper (ORM) that lets you work with databases using C# instead of SQL.

In this blog, we’ll explore what EF Core is, why it’s useful, and how to get started quickly with practical code examples.


πŸ” What Is Entity Framework Core?

EF Core is an ORM that maps your C# classes to database tables, so you can:

Query your database using LINQ
Automatically generate tables from models (Code First)
Keep your database schema in sync with your code
Avoid repetitive SQL queries

It supports SQL Server, PostgreSQL, SQLite, MySQL, and many others.


🏁 Why Use EF Core?

Feature

Benefit

LINQ Integration

Query your database using strongly-typed C# code

Migration Support

Evolve your schema without manual SQL scripts

Cross-platform

Works on Windows, Linux, and macOS

Performance Optimized

Lightweight and fast for most common scenarios


πŸ§ͺ Installation & Setup

Add EF Core and SQL Server provider to your project:

dotnet add package Microsoft.EntityFrameworkCore

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

dotnet add package Microsoft.EntityFrameworkCore.Tools


πŸ—️ Define Your Model

csharp


public class Product

{

    public int Id { get; set; }

    public string Name { get; set; }

    public decimal Price { get; set; }

}

Create a DbContext:

csharp

public class AppDbContext : DbContext

{

    public DbSet<Product> Products { get; set; }

 

    protected override void OnConfiguring(DbContextOptionsBuilder options)

        => options.UseSqlServer("YourConnectionStringHere");

}


πŸš€ Code First: Migrations

Migrations are how EF Core manages schema changes:

1. Create the database schema:

dotnet ef migrations add InitialCreate

2. Apply the migration:


dotnet ef database update

Boom πŸ’₯ — EF Core will create your database and tables automatically.


πŸ”Ž Querying Data with LINQ

csharp


using var context = new AppDbContext();

 

// Read

var products = context.Products.ToList();

 

// Filter

var cheapProducts = context.Products

    .Where(p => p.Price < 100)

    .ToList();

 

// Add

context.Products.Add(new Product { Name = "Keyboard", Price = 49.99m });

context.SaveChanges();


πŸ”„ Updating and Deleting Data

Update:

csharp


var product = context.Products.First();

product.Price = 29.99m;

context.SaveChanges();

Delete:

csharp


context.Products.Remove(product);

context.SaveChanges();


🧠 Best Practices

  • Use AsNoTracking() for read-only queries to improve performance.
  • Use Fluent API in OnModelCreating for advanced mappings.
  • Always validate migrations before applying in production.
  • Keep DbContext lifetime scoped in dependency injection (AddDbContext).

⚠️ Common Pitfalls

Mistake

Why it's a problem

Ignoring async queries

Leads to blocking and performance issues

Overusing .Include() eagerly

Can cause huge joins and slow queries

Skipping migrations

Makes schema drift likely


πŸ› ️ Real-World Use Case

EF Core is perfect for:

  • ASP.NET Core Web APIs
  • Background services
  • Microservices
  • Cross-platform apps

Pair it with Clean Architecture and you’ve got a scalable backend foundation.


πŸ“š Summary

EF Core lets you write modern, maintainable, and type-safe data access code. With powerful LINQ support, migration tools, and cross-database compatibility, it's a solid choice for any .NET developer.

 

Comments

Popular posts from this blog

Scrutor the built-in Dependency Injection (DI)

πŸ§… Understanding the Onion Architecture: A Clean Approach to Building Scalable Applications

πŸ”Œ Extension Methods in C#: Power Up Your Code Without Modifying It

Understanding Dependency Injection: A Modern Guide for Developers

🌐 CORS in .NET Explained: Solving the Cross-Origin Problem Like a Pro

Ensuring Data Integrity: The Backbone of Reliable Systems

πŸ” JWT (JSON Web Token) Explained: Secure Your APIs the Modern Way

πŸ”— SQL JOINs Explained: Mastering Table Relationships

πŸ—‚️ DROP vs DELETE vs TRUNCATE in SQL: What’s the Difference?

πŸ›‘️ SIEM Logs Explained: How to Build Secure and Auditable .NET Apps