Mastering Code First in Entity Framework Core: A Step-by-Step Beginner's Guide
Getting Started with Code First in EF Core
Today, we're exploring the Code First method using Entity Framework Core, a robust approach to managing databases within .NET applications. This guide will show you how to use EF Core’s Code First strategy to build your database directly from your C# classes—no manual database design needed.
Whether you're just getting started or aiming to strengthen your skills, this tutorial breaks down the key steps to help you structure your data models and auto-generate your database schema.
💡 What is Code First in EF Core?
The Code First approach allows you to define your data models (entities) using plain C# classes. EF Core then uses these definitions to automatically generate the database schema. This approach keeps your application logic and database structure in sync—perfect for developers who prefer writing code over dealing with SQL tables directly.
In simple terms:
You write the code first → EF Core builds the database for you.
🚀 Let's Build a Code First App in 6 Steps
Step 1: Install Required NuGet Packages
Make sure you have the following packages installed in your .NET project:
Microsoft.EntityFrameworkCore.DesignMicrosoft.EntityFrameworkCore.ToolsMicrosoft.EntityFrameworkCore.SqlServer
After installing, you’ll see them listed under Dependencies > Packages in your project.
Step 2: Create Your Model Class
Let’s create a sample entity model for a student:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace School.Models
{
public class CreateStudent
{
[Key]
public int StudentID { get; set; }
[Required]
[Column("Student Name", TypeName = "Varchar(80)")]
public string Name { get; set; }
[Column(TypeName = "Varchar(5)")]
public string Standard { get; set; }
[Column(TypeName = "Varchar(12)")]
public string Medium { get; set; }
[Column(TypeName = "Varchar(12)")]
public string Mobile { get; set; }
}
}
[Key]marks the primary key.[Required]ensures the property can’t be null.[Column]lets you define column names and types.
Step 3: Create a DbContext Class
Next, define the class that will manage your database operations:
using Microsoft.EntityFrameworkCore;
namespace School.Models
{
public class StudentDbContext : DbContext
{
public StudentDbContext(DbContextOptions<StudentDbContext> options)
: base(options)
{
}
public DbSet<CreateStudent> Students { get; set; }
}
}
Step 4: Configure DbContext in Program.cs
builder.Services.AddDbContext<StudentDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
Step 5: Add Connection String
Update appsettings.json with your SQL Server connection string:
"ConnectionStrings": {
"DefaultConnection": "Server=.;Database=SchoolDb;Trusted_Connection=True;"
}
Step 6: Run Migrations
In the Package Manager Console, run:
Add-Migration InitialCreate Update-Database
✅ You're Done!
Your database is now created using EF Core’s Code First approach. You can start building CRUD operations, APIs, or UIs on top of it!
Comments
Post a Comment