Database Reference
In-Depth Information
To create and use these functions, 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-7 just below the
<Schema>
tag in the conceptual models
section of the .edmx file. This defines the functions in the model.
Listing 11-7.
Model-Defined Functions for Navigating the Associate Hierarchy
<Function Name="GetProjectManager" ReturnType="EFRecipesModel.ProjectManager">
<Parameter Name="teammember" Type="EFRecipesModel.TeamMember" />
<DefiningExpression>
treat(teammember.Manager.Manager as EFRecipesModel.ProjectManager)
</DefiningExpression>
</Function>
<Function Name="GetSupervisor" ReturnType="EFRecipesModel.Supervisor">
<Parameter Name="teammember" Type="EFRecipesModel.TeamMember" />
<DefiningExpression>
treat(EFRecipesModel.GetProjectManager(teammember).Manager as
EFRecipesModel.Supervisor)
</DefiningExpression>
</Function>
3.
Insert into and query the model using code similar to the pattern shown in Listing 11-8.
Listing 11-8.
Using Both eSQL and LINQ to Query the Model
class Program
{
static void Main(string[] args)
{
RunExample();
}
static void RunExample()
{
using (var context = new EFRecipesEntities())
{
var john = new Supervisor { Name = "John Smith" };
var steve = new Supervisor {Name = "Steve Johnson"};
var jill = new ProjectManager { Name = "Jill Masterson",
Manager = john };
var karen = new ProjectManager { Name = "Karen Carns",
Manager = steve };
var bob = new TeamLead { Name = "Bob Richardson", Manager = karen };
var tom = new TeamLead { Name = "Tom Landers", Manager = jill };
var nancy = new TeamMember { Name = "Nancy Jones", Manager = tom };
var stacy = new TeamMember { Name = "Stacy Rutgers",
Manager = bob };