Entity Framework 4
Overview
Entity Framework (EF) 4 is an object‑relational mapping (ORM) framework that enables .NET developers to work with relational data using domain-specific objects. EF 4 ships as part of the .NET Framework 3.5 Service Pack 1 and introduces a range of improvements over the initial release.
Key Features
- Support for POCO (Plain Old CLR Objects) and self‑tracking entities.
- Improved LINQ to Entities performance and query translation.
- Support for complex types, enums and inheritance mapping strategies.
- Lazy loading and eager loading capabilities.
- Enhanced designer experience with Model First workflow.
- Better integration with Visual Studio 2008 SP1.
System Requirements
Operating System | Windows XP SP2 or later, Windows Server 2003 SP2 or later |
---|---|
.NET Framework | .NET Framework 3.5 SP1 (including EF 4) |
IDE | Visual Studio 2008 SP1 or later |
Getting Started
- Install .NET Framework 3.5 SP1 which includes EF 4.
- Create a new Console Application or ASP.NET Web Application project in Visual Studio.
- Right‑click the project, choose Add → New Item → ADO.NET Entity Data Model.
- Select “EF Designer from database” or “Empty EF Designer Model” to generate the EDMX file.
- Configure the connection string in
App.config
orWeb.config
. - Start querying the model using LINQ or Entity SQL.
Code Sample
Below is a simple example that demonstrates how to query a Products
table using LINQ to Entities.
// Assuming the EDMX generated context is named SampleContext
using (var ctx = new SampleContext())
{
var cheapProducts = ctx.Products
.Where(p => p.Price < 20)
.OrderBy(p => p.Name)
.ToList();
foreach (var p in cheapProducts)
{
Console.WriteLine($"{p.ProductID}: {p.Name} - ${p.Price}");
}
}