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
| NoSQL Integration (MongoDB, Redis) | Connection Pooling and Performance Tuning | |
🗄️ Database Migrations and Seeding in EF Core |
Migrations in EF Core are a way to keep your database schema in sync with your C# model classes. Instead of manually updating the database, EF Core generates migration scripts that apply schema changes incrementally.
// From Package Manager Console:
Add-Migration InitialCreate
Update-Database
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Products",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = table.Column<string>(nullable: true),
Price = table.Column<decimal>(nullable: false)
},
constraints: table => { table.PrimaryKey("PK_Products", x => x.Id); });
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable("Products");
}
}
Seeding is the process of populating the database with initial or default data. This is useful for inserting master data (e.g., roles, countries, admin accounts) when the database is first created or updated.
public class AppDbContext : DbContext
{
public DbSet<Product&ht; Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().HasData(
new Product { Id = 1, Name = "Laptop", Price = 1200 },
new Product { Id = 2, Name = "Phone", Price = 800 }
);
}
}
// From Package Manager Console:
Add-Migration SeedProducts
Update-Database
EF Core Migrations keep your database schema aligned with your code, while Seeding ensures essential data is available from the start. Together, they provide a robust way to manage schema evolution and application initialization in modern .NET projects.
| NoSQL Integration (MongoDB, Redis) | Connection Pooling and Performance Tuning | |