EntityReference<TEntity>.Load(MergeOption) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Loads the related object for this EntityReference<TEntity> with the specified merge option.
public:
override void Load(System::Data::Objects::MergeOption mergeOption);
public override void Load (System.Data.Objects.MergeOption mergeOption);
override this.Load : System.Data.Objects.MergeOption -> unit
Public Overrides Sub Load (mergeOption As MergeOption)
Parameters
- mergeOption
- MergeOption
Specifies how the object should be returned if it already exists in the ObjectContext.
Exceptions
The source of the EntityReference<TEntity> is null
.
-or-
A query returned more than one related end.
-or-
A query returned zero related ends, and one related end was expected.
Remarks
This method is used to load the related object.
When loaded, the related object is accessed from the Value property.
To explicitly load related objects, you must call the Load
method on the related end returned by the navigation property. For a one-to-many relationship, call the Load method on EntityCollection<TEntity>, and for a one-to-one relationship, call the Load on EntityReference<TEntity>. This loads the related object data into the object context. When a query returns results, you can enumerate through the collection of objects using a foreach
loop (For Each...Next
in Visual Basic) and conditionally call the Load
method on EntityReference<TEntity> and EntityCollection<TEntity> properties for each entity in the results.
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<T> collection. This closes the data reader and enables you to enumerate over the collection to load referenced objects.
This method calls the internal RelatedEnd.ValidateLoad
method before loading the related object, which validates that a call to Load has the correct conditions. The RelatedEnd.ValidateLoad
method checks that:
- A valid ObjectContext exists.
- The entity isn't in a Deleted state.
- MergeOption for Load must be NoTracking if and only if the source entity was NoTracking. If the source entity was retrieved with any other MergeOption, the Load MergeOption can be anything but NoTracking (for example, the entity could have been loaded with OverwriteChanges and the Load option can be AppendOnly).
- If
mergeOption
is NoTracking, Load isn't called on an already loaded entity and Load isn't called on a non-empty, not-tracked RelatedEnd.
When the related object is already loaded in the ObjectContext, the Load method enforces the MergeOption specified by the mergeOption
parameter. For more information, see Identity Resolution, State Management, and Change Tracking.