Databases Reference
In-Depth Information
retrieves and manipulates a relatively small number of entities at a time. However, web
applications and applications working with large numbers of entities in batch mode can
increase performance by disabling the change tracking and identity management as
shown here:
context.Products.MergeOption = MergeOption.NoTracking;
var chaiQuery = from p in context.Products
where p.ProductName == “Chai”
select p;
The
MergeOption
property is defined in the
ObjectQuery<T>
base class and thus can be
specified for the Products
ObjectSet
. It affects all entities returned by the query, which in
this case includes only Product entities. The
NoTracking
value instructs the
ObjectContext
to bypass the identity and property value tracking for the query results, which can be
illustrated by running the following code:
Product chai1 = chaiQuery.First();
Product chai2 = chaiQuery.First();
if (!object.ReferenceEquals(chai1, chai2))
Console.WriteLine(“chai1 and chai2 are different objects”);
When running this code, notice that
chai1
and
chai2
, initialized by two different execu-
tions of the same query, are actually two different objects. Had you not modified the
MergeOption
for this query, the object context would have detected that the
Chai
entity
had already been retrieved and returned the same entity for both query executions.
The Entity Model consists of three different sections, each describing a separate aspect of
the model. This is best illustrated by opening a newly created EDMX file in a text editor.
When stripped of actual content, it looks similar to this:
<?xml version=”1.0” encoding=”utf-8”?>
<Edmx Version=”2.0” xmlns=”http://schemas.microsoft.com/ado/2008/10/edmx”>
<Runtime>
<StorageModels/>
<ConceptualModels/>
<Mappings/>
</Runtime>
</Edmx>