✅ 1. What is Entity Framework Core? (Interview-ready definition)
EF Core is Microsoft’s Object-Relational Mapper (ORM) that lets you interact with a database using .NET classes instead of SQL queries.
It handles:
-
Database Connection & Commands
-
Mapping classes ↔ tables
-
CRUD operations
-
Migrations
-
Change Tracking
-
LINQ queries
Interview one-liner:
EF Core is an ORM that allows developers to work with databases using C# objects, eliminating most SQL and improving productivity while maintaining performance.
✅ 2. EF Core Workflow (Very Important for Interviews)
✅ 3. Hands-On Practical Example (Minimal, Perfect for Learning)
Let’s create a simple Employee Management app with EF Core.
🟦 Step 1: Install EF Core Packages
Run in terminal:
🟦 Step 2: Create Model Class
🟦 Step 3: Create DbContext
🟦 Step 4: Create Database Using Migrations
This creates your database + Employees table.
In interviews, they often ask:
What are migrations?
Answer:
Migrations help you evolve your database schema over time while keeping data safe.
🟦 Step 5: Perform CRUD Operations
▶ Insert Data
▶ Read Data (LINQ)
▶ Update
▶ Delete
✅ 4. Frequently Asked EF Core Interview Questions (with answers)
1. What is DbContext?
DbContext is the main class that manages database connections, CRUD operations, and mapping between classes and tables.
2. What is DbSet?
DbSet represents a table. It lets you query and save instances of a model.
3. What is Change Tracking?
EF Core automatically tracks object changes and updates only modified fields.
4. What are Migrations?
Migrations manage schema changes without dropping the database.
5. What is Lazy Loading, Eager Loading, Explicit Loading?
-
Eager Loading: Includes related data immediately using
.Include() -
Lazy Loading: Related data is loaded automatically when accessed
-
Explicit Loading: Manually load related data using
Entry(entity).Collection().Load()
6. How to use LINQ with EF Core?
Example:
✅ 5. Relationship Example (Highly asked in interviews)
Example: One-to-Many
Employee → Projects
One employee can have many projects.
Models:
DbContext Mapping:
Query with include:
No comments:
Post a Comment