Loads related objects into the collection, using the specified merge option.
Namespace:
System.Data.Objects.DataClasses
Assembly:
System.Data.Entity (in System.Data.Entity.dll)
Visual Basic (Declaration)
Public Overrides Sub Load ( _
mergeOption As MergeOption _
)
Dim instance As EntityCollection
Dim mergeOption As MergeOption
instance.Load(mergeOption)
public override void Load(
MergeOption mergeOption
)
public:
virtual void Load(
MergeOption mergeOption
) override
public override function Load(
mergeOption : MergeOption
)
Implements
IRelatedEnd..::.Load(MergeOption)
This method calls the ValidateLoad<(Of <(TEntity>)>) method before loading the collection. See this method for a list of possible exceptions.
When objects in the collection are already loaded into the ObjectContext, the Load method enforces the MergeOption specified by the mergeOption parameter. For more information, see Managing the Object Context (Entity Framework).
To explicitly load the related objects, you must call the Load method on the entity reference. This loads the related objects into the object context. When a query returns a collection of objects, you can enumerate through the collection and call the Load method to load the related objects for each object in the collection. The Load method loads related objects from the data source whether or not IsLoaded is true.
Note: |
|---|
When you call the Load method during a foreach (C#) or For Each (Visual Basic) enumeration, Object Services tries to open a new data reader. This operation will fail unless you have enabled multiple active results sets by specifying multipleactiveresultsets=true in the connection string. You can also load the result of the query into a List<(Of <(T>)>) collection. This closes the data reader and enables you to enumerate over the collection to load referenced objects. |
The EntityCollection<(Of <(TEntity>)>)..::.Load method is synchronized with the EntityReference<(Of <(TEntity>)>)..::.Load method.
This example is based on the Adventure Works Sales Model. To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).
This example loads the related SalesOrderHeader objects for the Contact entity.
' Specify the customer ID.
Dim customerId As Integer = 4332
Using context As New AdventureWorksEntities
Try
' Get a specified customer by contact ID.
Dim customer As Contact = context.Contact _
.Where("it.ContactID = @customerId", _
New ObjectParameter("customerId", customerId)).First()
' Load the orders for the customer
If (Not customer.SalesOrderHeader.IsLoaded) Then
customer.SalesOrderHeader.Load()
End If
For Each order As SalesOrderHeader In customer.SalesOrderHeader
' Load the items for the order if not already loaded.
If Not order.SalesOrderDetail.IsLoaded Then
order.SalesOrderDetail.Load()
End If
Console.WriteLine(String.Format("PO Number: {0}", _
order.PurchaseOrderNumber))
Console.WriteLine(String.Format("Order Date: {0}", _
order.OrderDate.ToString()))
Console.WriteLine("Order items:")
Dim item As SalesOrderDetail
For Each item In order.SalesOrderDetail
Console.WriteLine(String.Format("Product:{0}" _
+ "Quantity: {1}", item.ProductID.ToString(), _
item.OrderQty.ToString()))
Next
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
Catch ex As EntityCommandExecutionException
Console.WriteLine(ex.ToString())
End Try
End Using
// Specify the customer ID.
int customerId = 4332;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Get a specified customer by contact ID.
Contact customer = context.Contact
.Where("it.ContactID = @customerId",
new ObjectParameter("customerId", customerId)).First();
// Load the orders for the customer
if (!customer.SalesOrderHeader.IsLoaded)
{
customer.SalesOrderHeader.Load();
}
foreach (SalesOrderHeader order in customer.SalesOrderHeader)
{
// Load the items for the order if not already loaded.
if (!order.SalesOrderDetail.IsLoaded)
{
order.SalesOrderDetail.Load();
}
Console.WriteLine(String.Format("PO Number: {0}",
order.PurchaseOrderNumber));
Console.WriteLine(String.Format("Order Date: {0}",
order.OrderDate.ToString()));
Console.WriteLine("Order items:");
foreach (SalesOrderDetail item in order.SalesOrderDetail)
{
Console.WriteLine(String.Format("Product: {0} "
+ "Quantity: {1}", item.ProductID.ToString(),
item.OrderQty.ToString()));
}
}
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
catch (EntityCommandExecutionException ex)
{
Console.WriteLine(ex.ToString());
}
}
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5 SP1
Reference
Other Resources