Database Reference
In-Depth Information
Because our function returns
IQueryable<VisitSummary>
, we need to implement the bootstrapping code. Also,
because we need to get access to the QueryProvider inside our ObjectContext, we need to implement the method in a
partial class of our EFRecipesEntities class, which is our ObjectContext.
You might be wondering when you would return a collection of complex types rather than a collection of
anonymous types from a function. If you used the function in a LINQ query, the bootstrapping method would need
to return
IQueryable<DbDataRecord>
for the anonymous type. However, although this collection could not be filtered
further, a collection of complex types could be further filtered.
11-7. Returning a Collection of Entity References from a
Model-Defined Function
Problem
You want to return a collection of entity references from a model-defined function.
Solution
Let's say that you have a model, such as the one shown in Figure
11-7
, for events and their sponsors. Sponsors provide
different levels of financial support for events. Platinum sponsors provide the highest level of financial support.
Figure 11-7.
A model for events and their sponsors
You want to create a model-defined function that returns a collection of all the sponsors who are at the Platinum
level. Because you need only the entity key information for the sponsor, the function needs to return only a collection
of references to the sponsors.
To create and use the model-defined function, do the following:
1.
Right-click the .edmx file in the Solution Explorer, and click Open With
➤
XML Editor. This
will open the .edmx file in the XML Editor.
2.
Insert the code in Listing 11-13 just below the
<Schema>
tag in the conceptual models
section of the .edmx file. This defines the function in the model.