IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| Dead Letter Queues | Entity Framework Core, code first | |
ποΈ Entity Framework Core (EF Core) |
Entity Framework Core (EF Core) is a modern, lightweight, open-source, and cross-platform Object-Relational Mapper (ORM) for .NET. It allows developers to interact with databases using strongly typed C# objects instead of writing raw SQL queries. EF Core supports multiple database providers such as SQL Server, PostgreSQL, MySQL, SQLite, and Cosmos DB.
Hereβs a simple example of using EF Core with a DbContext and a model:
// Install-Package Microsoft.EntityFrameworkCore
// Install-Package Microsoft.EntityFrameworkCore.SqlServer
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
// Model
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
// DbContext
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=.;Database=DemoDb;Trusted_Connection=True;");
}
}
// Usage
public class Program
{
public static async Task Main()
{
using var context = new AppDbContext();
// Create
context.Products.Add(new Product { Name = "Laptop", Price = 1200 });
await context.SaveChangesAsync();
// Read
var products = await context.Products.ToListAsync();
foreach (var p in products)
Console.WriteLine($"{p.Id}: {p.Name} - {p.Price}");
}
}
EF Core is a powerful ORM that simplifies database access in .NET applications. Itβs ideal for CRUD-heavy applications, microservices, and enterprise systems. With proper optimization and best practices, EF Core balances productivity with performance in modern .NET development.
- Use Code First for new, agile projects. - Use Database First when working with an existing schema. - Model First is legacy (EF6), not supported in EF Core.
| Aspect | Code First | Database First | Model First (Classic EF6) |
|---|---|---|---|
| Definition | Define C# classes first, EF generates database schema via migrations. | Start with an existing database, scaffold DbContext and entities. | Design a visual model (EDMX), EF generates both DB schema and classes. |
| Best For | Greenfield projects, agile schema evolution. | Legacy databases, DBA-driven environments. | Visual-first design, teams preferring diagrams. |
| Tooling | Migrations (Add-Migration, Update-Database). | Scaffold-DbContext command. | EDMX Designer (not available in EF Core). |
| Flexibility | High β schema evolves with code. | Low β schema changes require re-scaffolding. | Medium β schema tied to visual model. |
| Supported in EF Core? | β Yes | β Yes | β No (only in EF6, not EF Core) |
| Advantages | Strongly typed, migrations, agile-friendly. | Aligns with existing DB, quick setup for legacy systems. | Visual modeling, auto-generation of DB + classes. |
| Disadvantages | Not ideal for existing DBs, requires migration discipline. | Less flexible, generated code may need customization. | No longer supported in EF Core, limited to EF6. |
- Use Code First when starting fresh and you want your C# models to drive the database. - Use Database First when working with an existing schema or DBA-managed database. - Model First was available in EF6 with EDMX diagrams, but is not supported in EF Core. In EF Core, βModel Firstβ is effectively achieved through Code First + Migrations.
| Dead Letter Queues | Entity Framework Core, code first | |