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
Beginner → Intermediate → Advanced → Professional
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
- Add Category to ShopNest.Domain and link Product.CategoryId + navigation.
- Build the solution before running migrations add.
- Compare Code First to database-first — list one advantage of each for your team.
- Add a Slug string to Category and preview migration SQL before apply.
- 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.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!