Tutorials Entity Framework Core Tutorial

Code First Introduction — EF Core

Code First Introduction — EF Core: free step-by-step lesson with examples, common mistakes, and interview tips — part of Entity Framework Core Tutorial on Toolliyo Academy.

On this page

Entity Framework Core Tutorial · Lesson 11 of 100

Code First Introduction

BeginnerIntermediateAdvancedProfessional

Beginner · 1 — Foundations · ~6 min · Module 2: Code First Approach

What is this?

Code First means C# entity classes are the source of truth for schema. EF Core compares the model to the database and migrations capture diffs as versioned C# files.

Why should you care?

ShopNest schema evolves in pull requests alongside API code — reviewers see Category and Product changes in Git, not mystery SSMS edits.

See it live — copy this example

Paste into a .NET project with EF Core packages, then run with LocalDB/SQL Server (dotnet ef / dotnet run).

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public ICollection<Product> Products { get; set; } = new List<Product>();
}

// Next: dotnet ef migrations add AddCategories

What happened?

  • Category defines columns by convention (Id key, Name string).
  • ICollection signals a one-to-many relationship EF will map with CategoryId FK on Product.

Practice next

  1. Add Category to ShopNest.Domain and link Product.CategoryId + navigation.
  2. Build the solution before running migrations add.
  3. Compare Code First to database-first — list one advantage of each for your team.
  4. Add a Slug string to Category and preview migration SQL before apply.
  5. Remove a property and observe how the next migration drops a column.

Remember

Code First drives schema from C# types. Migrations version every schema change. Model and app code stay in sync in Git.

ShopNest schema in PR review

Developer adds Category entity and commits InitialShopNest migration for team review.

Outcome: DBA signs off on Up() SQL before dotnet ef database update hits shared dev SQL.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Junior PDF Detailed
What is Code First approach?
Short answer: Advantages and when to use it Code First is an EF Core approach where you define your database schema using C# classes, and EF Core generates the database from your code. ✅ Advantages: Full control via C# c…
Junior PDF Detailed
What is Code First approach?
Short answer: Code First is an EF Core approach where you define your database schema using C# classes, and EF Core generates the database from your code. ✅ Advantages: Full control via C# code (POCOs) Easily version-con…
Mid PDF Detailed
How does Code First handle schema changes in the database?
Short answer: EF Core uses migrations to track and apply schema changes: You modify your C# models. Use Add-Migration to create a migration class. Use Update-Database to apply the changes to the DB. EF Core compares the…
Junior PDF Detailed
What is Database First approach?
Short answer: Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy databases ❌ Cons: Harder to vers…
Junior PDF Detailed
What is Database First approach?
Short answer: Use‑cases and pros and cons Database First involves generating C# entity classes and a DbContext from an existing database. ✅ Pros: Quick start if the DB already exists Ensures model aligns with legacy data…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Entity Framework Core Tutorial
Course syllabus

Entity Framework Core Tutorial

Module 1: EF Core Fundamentals
Module 2: Code First Approach
Module 3: CRUD Operations
Module 4: LINQ
Module 5: Relationships
Module 6: Advanced EF Core
Module 7: Performance Optimization
Module 8: Enterprise Architecture
Module 9: Testing & Debugging
Module 10: Real-World Projects
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details