Database Reference
In-Depth Information
A simpler version of the
Translate()
method does not require a
MergeOption
. This version materializes objects
that are disconnected from the object context. This is subtly different from objects that are not tracked in that the
objects are created completely outside of the object context. If you were to use this simpler
Translate()
to read the
jobs, you would not be able later to materialize new bids into the object context because Entity Framework would
not have any reference to the associated jobs. Those jobs are completely disconnected from the object context.
Additionally, you cannot change the properties of the instances and expect Entity Framework to be able to save those
changes.
We used
ToList()
to force the evaluation of each query. This is required because the
Translate()
method
returns an
ObjectResult<T>
. It does not actually cause the results to be read from the reader. We need to force the
results to be read from the reader before we can use
NextResult()
to advance to the next result set. In practice, you
would most likely construct your code to continue to loop through each result set with
NextResult()
that the stored
procedure might return.
Although we didn't run into it in this example, it is important to note that
Translate()
bypasses the mapping
layer of the model. If you try to map an inheritance hierarchy or use an entity that has complex type properties,
Translate()
will fail.
Translate()
requires that the
DbDataReader
have columns that match each property on
the entity. This matching is done using simple name matching. If a column name can't be matched to a property,
Translate()
will fail.
3-8. Comparing Against a List of Values
Problem
You want to query entities in which a specific property value matches a value contained in a given list.
Solution
Suppose that you have a model like the one shown in Figure
3-9
.
Figure 3-9.
A model for topics and their categories
You want to find all of the topics in a given list of categories. To do this using LINQ or Entity SQL, follow the
pattern in Listing 3-16.