Procédure : explorer des relations à l'aide des propriétés de navigation (Entity Framework)
Cette rubrique montre comment explorer des relations au moyen de propriétés de navigation. Pour plus d'informations, voir Propriétés de navigation (EDM). L'exemple obtient toutes les commandes des contacts dont le nom est « Zhou ». La propriété de navigation Contact.SalesOrderHeader est utilisée pour obtenir la collection des objets SalesOrderHeader de chaque contact. Le même exemple est repris en utilisant chacune des technologies de requête Entity Framework suivantes :
LINQ to Entities
Entity SQL avec ObjectQuery<T>
Méthodes du Générateur de requêtes d'ObjectQuery<T>
L'exemple de cette rubrique est basé sur le modèle de vente Adventure Works. Pour exécuter le code de cet exemple, vous devez déjà avoir ajouté le modèle de vente AdventureWorks à votre projet et configuré ce dernier pour qu'il utilise Entity Framework. Pour ce faire, exécutez les procédures décrites dans Procédure : configurer manuellement un projet Entity Framework et Procédure : définir manuellement un modèle EDM (Entity Data Model) (Entity Framework). Vous pouvez également définir le modèle de vente AdventureWorks Sales Model à l'aide de l'Assistant EDM. Pour plus d'informations, voir Procédure : utiliser l'Assistant Entity Data Model (Entity Framework).
Exemple
Cet exemple utilise LINQ to Entities.
Using AWEntities As New AdventureWorksEntities
Dim contacts As ObjectQuery(Of Contact) = AWEntities.Contact
Dim ordersQuery = From contact In contacts _
Where contact.LastName = "Zhou" _
Select New With _
{.LastName = contact.LastName, _
.Orders = contact.SalesOrderHeader}
For Each order In ordersQuery
Console.WriteLine("Name: {0}", order.LastName)
For Each orderInfo In order.Orders
Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}", _
orderInfo.SalesOrderID, orderInfo.OrderDate, orderInfo.TotalDue)
Next
Console.WriteLine("")
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Contact> contacts = AWEntities.Contact;
var ordersQuery = from contact in contacts
where contact.LastName == "Zhou"
select new { LastName = contact.LastName, Orders = contact.SalesOrderHeader };
foreach (var order in ordersQuery)
{
Console.WriteLine("Name: {0}", order.LastName);
foreach (SalesOrderHeader orderInfo in order.Orders)
{
Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}",
orderInfo.SalesOrderID, orderInfo.OrderDate, orderInfo.TotalDue);
}
Console.WriteLine("");
}
}
Cet exemple utilise Entité SQL.
Using advWorksContext As New AdventureWorksEntities
Dim esqlQuery As String = "SELECT c.FirstName, c.SalesOrderHeader " & _
" FROM AdventureWorksEntities.Contact AS c where c.LastName = 'Zhou'"
Try
Dim objQuery As New ObjectQuery(Of DbDataRecord)(esqlQuery, advWorksContext)
For Each rec As DbDataRecord In objQuery
' Display contact's first name.
Console.WriteLine("First Name {0}: ", rec.Item(0))
Dim list As List(Of SalesOrderHeader) = DirectCast(rec.Item(1), List(Of SalesOrderHeader))
' Display SalesOrderHeader information
' associated with the contact.
For Each soh As SalesOrderHeader In list
Console.WriteLine(" Order ID: {0}, Order date: {1}, Total Due: {2}", _
soh.SalesOrderID, soh.OrderDate, soh.TotalDue)
Next
Next
Catch ex As EntityException
Console.WriteLine(ex.ToString())
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
string esqlQuery = @"SELECT c.FirstName, c.SalesOrderHeader
FROM AdventureWorksEntities.Contact AS c where c.LastName = 'Zhou'";
try
{
foreach (DbDataRecord rec in
new ObjectQuery<DbDataRecord>(esqlQuery, advWorksContext))
{
// Display contact's first name.
Console.WriteLine("First Name {0}: ", rec[0]);
List<SalesOrderHeader> list = rec[1] as List<SalesOrderHeader>;
// Display SalesOrderHeader information
// associated with the contact.
foreach (SalesOrderHeader soh in list)
{
Console.WriteLine(" Order ID: {0}, Order date: {1}, Total Due: {2}",
soh.SalesOrderID, soh.OrderDate, soh.TotalDue);
}
}
}
catch (EntityException ex)
{
Console.WriteLine(ex.ToString());
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
}
Cet exemple utilise la méthode du Générateur de requêtes.
Using advWorksContext As New AdventureWorksEntities()
Try
' Define a query that returns a nested
' DbDataRecord for the projection.
Dim query As ObjectQuery(Of DbDataRecord) = _
advWorksContext.Contact.Select("it.FirstName, " _
+ "it.LastName, it.SalesOrderHeader") _
.Where("it.LastName = 'Zhou'")
For Each rec As DbDataRecord In query.Execute(MergeOption.AppendOnly)
' Display contact's first name.
Console.WriteLine("First Name {0}: ", rec(0))
' Display SalesOrderHeader information
' associated with the contact.
For Each soh As SalesOrderHeader In CType(rec(2), List(Of SalesOrderHeader))
Console.WriteLine(" Order ID: {0}, " + _
"Order date: {1}, Total Due: {2}", _
soh.SalesOrderID, soh.OrderDate, soh.TotalDue)
Next
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
try
{
// Define a query that returns a nested
// DbDataRecord for the projection.
ObjectQuery<DbDataRecord> query =
advWorksContext.Contact.Select("it.FirstName, "
+ "it.LastName, it.SalesOrderHeader")
.Where("it.LastName = 'Zhou'");
foreach (DbDataRecord rec in
query.Execute(MergeOption.AppendOnly))
{
// Display contact's first name.
Console.WriteLine("First Name {0}: ", rec[0]);
List<SalesOrderHeader> list = rec[2]
as List<SalesOrderHeader>;
// Display SalesOrderHeader information
// associated with the contact.
foreach (SalesOrderHeader soh in list)
{
Console.WriteLine(" Order ID: {0}, " +
"Order date: {1}, Total Due: {2}",
soh.SalesOrderID, soh.OrderDate, soh.TotalDue);
}
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
Voir aussi
Tâches
Concepts
Relations du modèle Entity Data Model