The Object-Relational Divide
Software developers write systems using Object-Oriented Programming (OOP) classes containing behavior and state. Relational databases store data in tables. Mapping objects to rows (the Object-Relational Impedance Mismatch) traditionally required writing boilerplate data access code.
Microsoft's early Object-Relational Mapper (ORM), Entity Framework 3.5, was heavily criticized for forcing developers to inherit classes from complex Microsoft base classes.
The release of Entity Framework 4.0 (EF4) in .NET 4 represents a major step forward for enterprise data architectures.
Key Improvements in EF4
EF4 addresses major developer pain points:
1. Plain Old CLR Objects (POCO) Support
Developers can now define clean, domain-driven class entities without any dependencies on the Entity Framework framework:
public class Customer {
public int CustomerId { get; set; }
public string Name { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}2. Lazy Loading
EF4 supports transparent lazy loading. Navigation properties are fetched from the database automatically on access, reducing initial query complexity.
3. Model-First and Code-First Workflows
Instead of designing databases first, EF4 supports:
- ◆Model-First: Designing models in a visual designer and generating the SQL database.
- ◆Code-First (early previews in late 2010): Writing code classes first and allowing the framework to create database schemas dynamically.
Performance Guidelines
ORMs provide productivity, but developers must manage:
- ◆The N+1 Query Problem: Lazy loading inside a loop can fire dozens of database queries. Use eager loading (
.Include()) to fetch related data in a single SQL query. - ◆Change Tracking Overhead: Disable object tracking (
MergeOption.NoTracking) for read-only queries to speed up execution times.