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

Entity Framework: Code First Approach in .NET

  🚀 Entity Framework: Code First Approach in .NET Entity Framework (EF) is a powerful Object-Relational Mapper (ORM) that allows developers to work with databases using C# classes instead of raw SQL queries . 🔷 What is Entity Framework? As discussed in the previous blog, Entity Framework simplifies database interaction by mapping C# objects to database tables , enabling clean, maintainable, and strongly typed data access. +-------------------+ +---------------------+ +------------------+ | C# Classes | ----> | Entity Framework | ----> | Database | | (Entities) | | (ORM Layer) | | (Tables/Rows) | +-------------------+ +---------------------+ +------------------+ 🔷 Entities and Domain Classes 📌 Entity Classes Entity classes represent database tables . Each object represents a row. Example: public class Student { public int Id { get ; set ; } public string Name { get ; set ; }...

C# Keywords

C# Keywords Many different reserved words or keywords are used in C# programming. They are categorised as described below. It is advised to properly get familiar with the categories, functionality, and usage of each of them before we move forward. If, somewhere, you feel you are not able to grasp the correct meaning, make a note of it and in the future specific examples where it is explicitly used, you would be able to recall and revise it. Some examples for certain keywords can be viewed at: Link 1. Data Types and Modifiers bool Explanation: Represents a Boolean value that can be either true or false . It is an alias for the system type System.Boolean . Example: bool isCoding = true; byte Explanation: Represents an 8-bit unsigned integer that can store values from 0 to 255. Example: byte age = 25; char Explanation: Represents a single 16-bit Unicode character. It is enclosed in single quotes. Example: char grade = 'A'; class Explanation: Used to declare a reference t...