Database Reference
In-Depth Information
How It Works
After inserting some sample data into our database, we explicitly disable the lazy loading feature of Entity Framework,
as we want to explicitly control the loading of related child entities. We can disable lazy loading in one of two ways:
Context.Configuration object to false . This
approach disables lazy loading for all entities assigned to the context.
Set the LazyLoadingEnabled property from the
Remove the virtual access modifier from each navigation property in each entity class. This
approach disables lazy loading per entity class, giving you explicit control of lazy loading.
The first bit of code retrieves an instance of the Doctor entity. If you are using the explicit loading approach, it
would be a good practice to use the IsLoaded property to check whether the entity or entity collection is already loaded.
In the code, we check whether the doctor's appointments are loaded. If not, we use the Load() method to load them.
In the foreach loop, we iterate through the appointments, checking if the associated doctor is loaded. Notice in
the output that one doctor was already loaded while the other one was not. This is because our first query retrieved
this doctor. During the retrieval process for the appointments, Entity Framework connected the loaded instance of the
doctor with her appointments. This process is informally referred to as relationship fixup . Relationship fixup will not
fix up all associations. In particular, it will not tie in entities across a many-to-many association.
In the last bit of code, we print the number of appointments we have for the doctor. Then we clear the collection
from the context using the Clear() method. The Clear() method removes the relationship between the Doctor
and appointments entity objects.. Interestingly, it does not remove the instances from memory;they are still in the
context—they are just no longer connected to this instance of the Doctor entity.
Somewhat surprisingly, after we call Load() to reload the appointments, we see from the output that no appointments
are in our collection! What happened? It turns out that the Load() method is overloaded to take a parameter that controls
how the loaded entities are merged into the context. The default behavior for the Load() method is MergeOption.
AppendOnly , which simply appends instances that are not already in the context. In our case, none of the appointments
was actually removed from the context. Our use of the Clear() method simply removed them from the entity collection,
not the context. When we called Load() with the default MergeOption.AppendOnly , no new instances were found,
so nothing was added to the entity collection. Other merge options include NoTracking , OverwriteChanges , and
PreserveChanges . When we use the OverwriteChanges option, the appointments appear in the Doctor's Appointments.
Note in our code how we drop down into the underlying ObjectContext object in order to gain access to
the MergeOption behaviors exposed by Entity Framework. The MergeOption type is not directly available in the
DbContext. You'll recall that when using Entity Framework, there are two context objects available for use. The
preferred context object for Entity Framework 6 is the DbContext object, which provides an intuitive and easy-to-use
facade around the legacy Object Context object. The older Object Context object is still available through an explicit
cast against the DbContext object, as demonstrated in our recipe.
Along with AppendOnly , the MergeOption type exposes three other options:
The
NoTracking option turns off object state tracking for the loaded instances. With
NoTracking , Entity Framework will not track changes to the object and will not be aware that
the object is loaded into the context. The NoTracking option can be used on a navigation
property of an object only if the object was loaded with the NoTracking option. NoTracking
has one additional side effect. If we had loaded an instance of the Doctor entity with
NoTracking , loading the appointments with the Load() method would also occur with
NoTracking , regardless of the default AppendOnly option.
OverwriteChanges option will update the values in the current instance with that from
the database. Entity Framework will continue to use the same instance of the entity object.
This option is particularly useful if you need to discard changes made in the context and
refresh them from the database. This would be helpful, for example, in implementing an undo
operation in an application.
The
 
Search WWH ::




Custom Search