## **Entity Framework (EF) Core – Overview**
**Entity Framework Core (EF Core)** is a modern, open-source, lightweight, and
cross-platform **Object-Relational Mapper (ORM)** developed by **Microsoft**. It
enables .NET developers to interact with databases using **.NET objects**,
eliminating most of the need to write raw SQL queries.
---
### **1. Key Concepts**
* **ORM (Object-Relational Mapping):**
Converts data between **relational databases** and **object-oriented**
programming languages such as C#.
* Allows developers to work with database data as **objects** and **classes**.
* Automatically handles CRUD (Create, Read, Update, Delete) operations.
* **DbContext:**
The primary class that acts as a **bridge** between the database and the .NET
application.
* Manages entity objects during runtime.
* Tracks changes and handles database interactions.
* **Entities:**
These are **C# classes** representing database tables.
* Each property maps to a column.
* Each instance represents a row in the database.
* **Migrations:**
EF Core provides **migrations** to update the database schema as your model
evolves, without losing existing data.
* Uses commands like `Add-Migration` and `Update-Database`.
---
### **2. Core Features**
* **Cross-Platform:**
Works on **Windows, Linux, and macOS**, supporting .NET 6, .NET 7, and beyond.
* **Multiple Database Providers:**
Supports various databases such as:
* Microsoft SQL Server
* SQLite
* PostgreSQL (via Npgsql)
* MySQL
* Oracle
* Cosmos DB
* **LINQ Integration:**
EF Core allows querying databases using **Language Integrated Query (LINQ)**.
* Example:
```csharp
var students = [Link]
.Where(s => [Link] > 18)
.ToList();
```
* **Change Tracking:**
EF Core automatically tracks changes in objects and updates the database when
`SaveChanges()` is called.
* **Concurrency Handling:**
Prevents data conflicts in multi-user environments by using concurrency tokens
(e.g., timestamps or version columns).
* **Lazy, Eager, and Explicit Loading:**
EF Core provides flexible options for loading related data.
* **Lazy Loading:** Loads related data when accessed.
* **Eager Loading:** Loads related data immediately with `Include()`.
* **Explicit Loading:** Manually load related data using methods like `Load()`.
---
### **3. Workflow Overview**
1. **Define Model Classes:**
Create C# classes that represent database tables.
2. **Configure DbContext:**
Set up the connection string and entity configurations.
3. **Apply Migrations:**
Generate and apply migrations to create or update the database schema.
4. **Perform CRUD Operations:**
Use LINQ queries and `DbSet` methods like `Add()`, `Find()`, `Update()`, and
`Remove()`.
---
### **4. Advantages**
* Reduces boilerplate SQL code.
* Strongly-typed data access.
* Easier maintenance and refactoring.
* Works with domain-driven design (DDD) principles.
* Integrates well with [Link] Core applications.
---
### **5. Limitations**
* May have slower performance for extremely large-scale or complex queries.
* Requires understanding of how EF Core translates LINQ to SQL.
* Advanced queries might still need raw SQL for optimization.
---
### **6. Summary**
EF Core simplifies database access in .NET applications by providing a **high-level
abstraction layer** over traditional SQL operations. It promotes cleaner,
maintainable, and scalable code while supporting modern development practices like
**code-first**, **migrations**, and **asynchronous programming**. With continuous
updates and strong community support, EF Core is the **preferred ORM** for most
modern .NET developers.
---
*(≈500 words)*