Concepts: Data Access - ADO.NET - Entity Framework
This section delves into the powerful querying capabilities of Entity Framework (EF), a popular Object-Relational Mapper (ORM) for .NET. EF allows developers to interact with a relational database using .NET objects, abstracting away much of the complexity of SQL.
Entity Framework leverages Language Integrated Query (LINQ) to provide a type-safe and expressive way to query your data. LINQ to Entities translates your LINQ queries into SQL queries that are executed against your database.
Here's a simple example of how to retrieve all customers from a database:
using (var context = new YourDbContext())
{
var allCustomers = from c in context.Customers
select c;
foreach (var customer in allCustomers)
{
Console.WriteLine($"Name: {customer.Name}, Email: {customer.Email}");
}
}
Alternatively, you can use the fluent method syntax:
using (var context = new YourDbContext())
{
var allCustomers = context.Customers.ToList();
foreach (var customer in allCustomers)
{
Console.WriteLine($"Name: {customer.Name}, Email: {customer.Email}");
}
}
The Where
clause or method is used to filter results based on specific criteria.
using (var context = new YourDbContext())
{
var customersInLondon = from c in context.Customers
where c.City == "London"
select c;
// Or using method syntax:
// var customersInLondon = context.Customers.Where(c => c.City == "London").ToList();
foreach (var customer in customersInLondon)
{
Console.WriteLine($"Name: {customer.Name}, City: {customer.City}");
}
}
Use the OrderBy
and OrderByDescending
clauses or methods to sort your results.
using (var context = new YourDbContext())
{
var sortedCustomers = from c in context.Customers
orderby c.Name ascending
select c;
// Or using method syntax:
// var sortedCustomers = context.Customers.OrderBy(c => c.Name).ToList();
foreach (var customer in sortedCustomers)
{
Console.WriteLine($"Name: {customer.Name}");
}
}
Projection allows you to select specific properties or create anonymous types from your query results.
using (var context = new YourDbContext())
{
var customerContactInfo = from c in context.Customers
select new { c.Name, c.Email };
// Or using method syntax:
// var customerContactInfo = context.Customers.Select(c => new { c.Name, c.Email }).ToList();
foreach (var info in customerContactInfo)
{
Console.WriteLine($"Name: {info.Name}, Email: {info.Email}");
}
}
Entity Framework supports joins between related entities, often automatically inferred based on your model's relationships.
using (var context = new YourDbContext())
{
var customerOrders = from c in context.Customers
join o in context.Orders on c.CustomerId equals o.CustomerId
select new { CustomerName = c.Name, OrderId = o.OrderId, OrderDate = o.OrderDate };
// Or using method syntax:
// var customerOrders = context.Customers
// .Join(context.Orders, c => c.CustomerId, o => o.CustomerId, (c, o) => new { CustomerName = c.Name, OrderId = o.OrderId, OrderDate = o.OrderDate })
// .ToList();
foreach (var item in customerOrders)
{
Console.WriteLine($"Customer: {item.CustomerName}, Order ID: {item.OrderId}, Date: {item.OrderDate}");
}
}
EF also provides mechanisms to call database functions and stored procedures directly.
Understanding how related data is loaded is crucial for performance. Entity Framework offers:
Include()
.
using (var context = new YourDbContext())
{
var customersWithOrders = context.Customers
.Include(c => c.Orders) // Eagerly load the Orders navigation property
.ToList();
foreach (var customer in customersWithOrders)
{
Console.WriteLine($"Customer: {customer.Name}");
foreach (var order in customer.Orders)
{
Console.WriteLine($"- Order ID: {order.OrderId}, Date: {order.OrderDate}");
}
}
}
Include()
) when you know you will need the related data to avoid multiple round trips to the database.
Entity Framework supports many other advanced querying patterns, including:
GroupBy
)Count()
, Sum()
, Average()
, Min()
, Max()
)Skip()
and Take()
)Exploring these query features will enable you to efficiently retrieve and manipulate data within your .NET applications using Entity Framework.