π± 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
Post a Comment