What is entity framework
Object relational mapper. ORM
Maps .net objects to database tables
Generates sql, query via LINQ
Simplify CRUD
Change tracking
“Entity Framework Core maps .NET objects to database tables and handles translating C# operations into SQL commands automatically.”
What is DbContext
Unit of work object
It can change tracking. Query. Save changes
Dbset
DbSet represents a table in the database.
It lets you query, add, update, and delete records.
Each DbSet is a collection of entities of a specific type.
What is entity
An Entity is a class that maps to a database table.
Each object of the class represents a row in that table.
Entities have properties that map to columns.
Change tracking
Change Tracking monitors entities for changes.
EF remembers what’s added, modified, or deleted.
When you call SaveChanges(), EF applies only those changes to the database.
Lazy loading
Lazy Loading loads related data only when accessed.
EF does not fetch related entities until you use them.
This saves memory but can cause multiple database queries.
Eager Loading
Eager Loading loads related data immediately with the main entity.
Use Include() to fetch related tables in a single query.
It reduces multiple database calls.
Explicit loading
Explicit Loading manually loads related data after the main entity is fetched.
You call methods like Load() on a DbSet or navigation property.
It gives control over when data is fetched.
Primary key
A Primary Key uniquely identifies each entity in a table. EF requires a key to track and update records. It is usually a property named Id or [ClassName]Id.
Foreign key
Question: What is Foreign Key in Entity Framework?
Answer: A Foreign Key links two tables. It stores the primary key of another table. EF uses it to establish relationships between entities.