Tutorials Entity Framework Core Tutorial

Entity Classes in EF Core

Entity Classes in 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 12 of 100

Entity Classes in EF Core

BeginnerIntermediateAdvancedProfessional

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

What is this?

Entity classes are persistence-focused C# types EF maps to tables. Scalar properties become columns; navigation properties express relationships to other entities.

Why should you care?

Clear ShopNest entities (Customer, Order, Product) keep queries readable and stop duplicate email columns scattered across tables.

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 Customer
{
    public int Id { get; set; }
    public string Email { get; set; } = "";
    public string FullName { get; set; } = "";
    public ICollection<Order> Orders { get; set; } = new List<Order>();
}

public class Order
{
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public Customer Customer { get; set; } = null!;
    public DateTime OrderDate { get; set; }
}

What happened?

  • Id is PK by convention.
  • CustomerId on Order is FK.
  • Customer navigation is many-side; Orders collection is one-to-many pairing.

Practice next

  1. Place entities in ShopNest.Domain without EF attributes initially.
  2. Draw ER diagram: Customer —< Order —< OrderItem >— Product.
  3. Avoid putting API DTO fields on entities — keep persistence separate.
  4. Switch ICollection to List and confirm EF still maps the relationship.
  5. Add a computed UI field in a separate ProductDto class instead of on Product.

Remember

Entities represent tables in C#. FK + navigation pairs define relationships. Keep entities free of UI concerns.

ShopNest domain model clarity

Team models Customer and Order in Domain project consumed by Infrastructure and API layers.

Outcome: New hires read entities to understand core commerce relationships quickly.

Interview prep for this lesson

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

Mid PDF Detailed
How do you reverse engineer a database in EF Core (DB First) into entity classes & DbContext?
Short answer: Use the CLI or Package Manager Console: dotnet ef dbcontext scaffold &quot;YourConnectionString&quot; Microsoft.EntityFrameworkCore.SqlServer -o Models OR Scaffold-DbContext &quot;YourConnectionString&quot;…
Junior PDF Detailed
What is Entity Framework Core?
Short answer: Entity Framework Core (EF Core) is a modern, lightweight, cross-platform, open-source ORM (Object-Relational Mapper) for .NET. It allows developers to interact with databases using C# objects rather than SQ…
Mid PDF Detailed
What are the main differences between Entity Framework (EF6) and EF Core?
Short answer: Feature EF6 EF Core Platform .NET Framework only Cross-platform (.NET Core) Performance Slower Faster and more optimized LINQ support Limited Enhanced Change tracking Basic More efficient Migrations Availab…
Mid PDF Detailed
What are the different entity states?
Short answer: EF Core uses these EntityState values: State Description Added New entity to be inserted into the database Modified Entity has been changed and will be updated Deleted Entity will be deleted from the databa…
Mid PDF Detailed
What are the different entity states?
Short answer: dded New entity to be inserted into the database Modified Entity has been changed and will be updated Deleted Entity will be deleted from the database Detached Entity is not tracked by the context Unchange…
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