Skip to main content

Entity Framework: Database First Approach in .NET

Getting Started with Entity Framework: Database First Approach in .NET

Entity Framework (EF) is a powerful Object-Relational Mapper (ORM) that allows developers to work with databases using C# objects instead of writing raw SQL queries.


🔷 What is Entity Framework?

Entity Framework simplifies database interaction by mapping database tables to C# classes, enabling clean and maintainable code.

+-------------------+       +---------------------+       +------------------+
|   C# Classes      | ----> | Entity Framework    | ----> |   Database       |
| (Entities)        |       | (ORM Layer)         |       | (Tables/Rows)    |
+-------------------+       +---------------------+       +------------------+

🔷 Entities and Domain Classes

📌 Entity Classes

Entity classes represent database tables. Each object corresponds to a row in the table.

📌 Domain Classes

Domain classes represent business logic and real-world entities.

  • Entities: Have primary keys
  • Value Objects: No identity
  • Aggregates: Group of related objects
Domain Layer
   |
   |--- Student (Entity)
   |--- Address (Value Object)
   |--- Course (Aggregate)

🔷 Entity Framework Approaches

Approach Description Use Case
Code First Start with C# classes New applications
Database First Start with existing database Legacy systems

🚀 Hands-on: Database First Approach

Step 1: Create Database

CREATE DATABASE newdb;

CREATE TABLE student (
    Id INT PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(100),
    Age INT
);

Step 2: Install NuGet Packages

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Tools
  • Pomelo.EntityFrameworkCore.MySql

Step 3: Scaffold Database

Scaffold-DbContext "server=localhost;database=newdb;user=root;password=;" 
Pomelo.EntityFrameworkCore.MySql -OutputDir Models -Force

📊 Diagram: Database First Workflow

Existing Database
       |
       v
Scaffold Command
       |
       v
Auto Generated Models
       |
       v
DbContext Class
       |
       v
C# Application (CRUD Operations)

🔧 CRUD Application Flow

User Input
   |
   v
Program.cs (Menu)
   |
   v
DbContext
   |
   v
Database (Insert / Update / Delete / Fetch)

💻 Sample CRUD Code

NewdbContext context = new NewdbContext();

Student s = new Student();
s.Name = "John";
s.Age = 20;

context.Students.Add(s);
context.SaveChanges();

🎯 Key Learnings

  • Understand ORM concept
  • Database to Code mapping
  • CRUD operations without SQL
  • Real-world backend development

📌 When to Use Database First?

  • Working with existing databases
  • Maintaining legacy systems
  • Database controlled externally

📎 Source Code

👉 View GitHub Repository


🏁 Final Thoughts

Entity Framework reduces development complexity and improves productivity. While Code First is preferred for modern applications, Database First is essential for working with legacy systems and real-world enterprise applications.

Comments

Popular posts from this blog