Step 6 of 11
EF Core code-first: Departments, Employees, CRUD, migrations
Workshop 2: ฐานข้อมูลพนักงานด้วย EF Core — ฝึก code-first, migrations, LINQ และ relationships
Scenario: Your company's HR team manages employee data in spreadsheets. It's error-prone and hard to query. Build a database-backed tool that stores departments and employees, with proper relationships and data integrity.
A .NET console application with Entity Framework Core that manages employee records — create, read, update, and delete (CRUD) operations against a PostgreSQL database.
dotnet ef)Create two entities with a one-to-many relationship:
Department (1) ─── (*) Employee
- Id (int, PK) - Id (int, PK)
- Name (string) - FirstName (string)
- Code (string) - LastName (string)
- Email (string)
- Salary (decimal)
- DepartmentId (int, FK)
- HireDate (DateTime)
Configure EF Core with PostgreSQL provider
Create and apply a migration: dotnet ef migrations add InitialCreate && dotnet ef database update
Implement CRUD operations via a CLI menu:
=== Employee Records ===
1. List all employees (with department name)
2. Add employee
3. Update employee salary
4. Delete employee
5. List employees by department
6. Exit
Use AsNoTracking() for read-only queries
Validate email format before saving — reject invalid emails with a clear error
--export option that writes all employees to a CSV fileThis database schema and CRUD foundation becomes the Employee Service in your Payroll & Benefits Management System. The Employee entity and Department relationship carry directly into the final system.