EF Tutorials Gettingstarted
EF Tutorials Gettingstarted
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
Contents
Part 1: Overview .................................................................................................................................. 4
Creating the Web Application ...................................................................................................................................5 Creating the Database ...............................................................................................................................................7 Creating the Entity Framework Data Model ...........................................................................................................10 Exploring the Entity Framework Data Model ..........................................................................................................13
Adding the Stored Procedures to the Data Model ..................................................................................................91 Mapping the Stored Procedures .............................................................................................................................92 Using Insert, Update, and Delete Stored Procedures .............................................................................................96 Using Select Stored Procedures ..............................................................................................................................97
Part 8: Using Dynamic Data Functionality to Format and Validate Data .............................................. 99
Using DynamicField and DynamicControl Controls .................................................................................................99 Adding Metadata to the Data Model ....................................................................................................................103
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
Part 1: Overview
The application you'll be building in these tutorials is a simple university website.
Users can view and update student, course, and instructor information. A few of the screens you'll create are shown below.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
This template creates a web application project that already includes a style sheet and master pages:
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
Open the Site.Master file and change "My ASP.NET Application" to "Contoso University".
<h1> Contoso University </h1>
Find the Menu control named NavigationMenu and replace it with the following markup, which adds menu items for the pages you'll be creating.
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal"> <Items> <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home" /> <asp:MenuItem NavigateUrl="~/About.aspx" Text="About" /> <asp:MenuItem NavigateUrl="~/Students.aspx" Text="Students"> <asp:MenuItem NavigateUrl="~/StudentsAdd.aspx" Text="Add Students" /> </asp:MenuItem> <asp:MenuItem NavigateUrl="~/Courses.aspx" Text="Courses"> <asp:MenuItem NavigateUrl="~/CoursesAdd.aspx" Text="Add Courses" /> </asp:MenuItem> <asp:MenuItem NavigateUrl="~/Instructors.aspx" Text="Instructors"> <asp:MenuItem NavigateUrl="~/InstructorsCourses.aspx" Text="Course Assignments" /> The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
<asp:MenuItem NavigateUrl="~/OfficeAssignments.aspx" Text="Office Assignments" /> </asp:MenuItem> <asp:MenuItem NavigateUrl="~/Departments.aspx" Text="Departments"> <asp:MenuItem NavigateUrl="~/DepartmentsAdd.aspx" Text="Add Departments" /> </asp:MenuItem> </Items> </asp:Menu>
Open the Default.aspx page and change the Content control named BodyContent to this:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> <h2> Welcome to Contoso University! </h2> </asp:Content>
You now have a simple home page with links to the various pages that you'll be creating:
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
(This location of the .mdf file assumes you're using SQL Server 2008 Express.) If you create the database from a script, perform the following steps to create a database diagram: 1. In Server Explorer, expand Data Connections, expand School.mdf, right-click Database Diagrams, and select Add New Diagram.
SQL Server creates a database diagram that shows tables, columns in the tables, and relationships between the tables. You can move the tables around to organize them however you like. 3. Save the diagram as "SchoolDiagram" and close it.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
If you download the School.mdf file that goes with this tutorial, you can view the database diagram by double-clicking SchoolDiagram under Database Diagrams in Server Explorer.
The diagram looks something like this (the tables might be in different locations from what's shown here):
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
This starts the Entity Data Model Wizard. In the first wizard step, the Generate from database option is selected by default. Click Next.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
10
In the Choose Your Data Connection step, leave the default values and click Next. The School database is selected by default and the connection setting is saved in the Web.config file as SchoolEntities.
In the Choose Your Database Objects wizard step, select all of the tables except sysdiagrams (which was created for the diagram you generated earlier) and then click Finish.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
11
After it's finished creating the model, Visual Studio shows you a graphical representation of the Entity Framework objects (entities) that correspond to your database tables. (As with the database diagram, the location of individual elements might be different from what you see in this illustration. You can drag the elements around to match the illustration if you want.)
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
12
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
13
In this case, a Person entity may or may not be associated with an OfficeAssignment entity. An OfficeAssignment entity must be associated with a Person entity. In other words, an instructor may or may not be assigned to an office, and any office can be assigned to only one instructor.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
14
In this case, a Person entity may or may not have associated StudentGrade entities. A StudentGrade entity must be associated with one Person entity. StudentGrade entities actually represent enrolled courses in this database; if a student is enrolled in a course and there's no grade yet, the Grade property is null. In other words, a student may not be enrolled in any courses yet, may be enrolled in one course, or may be enrolled in multiple courses. Each grade in an enrolled course applies to only one student.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
15
In this case, a Person entity may or may not have associated Course entities, and the reverse is also true: a Course entity may or may not have associated Person entities. In other words, an instructor may teach multiple courses, and a course may be taught by multiple instructors. (In this database, this relationship applies only to instructors; it does not link students to courses. Students are linked to courses by the StudentGrades table.) Another difference between the database diagram and the data model is the additional Navigation Properties section for each entity. A navigation property of an entity references related entities. For example, the Courses property in a Person entity contains a collection of all the Course entities that are related to that Person entity.
Yet another difference between the database and data model is the absence of the CourseInstructor association table that's used in the database to link the Person and Course tables in a many-to-many relationship. The navigation properties enable you to get related Course entities from the Person entity and related Person entities from the Course entity, so there's no need to represent the association table in the data model.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
16
For purposes of this tutorial, suppose the FirstName column of the Person table actually contains both a person's first name and middle name. You want to change the name of the field to reflect this, but the database administrator (DBA) might not want to change the database. You can change the name of the FirstName property in the data model, while leaving its database equivalent unchanged. In the designer, right-click FirstName in the Person entity, and then select Rename.
Type in the new name "FirstMidName". This changes the way you refer to the column in code without changing the database.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
17
The model browser provides another way to view the database structure, the data model structure, and the mapping between them. To see it, right-click a blank area in the entity designer and then click Model Browser.
The Model Browser pane displays a tree view. (The Model Browser pane might be docked with the Solution Explorer pane.) The SchoolModel node represents the data model structure, and the SchoolModel.Store node represents the database structure.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
18
Expand SchoolModel.Store to see the tables, expand Tables / Views to see tables, and then expand Course to see the columns within a table.
Expand SchoolModel, expand Entity Types, and then expand the Course node to see the entities and the properties within the entities.
In either the designer or the Model Browser pane you can see how the Entity Framework relates the objects of the two models. Right-click the Person entity and select Table Mapping.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
19
This opens the Mapping Details window. Notice that this window lets you see that the database column FirstName is mapped to FirstMidName, which is what you renamed it to in the data model.
The Entity Framework uses XML to store information about the database, the data model, and the mappings between them. The SchoolModel.edmx file is actually an XML file that contains this information. The designer renders the information in a graphical format, but you can also view the file as XML by right-clicking the .edmx file in Solution Explorer, clicking Open With, and selecting XML (Text) Editor. (The data model designer and an XML editor are just two different ways of opening and working with the same file, so you cannot have the designer open and open the file in an XML editor at the same time.) You've now created a website, a database, and a data model. In the next walkthrough you'll begin working with data using the data model and the ASP.NET EntityDataSource control.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 1: Overview
20
Note that in this application you won't be adding input validation to pages that update the database, and some of the error handling will not be as robust as would be required in a production application. That keeps the tutorial focused on the Entity Framework and keeps it from getting too long. For details about how to add these features to your application, see Validating User Input in ASP.NET Web Pages and Error Handling in ASP.NET Pages and Applications.
21
build the project now. Changes to the data model are not made available to the designer until the project is built. Create a new web page using the Web Form using Master Page template, and name it Students.aspx.
Specify Site.Master as the master page. All of the pages you create for these tutorials will use this master page.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
22
In Source view, add an h2 heading to the Content control named Content2, as shown in the following example:
From the Data tab of the Toolbox, drag an EntityDataSource control to the page, drop it below the heading, and change the ID to StudentsEntityDataSource:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Student List</h2> <asp:EntityDataSource ID="StudentsEntityDataSource" runat="server"> </asp:EntityDataSource> </asp:Content>
Switch to Design view, click the data source control's smart tag, and then click Configure Data Source to launch the Configure Data Source wizard.
In the Configure ObjectContext wizard step, select SchoolEntities as the value for Named Connection, and select SchoolEntities as the DefaultContainerName value. Then click Next.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
23
Note: If you get the following dialog box at this point, you have to build the project before proceeding.)
In the Configure Data Selection step, select People as the value for EntitySetName. Under Select, make sure the Select All check box is selected. Then select the options to enable update and delete. When you're done, click Finish.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
24
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
25
In the Properties window, expand INSERT and UPDATE Specification and set the DeleteRule property to Cascade.
Save and close the diagram. If you're asked whether you want to update the database, click Yes.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
26
To make sure that the model keeps entities that are in memory in sync with what the database is doing, you must set corresponding rules in the data model. Open SchoolModel.edmx, right-click the association line between Person and StudentGrade, and then select Properties.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
27
Save and close the SchoolModel.edmx file, and then rebuild the project. In general, when the database changes, you have several choices for how to sync up the model:
For certain kinds of changes (such as adding or refreshing tables, views, or stored procedures), right-click in the designer and select Update Model from Database to have the designer make the changes automatically. Regenerate the data model. Make manual updates like this one.
In this case, you could have regenerated the model or refreshed the tables affected by the relationship change, but then you'd have to make the field-name change again (from FirstName to FirstMidName).
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
28
Click Refresh Schema (click Yes if you're prompted to confirm), then click Enable Paging, Enable Sorting, Enable Editing, and Enable Deleting. Click Edit Columns.
In the Selected fields box, delete PersonID, LastName, and HireDate. You typically don't display a record key to users, hire date is not relevant to students, and you'll put both parts of the name in one field, so you only need one of the name fields.)
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
29
Select the FirstMidName field and then click Convert this field into a TemplateField. Do the same for EnrollmentDate.
Click OK and then switch to Source view. The remaining changes will be easier to do directly in markup. The GridView control markup now looks like the following example.
<asp:GridView ID="StudentsGridView" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PersonID" DataSourceID="StudentsEntityDataSource"> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> <asp:TemplateField HeaderText="FirstMidName" SortExpression="FirstMidName"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstMidName") %>'></asp:TextBox> </EditItemTemplate>
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
30
<ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstMidName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="EnrollmentDate" SortExpression="EnrollmentDate"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("EnrollmentDate") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("EnrollmentDate") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
The first column after the command field is a template field that currently displays the first name. Change the markup for this template field to look like the following example:
<asp:TemplateField HeaderText="Name" SortExpression="LastName"> <edititemtemplate> <asp:TextBox ID="LastNameTextBox" runat="server" Text='<%# Bind("LastName") %>'></asp:TextBox> <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Bind("FirstMidName") %>'></asp:TextBox> </edititemtemplate> <itemtemplate> <asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>, <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstMidName") %>'></asp:Label> </itemtemplate> </asp:TemplateField>
In display mode, two Label controls display the first and last name. In edit mode, two text boxes are provided so you can change the first and last name. As with the Label controls in display mode, you use Bind and Eval expressions exactly as you would with ASP.NET data source controls that connect directly to databases. The only difference is that you're specifying entity properties instead of database columns. The last column is a template field that displays the enrollment date. Change the markup for this field to look like the following example:
<asp:TemplateField HeaderText="Enrollment Date" SortExpression="EnrollmentDate"> <EditItemTemplate> The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
31
<asp:TextBox ID="EnrollmentDateTextBox" runat="server" Text='<%# Bind("EnrollmentDate", "{0:d}") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="EnrollmentDateLabel" runat="server" Text='<%# Eval("EnrollmentDate", "{0:d}") %>'></asp:Label> </ItemTemplate> </asp:TemplateField>
In both display and edit mode, the format string "{0,d}" causes the date to be displayed in the "short date" format. (Your computer might be configured to display this format differently from the screen images shown in this tutorial.) Notice that in each of these template fields, the designer used a Bind expression by default, but you've changed that to an Eval expression in the ItemTemplate elements. The Bind expression makes the data available in GridView control properties in case you need to access the data in code. In this page you don't need to access this data in code, so you can use Eval, which is more efficient. For more information, see Getting your data out of the data controls.
Better performance. When the EntityDataSource control initializes the data model using the ConnectionString and DefaultContainerName attributes, it performs additional work to load metadata on every request. This isn't necessary if you specify the ContextTypeName attribute. Lazy loading is turned on by default in generated object context classes (such as SchoolEntities in this tutorial) in Entity Framework 4.0. This means that navigation properties are loaded with related data automatically right when you need it. Lazy loading is explained in more detail later in this tutorial. Any customizations that you've applied to the object context class (in this case, the SchoolEntities class) will be available to controls that use the EntityDataSource control. Customizing the object context class is an advanced topic that is not covered in this tutorial series. For more information, see Extending Entity Framework Generated Types.
The markup will now resemble the following example (the order of the properties might be different):
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
32
The EnableFlattening attribute refers to a feature that was needed in earlier versions of the Entity Framework because foreign key columns were not exposed as entity properties. The current version makes it possible to use foreign key associations, which means foreign key properties are exposed for all but many-to-many associations. If your entities have foreign key properties and no complex types, you can leave this attribute set to False. Don't remove the attribute from the markup, because the default value is True. For more information, see Flattening Objects (EntityDataSource). Run the page and you see a list of students and employees (you'll filter for just students in the next tutorial). The first name and last name are displayed together.
To sort the display, click a column name. Click Edit in any row. Text boxes are displayed where you can change the first and last name.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
33
The Delete button also works. Click delete for a row that has an enrollment date and the row disappears. (Rows without an enrollment date represent instructors and you may get a referential integrity error. In the next tutorial you'll filter this list to include just students.)
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
34
properties, you could specify their names separated by commas for example, StudentGrades, Courses.)
Switch to Source view. In the StudentsGridView control, after the last asp:TemplateField element, add the following new template field:
<asp:TemplateField HeaderText="Number of Courses"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("StudentGrades.Count") %>'></asp:Label> </ItemTemplate> </asp:TemplateField>
In the Eval expression, you can reference the navigation property StudentGrades. Because this property contains a collection, it has a Count property that you can use to display the number of courses in which the student is enrolled. In a later tutorial you'll see how to display data from navigation properties that contain single entities instead of collections. (Note that you cannot use BoundField elements to display data from navigation properties.) Run the page and you now see how many courses each student is enrolled in.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
35
This markup creates an EntityDataSource control that is similar to the one you created in Students.aspx, except it enables insertion. As with the GridView control, the bound fields of the DetailsView control are coded exactly as they would be for a data control that connects directly to a database, except that they reference entity properties. In this case, the DetailsView control is used only for inserting rows, so you have set the default mode to Insert. Run the page and add a new student.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
36
Nothing will happen after you insert a new student, but if you now run Students.aspx, you'll see the new student information.
In Design view, add an EntityDataSource control to the page as you did before, except this time name it DepartmentsEntityDataSource. Select Departments as the EntitySetName value, and select only the DepartmentID and Name properties.
From the Standard tab of the Toolbox, drag a DropDownList control to the page, name it DepartmentsDropDownList, click the smart tag, and select Choose Data Source to start the DataSource Configuration Wizard.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
37
In the Choose a Data Source step, select DepartmentsEntityDataSource as the data source, click Refresh Schema, and then select Name as the data field to display and DepartmentID as the value data field. Click OK.
The method you use to databind the control using the Entity Framework is the same as with other ASP.NET data source controls except you're specifying entities and entity properties. Switch to Source view and add "Select a department:" immediately before the DropDownList control.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
38
As a reminder, change the markup for the EntityDataSource control at this point by replacing the ConnectionString and DefaultContainerName attributes with a ContextTypeName="ContosoUniversity.DAL.SchoolEntities" attribute. It's often best to wait until after you've created the data-bound control that is linked to the data source control before you change the EntityDataSource control markup, because after you make the change, the designer will not provide you with a Refresh Schema option in the data-bound control. Run the page and you can select a department from the drop-down list.
This completes the introduction to using the EntityDataSource control. Working with this control is generally no different from working with other ASP.NET data source controls, except that you reference entities and properties instead of tables and columns. The only exception is when you want to access navigation properties. In the next tutorial you'll see that the syntax you use with EntityDataSource control might also differ from other data source controls when you filter, group, and order data.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 2: The EntityDataSource Control
39
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
40
The syntax you use in the Where property of the EntityDataSource control is Entity SQL. Entity SQL is similar to Transact-SQL, but it's customized for use with entities rather than database objects. In the expression it.EnrollmentDate is not null, the word it represents a reference to the entity
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
41
returned by the query. Therefore, it.EnrollmentDate refers to the EnrollmentDate property of the Person entity that the EntityDataSource control returns. Run the page. The students list now contains only students. (There are no rows displayed where there's no enrollment date.)
Run the page. The students list is now in order by last name.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
42
The Expression Editor dialog box is displayed. In this dialog box, select Automatically generate the Where expression based on the provided parameters, and then click Add Parameter. Name the parameter DepartmentID, select Control as the Parameter source value, and select DepartmentsDropDownList as the ControlID value.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
43
Click Show advanced properties, and in the Properties window of the Expression Editor dialog box, change the Type property to Int32.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
44
Below the drop-down list, add a GridView control to the page and name it CoursesGridView. Connect it to the CoursesEntityDataSource data source control, click Refresh Schema, click Edit Columns, and remove the DepartmentID column. The GridView control markup resembles the following example.
<asp:GridView ID="CoursesGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="CourseID" DataSourceID="CoursesEntityDataSource"> <Columns> <asp:BoundField DataField="CourseID" HeaderText="ID" ReadOnly="True" SortExpression="CourseID" /> <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> <asp:BoundField DataField="Credits" HeaderText="Credits" SortExpression="Credits" /> </Columns> </asp:GridView>
When the user changes the selected department in the drop-down list, you want the list of associated courses to change automatically. To make this happen, select the drop-down list, and in the Properties window set the AutoPostBack property to True.
Now that you're finished using the designer, switch to Source view and replace the ConnectionString and DefaultContainer name properties of the CoursesEntityDataSource control with the ContextTypeName="ContosoUniversity.DAL.SchoolEntities" attribute. When you're done, the markup for the control will look like the following example.
<asp:EntityDataSource ID="CoursesEntityDataSource" runat="server" ContextTypeName="ContosoUniversity.DAL.SchoolEntities" EnableFlattening="false" EntitySetName="Courses" AutoGenerateWhereClause="true" Where=""> <WhereParameters> <asp:ControlParameter ControlID="DepartmentsDropDownList" Type="Int32" Name="DepartmentID" PropertyName="SelectedValue" /> </WhereParameters> </asp:EntityDataSource> The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
45
Run the page and use the drop-down list to select different departments. Only courses that are offered by the selected department are displayed in the GridView control.
After the heading, add an EntityDataSource control and name it StudentStatisticsEntityDataSource. Connect it to SchoolEntities, select the People entity set, and leave the Select box in the wizard unchanged. Set the following properties in the Properties window:
To filter for students only, set the Where property to it.EnrollmentDate is not null. To group the results by the enrollment date, set the GroupBy property to it.EnrollmentDate. To select the enrollment date and the number of students, set the Select property to it.EnrollmentDate, Count(it.EnrollmentDate) AS NumberOfStudents. To order the results by the enrollment date, set the OrderBy property to it.EnrollmentDate.
In Source view, replace the ConnectionString and DefaultContainer name properties with a ContextTypeName property. The EntityDataSource control markup now resembles the following example.
<asp:EntityDataSource ID="StudentStatisticsEntityDataSource" runat="server" ContextTypeName="ContosoUniversity.DAL.SchoolEntities" EnableFlattening="False" EntitySetName="People" Select="it.EnrollmentDate, Count(it.EnrollmentDate) AS NumberOfStudents" The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
46
The syntax of the Select, GroupBy, and Where properties resembles Transact-SQL except for the it keyword that specifies the current entity. Add the following markup to create a GridView control to display the data.
<asp:GridView ID="StudentStatisticsGridView" runat="server" AutoGenerateColumns="False" DataSourceID="StudentStatisticsEntityDataSource"> <Columns> <asp:BoundField DataField="EnrollmentDate" DataFormatString="{0:d}" HeaderText="Date of Enrollment" ReadOnly="True" SortExpression="EnrollmentDate" /> <asp:BoundField DataField="NumberOfStudents" HeaderText="Students" ReadOnly="True" SortExpression="NumberOfStudents" /> </Columns> </asp:GridView>
Run the page to see a list showing the number of students by enrollment date.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
47
In this part of the tutorial you'll use a QueryExtender control to filter and order data, and one of the order-by fields will be a navigation property. (If you prefer to use code instead of markup to extend the queries that are automatically generated by the EntityDataSource control, you can do that by handling the QueryCreated event. This is how the QueryExtender control extends EntityDataSource control queries also.) Open the Courses.aspx page, and below the markup you added previously, insert the following markup to create a heading, a text box for entering search strings, a search button, and an EntityDataSource control that's bound to the Courses entity set.
<h2>Courses by Name</h2> Enter a course name <asp:TextBox ID="SearchTextBox" runat="server" AutoPostBack="true"></asp:TextBox> <asp:Button ID="SearchButton" runat="server" Text="Search" /> <br /><br /> <asp:EntityDataSource ID="SearchEntityDataSource" runat="server" ContextTypeName="ContosoUniversity.DAL.SchoolEntities" EnableFlattening="False" EntitySetName="Courses" Include="Department" > </asp:EntityDataSource>
Notice that the EntityDataSource control's Include property is set to Department. In the database, the Course table does not contain the department name; it contains a DepartmentID foreign key column. If you were querying the database directly, to get the department name along with course data, you would have to join the Course and Department tables. By setting the Include property to Department, you specify that the Entity Framework should do the work of getting the related Department entity when it gets a Course entity. The Department entity is then stored in the Department navigation property of the Course entity. (By default, the SchoolEntities class that was generated by the data model designer retrieves related data when it's needed, and you've bound the data source control to that class, so setting the Include property is not necessary. However, setting it improves performance of the page, because otherwise the Entity Framework would make separate calls to the database to retrieve data for the Course entities and for the related Department entities.) After the EntityDataSource control you just created, insert the following markup to create a QueryExtender control that's bound to that EntityDataSource control.
<asp:QueryExtender ID="SearchQueryExtender" runat="server" TargetControlID="SearchEntityDataSource" > <asp:SearchExpression SearchType="StartsWith" DataFields="Title"> <asp:ControlParameter ControlID="SearchTextBox" /> </asp:SearchExpression> <asp:OrderByExpression DataField="Department.Name" Direction="Ascending"> <asp:ThenBy DataField="Title" Direction="Ascending" /> The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
48
</asp:OrderByExpression> </asp:QueryExtender>
The SearchExpression element specifies that you want to select courses whose titles match the value entered in the text box. Only as many characters as are entered in the text box will be compared, because the SearchType property specifies StartsWith. The OrderByExpression element specifies that the result set will be ordered by course title within department name. Notice how department name is specified: Department.Name. Because the association between the Course entity and the Department entity is one-to-one, the Department navigation property contains a Department entity. (If this were a one-to-many relationship, the property would contain a collection.) To get the department name, you must specify the Name property of the Department entity. Finally, add a GridView control to display the list of courses:
<asp:GridView ID="SearchGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="CourseID" DataSourceID="SearchEntityDataSource" AllowPaging="true"> <Columns> <asp:TemplateField HeaderText="Department"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Eval("Department.Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="CourseID" HeaderText="ID"/> <asp:BoundField DataField="Title" HeaderText="Title" /> <asp:BoundField DataField="Credits" HeaderText="Credits" /> </Columns> </asp:GridView>
The first column is a template field that displays the department name. The databinding expression specifies Department.Name, just as you saw in the QueryExtender control. Run the page. The initial display shows a list of all courses in order by department and then by course title.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
49
Enter an "m" and click Search to see all courses whose titles begin with "m" (the search is not case sensitive).
<h2>Find Students by Name</h2> Enter any part of the name <asp:TextBox ID="SearchTextBox" runat="server" AutoPostBack="true"></asp:TextBox> The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
50
<asp:Button ID="SearchButton" runat="server" Text="Search" /> <br /> <br /> <asp:EntityDataSource ID="SearchEntityDataSource" runat="server" ContextTypeName="ContosoUniversity.DAL.SchoolEntities" EnableFlattening="False" EntitySetName="People" Where="it.EnrollmentDate is not null and (it.FirstMidName Like '%' + @StudentName + '%' or it.LastName Like '%' + @StudentName + '%')" > <WhereParameters> <asp:ControlParameter ControlID="SearchTextBox" Name="StudentName" PropertyName="Text" Type="String" DefaultValue="%"/> </WhereParameters> </asp:EntityDataSource> <asp:GridView ID="SearchGridView" runat="server" AutoGenerateColumns="False" DataKeyNames="PersonID" DataSourceID="SearchEntityDataSource" AllowPaging="true"> <Columns> <asp:TemplateField HeaderText="Name" SortExpression="LastName, FirstMidName"> <ItemTemplate> <asp:Label ID="LastNameFoundLabel" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>, <asp:Label ID="FirstNameFoundLabel" runat="server" Text='<%# Eval("FirstMidName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Enrollment Date" SortExpression="EnrollmentDate"> <ItemTemplate> <asp:Label ID="EnrollmentDateFoundLabel" runat="server" Text='<%# Eval("EnrollmentDate", "{0:d}") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
This markup is similar to what you've seen earlier except for the Where property value. The second part of the Where expression defines a substring search (LIKE %FirstMidName% or LIKE %LastName%) that searches both the first and last names for whatever is entered in the text box. Run the page. Initially you see all of the students because the default value for the StudentName parameter is "%".
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
51
Enter the letter "g" in the text box and click Search. You see a list of students that have a "g" in either the first or last name.
You've now displayed, updated, filtered, ordered, and grouped data from individual tables. In the next tutorial you'll begin to work with related data (master-detail scenarios).
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 3: Filtering, Ordering, and Grouping Data
52
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
53
This markup creates an EntityDataSource control that selects instructors and enables updates. The div element configures markup to render on the left so that you can add a column on the right later. Between the EntityDataSource markup and the closing </div> tag, add the following markup that creates a GridView control and a Label control that you'll use for error messages:
<asp:gridview id="InstructorsGridView" runat="server" allowpaging="True" allowsorting="True" autogeneratecolumns="False" datakeynames="PersonID" datasourceid="InstructorsEntityDataSource" onselectedindexchanged="InstructorsGridView_SelectedIndexChanged" selectedrowstyle-backcolor="LightGray" onrowupdating="InstructorsGridView_RowUpdating"> <Columns> <asp:CommandField ShowSelectButton="True" ShowEditButton="True" /> <asp:TemplateField HeaderText="Name" SortExpression="LastName"> <ItemTemplate> <asp:Label ID="InstructorLastNameLabel" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>, <asp:Label ID="InstructorFirstNameLabel" runat="server" Text='<%# Eval("FirstMidName") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="InstructorLastNameTextBox" runat="server" Text='<%# Bind("FirstMidName") %>' Width="7em"></asp:TextBox> <asp:TextBox ID="InstructorFirstNameTextBox" runat="server" Text='<%# Bind("LastName") %>' Width="7em"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Hire Date" SortExpression="HireDate"> <ItemTemplate> <asp:Label ID="InstructorHireDateLabel" runat="server" Text='<%# Eval("HireDate", "{0:d}") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="InstructorHireDateTextBox" runat="server" Text='<%# Bind("HireDate", "{0:d}") %>' Width="7em"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Office Assignment" SortExpression="OfficeAssignment.Location"> <ItemTemplate> <asp:Label ID="InstructorOfficeLabel" runat="server" Text='<%# Eval("OfficeAssignment.Location") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="InstructorOfficeTextBox" runat="server" The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
54
Text='<%# Eval("OfficeAssignment.Location") %>' Width="7em" oninit="InstructorOfficeTextBox_Init"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> </Columns> <SelectedRowStyle BackColor="LightGray"></SelectedRowStyle> </asp:gridview> <asp:label id="ErrorMessageLabel" runat="server" text="" visible="false" viewstatemode="Disabled"> </asp:label>
This GridView control enables row selection, highlights the selected row with a light gray background color, and specifies handlers (which you'll create later) for the SelectedIndexChanged and Updating events. It also specifies PersonID for the DataKeyNames property, so that the key value of the selected row can be passed to another control that you'll add later. The last column contains the instructor's office assignment, which is stored in a navigation property of the Person entity because it comes from an associated entity. Notice that the EditItemTemplate element specifies Eval instead of Bind, because the GridView control cannot directly bind to navigation properties in order to update them. You'll update the office assignment in code. To do that, you'll need a reference to the TextBox control, and you'll get and save that in the TextBox control's Init event. Following the GridView control is a Label control that's used for error messages. The control's Visible property is false, and view state is turned off, so that the label will appear only when code makes it visible in response to an error. Open the Instructors.aspx.cs file and add the following using statement:
using ContosoUniversity.DAL;
Add a private class field immediately after the partial-class name declaration to hold a reference to the office assignment text box.
private TextBox instructorOfficeTextBox;
Add a stub for the SelectedIndexChanged event handler that you'll add code for later. Also add a handler for the office assignment TextBox control's Init event so that you can store a reference to the TextBox control. You'll use this reference to get the value the user entered in order to update the entity associated with the navigation property.
protected void InstructorsGridView_SelectedIndexChanged(object sender, EventArgs e) { } protected void InstructorOfficeTextBox_Init(object sender, EventArgs e) { The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
55
You'll use the GridView control's Updating event to update the Location property of the associated OfficeAssignment entity. Add the following handler for the Updating event:
protected void InstructorsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) { using (var context = new SchoolEntities()) { var instructorBeingUpdated = Convert.ToInt32(e.Keys[0]); var officeAssignment = (from o in context.OfficeAssignments where o.InstructorID == instructorBeingUpdated select o).FirstOrDefault(); try { if (String.IsNullOrWhiteSpace(instructorOfficeTextBox.Text) == false) { if (officeAssignment == null) { context.OfficeAssignments.AddObject(OfficeAssignment.CreateOfficeAssignment(instructo rBeingUpdated, instructorOfficeTextBox.Text, null)); } else { officeAssignment.Location = instructorOfficeTextBox.Text; } } else { if (officeAssignment != null) { context.DeleteObject(officeAssignment); } } context.SaveChanges(); } catch (Exception) { e.Cancel = true; ErrorMessageLabel.Visible = true; ErrorMessageLabel.Text = "Update failed."; //Add code to log the error.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
56
} } }
This code is run when the user clicks Update in a GridView row. The code uses LINQ to Entities to retrieve the OfficeAssignment entity that's associated with the current Person entity, using the PersonID of the selected row from the event argument. The code then takes one of the following actions depending on the value in the InstructorOfficeTextBox control:
If the text box has a value and there's no OfficeAssignment entity to update, it creates one. If the text box has a value and there's an OfficeAssignment entity, it updates the Location property value. If the text box is empty and an OfficeAssignment entity exists, it deletes the entity.
After this, it saves the changes to the database. If an exception occurs, it displays an error message. Run the page.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
57
Change any of these values, including Office Assignment. Click Update and you'll see the changes reflected in the list.
The Where parameter contains the value of the PersonID of the instructor whose row is selected in the InstructorsGridView control. The Where property contains a subselect command that gets all associated Person entities from a Course entity's People navigation property and selects the Course entity only if one of the associated Person entities contains the selected PersonID value. To create the GridView control., add the following markup immediately following the CoursesEntityDataSource control (before the closing </div> tag):
<asp:GridView ID="CoursesGridView" runat="server" DataSourceID="CoursesEntityDataSource" AllowSorting="True" AutoGenerateColumns="False" SelectedRowStyle-BackColor="LightGray" DataKeyNames="CourseID"> <EmptyDataTemplate> <p>No courses found.</p> </EmptyDataTemplate> <Columns> <asp:CommandField ShowSelectButton="True" /> <asp:BoundField DataField="CourseID" HeaderText="ID" ReadOnly="True" SortExpression="CourseID" /> <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
58
<asp:TemplateField HeaderText="Department" SortExpression="DepartmentID"> <ItemTemplate> <asp:Label ID="GridViewDepartmentLabel" runat="server" Text='<%# Eval("Department.Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Because no courses will be displayed if no instructor is selected, an EmptyDataTemplate element is included. Run the page.
Select an instructor who has one or more courses assigned, and the course or courses appear in the list. (Note: although the database schema allows multiple courses, in the test data supplied with the database no instructor actually has more than one course. You can add courses to the database yourself using the Server Explorer window or the CoursesAdd.aspx page, which you'll add in a later tutorial.)
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
59
The CoursesGridView control shows only a few course fields. To display all the details for a course, you'll use a DetailsView control for the course that the user selects. In Instructors.aspx, add the following markup after the closing </div> tag (make sure you place this markup after the closing div tag, not before it):
<div> <h3>Course Details</h3> <asp:EntityDataSource ID="CourseDetailsEntityDataSource" runat="server" ContextTypeName="ContosoUniversity.DAL.SchoolEntities" EnableFlattening="False" EntitySetName="Courses" AutoGenerateWhereClause="False" Where="it.CourseID = @CourseID" Include="Department,OnlineCourse,OnsiteCourse,StudentGrades.Person" OnSelected="CourseDetailsEntityDataSource_Selected"> <WhereParameters> <asp:ControlParameter ControlID="CoursesGridView" Type="Int32" Name="CourseID" PropertyName="SelectedValue" /> </WhereParameters> </asp:EntityDataSource> <asp:DetailsView ID="CourseDetailsView" runat="server" AutoGenerateRows="False" DataSourceID="CourseDetailsEntityDataSource"> <EmptyDataTemplate> <p> No course selected.</p> </EmptyDataTemplate> <Fields> <asp:BoundField DataField="CourseID" HeaderText="ID" ReadOnly="True" SortExpression="CourseID" /> <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> <asp:BoundField DataField="Credits" HeaderText="Credits" SortExpression="Credits" /> <asp:TemplateField HeaderText="Department">
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
60
<ItemTemplate> <asp:Label ID="DetailsViewDepartmentLabel" runat="server" Text='<%# Eval("Department.Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Location"> <ItemTemplate> <asp:Label ID="LocationLabel" runat="server" Text='<%# Eval("OnsiteCourse.Location") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="URL"> <ItemTemplate> <asp:Label ID="URLLabel" runat="server" Text='<%# Eval("OnlineCourse.URL") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView> </div>
This markup creates an EntityDataSource control that's bound to the Courses entity set. The Where property selects a course using the CourseID value of the selected row in the courses GridView control. The markup specifies a handler for the Selected event, which you'll use later for displaying student grades, which is another level lower in the hierarchy. In Instructors.aspx.cs, create the following stub for the CourseDetailsEntityDataSource_Selected method. (You'll fill this stub out later in the tutorial; for now, you need it so that the page will compile and run.)
protected void CourseDetailsEntityDataSource_Selected(object sender, EntityDataSourceSelectedEventArgs e) { }
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
61
Initially there are no course details because no course is selected. Select an instructor who has a course assigned, and then select a course to see the details.
62
<EmptyDataTemplate> <p>No student grades found.</p> </EmptyDataTemplate> <LayoutTemplate> <table border="1" runat="server" id="itemPlaceholderContainer"> <tr runat="server"> <th runat="server"> Name </th> <th runat="server"> Grade </th> </tr> <tr id="itemPlaceholder" runat="server"> </tr> </table> </LayoutTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="StudentLastNameLabel" runat="server" Text='<%# Eval("Person.LastName") %>' />, <asp:Label ID="StudentFirstNameLabel" runat="server" Text='<%# Eval("Person.FirstMidName") %>' /> </td> <td> <asp:Label ID="StudentGradeLabel" runat="server" Text='<%# Eval("Grade") %>' /> </td> </tr> </ItemTemplate> </asp:ListView>
This markup creates a ListView control that displays a list of students and their grades for the selected course. No data source is specified because you'll databind the control in code. The EmptyDataTemplate element provides a message to display when no course is selectedin that case, there are no students to display. The LayoutTemplate element creates an HTML table to display the list, and the ItemTemplate specifies the columns to display. The student ID and the student grade are from the StudentGrade entity, and the student name is from the Person entity that the Entity Framework makes available in the Person navigation property of the StudentGrade entity. In Instructors.aspx.cs, replace the stubbed-out CourseDetailsEntityDataSource_Selected method with the following code:
protected void CourseDetailsEntityDataSource_Selected(object sender, The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
63
EntityDataSourceSelectedEventArgs e) { var course = e.Results.Cast<Course>().FirstOrDefault(); if (course != null) { var studentGrades = course.StudentGrades.ToList(); GradesListView.DataSource = studentGrades; GradesListView.DataBind(); } }
The event argument for this event provides the selected data in the form of a collection, which will have zero items if nothing is selected or one item if a Course entity is selected. If a Course entity is selected, the code uses the First method to convert the collection to a single object. It then gets StudentGrade entities from the navigation property, converts them to a collection, and binds the GradesListView control to the collection. This is sufficient to display grades, but you want to make sure that the message in the empty data template is displayed the first time the page is displayed and whenever a course is not selected. To do that, create the following method, which you'll call from two places:
private void ClearStudentGradesDataSource() { var emptyStudentGradesList = new List<StudentGrade>(); GradesListView.DataSource = emptyStudentGradesList; GradesListView.DataBind(); }
Call this new method from the Page_Load method to display the empty data template the first time the page is displayed. And call it from the InstructorsGridView_SelectedIndexChanged method because that event is raised when an instructor is selected, which means new courses are loaded into the courses GridView control and none is selected yet. Here are the two calls:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ClearStudentGradesDataSource(); } } protected void InstructorsGridView_SelectedIndexChanged(object sender, EventArgs e) { ClearStudentGradesDataSource(); } The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
64
Select an instructor that has a course assigned, and then select the course.
You have now seen a few ways to work with related data. In the following tutorial, you'll learn how to add relationships between existing entities, how to remove relationships, and how to add a new entity that has a relationship to an existing entity.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 4: Working with Related Data
65
You'll also create a page that works with a many-to-many relationship by assigning an instructor to a course (adding a relationship between two entities that you select) or removing an instructor from a course (removing a relationship between two entities that you select). In the database, adding a relationship between an instructor and a course results in a new row being added to the CourseInstructor association table; removing a relationship involves deleting a row from the CourseInstructor association table. However, you do this in the Entity Framework by setting navigation properties, without referring to the CourseInstructor table explicitly.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 5: Working with Related Data, Continued
66
This markup creates an EntityDataSource control that selects courses, that enables inserting, and that specifies a handler for the Inserting event. You'll use the handler to update the Department navigation property when a new Course entity is created. The markup also creates a DetailsView control to use for adding new Course entities. The markup uses bound fields for Course entity properties. You have to enter the CourseID value because this is not a
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 5: Working with Related Data, Continued
67
system-generated ID field. Instead, it's a course number that must be specified manually when the course is created. You use a template field for the Department navigation property because navigation properties cannot be used with BoundField controls. The template field provides a drop-down list to select the department. The drop-down list is bound to the Departments entity set by using Eval rather than Bind, again because you cannot directly bind navigation properties in order to update them. You specify a handler for the DropDownList control's Init event so that you can store a reference to the control for use by the code that updates the DepartmentID foreign key. In CoursesAdd.aspx.cs just after the partial-class declaration, add a class field to hold a reference to the DepartmentsDropDownList control:
private DropDownList departmentDropDownList;
Add a handler for the DepartmentsDropDownList control's Init event so that you can store a reference to the control. This lets you get the value the user has entered and use it to update the DepartmentID value of the Course entity.
protected void DepartmentsDropDownList_Init(object sender, EventArgs e) { departmentDropDownList = sender as DropDownList; }
When the user clicks Insert, the Inserting event is raised before the new record is inserted. The code in the handler gets the DepartmentID from the DropDownList control and uses it to set the value that will be used for the DepartmentID property of the Course entity. The Entity Framework will take care of adding this course to the Courses navigation property of the associated Department entity. It also adds the department to the Department navigation property of the Course entity. Run the page.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 5: Working with Related Data, Continued
68
Enter an ID, a title, a number of credits, and select a department, then click Insert. Run the Courses.aspx page, and select the same department to see the new course.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 5: Working with Related Data, Continued
69
Where="it.HireDate is not null" Select="it.LastName + ', ' + it.FirstMidName AS Name, it.PersonID"> </asp:EntityDataSource> Select an Instructor: <asp:DropDownList ID="InstructorsDropDownList" runat="server" DataSourceID="InstructorsEntityDataSource" AutoPostBack="true" DataTextField="Name" DataValueField="PersonID" OnSelectedIndexChanged="InstructorsDropDownList_SelectedIndexChanged" OnDataBound="InstructorsDropDownList_DataBound"> </asp:DropDownList> <h3>Assign a Course</h3> <br /> Select a Course: <asp:DropDownList ID="UnassignedCoursesDropDownList" runat="server" DataTextField="Title" DataValueField="CourseID"> </asp:DropDownList> <br /> <asp:Button ID="AssignCourseButton" runat="server" Text="Assign" OnClick="AssignCourseButton_Click" /> <br /> <asp:Label ID="CourseAssignedLabel" runat="server" Visible="false" Text="Assignment successful"></asp:Label> <br /> <h3>Remove a Course</h3> <br /> Select a Course: <asp:DropDownList ID="AssignedCoursesDropDownList" runat="server" DataTextField="title" DataValueField="courseiD"> </asp:DropDownList> <br /> <asp:Button ID="RemoveCourseButton" runat="server" Text="Remove" OnClick="RemoveCourseButton_Click" /> <br /> <asp:Label ID="CourseRemovedLabel" runat="server" Visible="false" Text="Removal successful"></asp:Label>
This markup creates an EntityDataSource control that retrieves the name and PersonID of Person entities for instructors. A DropDrownList control is bound to the EntityDataSource control. The DropDownList control specifies a handler for the DataBound event. You'll use this handler to databind the two drop-down lists that display courses. The markup also creates the following group of controls to use for assigning a course to the selected instructor:
A DropDownList control for selecting a course to assign. This control will be populated with courses that are currently not assigned to the selected instructor.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 5: Working with Related Data, Continued
70
A Button control to initiate the assignment. A Label control to display an error message if the assignment fails.
Finally, the markup also creates a group of controls to use for removing a course from the selected instructor. In InstructorsCourses.aspx.cs, add a using statement:
using ContosoUniversity.DAL;
Add a method for populating the two drop-down lists that display courses:
private void PopulateDropDownLists() { using (var context = new SchoolEntities()) { var allCourses = (from c in context.Courses select c).ToList(); var instructorID = Convert.ToInt32(InstructorsDropDownList.SelectedValue); var instructor = (from p in context.People.Include("Courses") where p.PersonID == instructorID select p).First(); var assignedCourses = instructor.Courses.ToList(); var unassignedCourses = allCourses.Except(assignedCourses.AsEnumerable()).ToList(); UnassignedCoursesDropDownList.DataSource = unassignedCourses; UnassignedCoursesDropDownList.DataBind(); UnassignedCoursesDropDownList.Visible = true; AssignedCoursesDropDownList.DataSource = assignedCourses; AssignedCoursesDropDownList.DataBind(); AssignedCoursesDropDownList.Visible = true; } }
This code gets all courses from the Courses entity set and gets the courses from the Courses navigation property of the Person entity for the selected instructor. It then determines which courses are assigned to that instructor and populates the drop-down lists accordingly. Add a handler for the Assign button's Click event:
protected void AssignCourseButton_Click(object sender, EventArgs e)
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 5: Working with Related Data, Continued
71
{ using (var context = new SchoolEntities()) { var instructorID = Convert.ToInt32(InstructorsDropDownList.SelectedValue); var instructor = (from p in context.People where p.PersonID == instructorID select p).First(); var courseID = Convert.ToInt32(UnassignedCoursesDropDownList.SelectedValue); var course = (from c in context.Courses where c.CourseID == courseID select c).First(); instructor.Courses.Add(course); try { context.SaveChanges(); PopulateDropDownLists(); CourseAssignedLabel.Text = "Assignment successful."; } catch (Exception) { CourseAssignedLabel.Text = "Assignment unsuccessful."; //Add code to log the error. } CourseAssignedLabel.Visible = true; } }
This code gets the Person entity for the selected instructor, gets the Course entity for the selected course, and adds the selected course to the Courses navigation property of the instructor's Person entity. It then saves the changes to the database and repopulates the drop-down lists so the results can be seen immediately. Add a handler for the Remove button's Click event:
protected void RemoveCourseButton_Click(object sender, EventArgs e) { using (var context = new SchoolEntities()) { var instructorID = Convert.ToInt32(InstructorsDropDownList.SelectedValue); var instructor = (from p in context.People where p.PersonID == instructorID select p).First(); var courseID = Convert.ToInt32(AssignedCoursesDropDownList.SelectedValue); var courses = instructor.Courses; var courseToRemove = new Course(); foreach (Course c in courses) The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 5: Working with Related Data, Continued
72
{ if (c.CourseID == courseID) { courseToRemove = c; break; } } try { courses.Remove(courseToRemove); context.SaveChanges(); PopulateDropDownLists(); CourseRemovedLabel.Text = "Removal successful."; } catch (Exception) { CourseRemovedLabel.Text = "Removal unsuccessful."; //Add code to log the error. } CourseRemovedLabel.Visible = true; } }
This code gets the Person entity for the selected instructor, gets the Course entity for the selected course, and removes the selected course from the Person entity's Courses navigation property. It then saves the changes to the database and repopulates the drop-down lists so the results can be seen immediately. Add code to the Page_Load method that makes sure the error messages are not visible when there's no error to report, and add handlers for the DataBound and SelectedIndexChanged events of the instructors drop-down list to populate the courses drop-down lists:
protected void Page_Load(object sender, EventArgs e) { CourseAssignedLabel.Visible = false; CourseRemovedLabel.Visible = false; } protected void InstructorsDropDownList_DataBound(object sender, EventArgs e) { PopulateDropDownLists(); } protected void InstructorsDropDownList_SelectedIndexChanged(object sender, EventArgs e) { The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 5: Working with Related Data, Continued
73
PopulateDropDownLists(); }
Select an instructor. The Assign a Course drop-down list displays the courses that the instructor doesn't teach, and the Remove a Course drop-down list displays the courses that the instructor is already assigned to. In the Assign a Course section, select a course and then click Assign. The course moves to the Remove a Course drop-down list. Select a course in the Remove a Course section and click Remove. The course moves to the Assign a Course drop-down list. You have now seen some more ways to work with related data. In the following tutorial, you'll learn how to use inheritance in the data model to improve the maintainability of your application.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 5: Working with Related Data, Continued
74
You can configure the Entity Framework to create Instructor and Student entities that inherit from the Person entity. This pattern of generating an entity inheritance structure from a single database table is called table-per-hierarchy (TPH) inheritance. For courses, the School database uses a different pattern. Online courses and onsite courses are stored in separate tables, each of which has a foreign key that points to the Course table. Information common to both course types is stored only in the Course table.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
75
You can configure the Entity Framework data model so that OnlineCourse and OnsiteCourse entities inherit from the Course entity. This pattern of generating an entity inheritance structure from separate tables for each type, with each separate table referring back to a table that stores data common to all types, is called table per type (TPT) inheritance. TPH inheritance patterns generally deliver better performance in the Entity Framework than TPT inheritance patterns, because TPT patterns can result in complex join queries. This walkthrough demonstrates how to implement TPH inheritance. You'll do that by performing the following steps:
Create Instructor and Student entity types that derive from Person. Move properties that pertain to the derived entities from the Person entity to the derived entities. Set constraints on properties in the derived types. Make the Person entity an abstract entity. Map each derived entity to the Person table with a condition that specifies how to determine whether a Person row represents that derived type.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
76
In the Add Entity dialog box, name the entity Instructor and set its Base type option to Person.
Click OK. The designer creates an Instructor entity that derives from the Person entity. The new entity does not yet have any properties.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
77
Repeat the procedure to create a Student entity that also derives from Person. Only instructors have hire dates, so you need to move that property from the Person entity to the Instructor entity. In the Person entity, right-click the HireDate property and click Cut. Then right-click Properties in the Instructor entity and click Paste.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
78
The hire date of an Instructor entity cannot be null. Right-click the HireDate property, click Properties, and then in the Properties window change Nullable to False.
Repeat the procedure to move the EnrollmentDate property from the Person entity to the Student entity. Make sure that you also set Nullable to False for the EnrollmentDate property.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
79
Now that a Person entity has only the properties that are common to Instructor and Student entities (aside from navigation properties, which you're not moving), the entity can only be used as a base entity in the inheritance structure. Therefore, you need to ensure that it's never treated as an independent entity. Right-click the Person entity, select Properties, and then in the Properties window change the value of the Abstract property to True.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
80
Repeat the procedure for the Students entity, specifying that this entity maps to the Person table when the EnrollmentDate column is not null. Then save and close the data model. Build the project in order to create the new entities as classes and make them available in the designer.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
81
And in the Properties window you can remove Where clause values that are no longer needed, as shown in the following example.
However, because you've changed the markup for EntityDataSource controls to use the ContextTypeName attribute, you cannot run the Configure Data Source wizard on EntityDataSource controls that you've already created. Therefore, you'll make the required changes by changing markup instead. Open the Students.aspx page. In the StudentsEntityDataSource control, remove the Where attribute and add an EntityTypeFilter="Student" attribute. The markup will now resemble the following example:
<asp:EntityDataSource ID="StudentsEntityDataSource" runat="server" ContextTypeName="ContosoUniversity.DAL.SchoolEntities" EnableFlattening="False" The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
82
Setting the EntityTypeFilter attribute ensures that the EntityDataSource control will select only the specified entity type. If you wanted to retrieve both Student and Instructor entity types, you would not set this attribute. (You have the option of retrieving multiple entity types with one EntityDataSource control only if you're using the control for read-only data access. If you're using an EntityDataSource control to insert, update, or delete entities, and if the entity set it's bound to can contain multiple types, you can only work with one entity type, and you have to set this attribute.) Repeat the procedure for the SearchEntityDataSource control, except remove only the part of the Where attribute that selects Student entities instead of removing the property altogether. The opening tag of the control will now resemble the following example:
<asp:EntityDataSource ID="SearchEntityDataSource" runat="server" ContextTypeName="ContosoUniversity.DAL.SchoolEntities" EnableFlattening="False" EntitySetName="People" EntityTypeFilter="Student" Where="it.FirstMidName Like '%' + @StudentName + '%' or it.LastName Like '%' + @StudentName + '%'" >
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
83
Update the following pages that you created in earlier tutorials so that they use the new Student and Instructor entities instead of Person entities, then run them to verify that they work as they did before:
In StudentsAdd.aspx, add EntityTypeFilter="Student" to the StudentsEntityDataSource control. The markup will now resemble the following example:
<asp:EntityDataSource ID="StudentsEntityDataSource" runat="server" ContextTypeName="ContosoUniversity.DAL.SchoolEntities" EnableFlattening="False" EntitySetName="People" EntityTypeFilter="Student" EnableInsert="True" </asp:EntityDataSource>
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
84
In About.aspx, add EntityTypeFilter="Student" to the StudentStatisticsEntityDataSource control and remove Where="it.EnrollmentDate is not null". The markup will now resemble the following example:
<asp:EntityDataSource ID="StudentStatisticsEntityDataSource" runat="server" ContextTypeName="ContosoUniversity.DAL.SchoolEntities" EnableFlattening="False" EntitySetName="People" EntityTypeFilter="Student" Select="it.EnrollmentDate, Count(it.EnrollmentDate) AS NumberOfStudents" OrderBy="it.EnrollmentDate" GroupBy="it.EnrollmentDate" > </asp:EntityDataSource>
In Instructors.aspx and InstructorsCourses.aspx, add EntityTypeFilter="Instructor" to the InstructorsEntityDataSource control and remove Where="it.HireDate is not null". The markup in Instructors.aspx now resembles the following example:
<asp:EntityDataSource ID="InstructorsEntityDataSource" runat="server" ContextTypeName="ContosoUniversity.DAL.SchoolEntities" EnableFlattening="false" EntitySetName="People" EntityTypeFilter="Instructor" Include="OfficeAssignment" EnableUpdate="True"> </asp:EntityDataSource>
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
85
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
86
As a result of these changes, you've improved the Contoso University application's maintainability in several ways. You've moved selection and validation logic out of the UI layer (.aspx markup) and made it an integral part of the data access layer. This helps to isolate your application code from changes that you might make in the future to the database schema or the data model. For example, you could decide that students might be hired as teachers' aids and therefore would get a hire date. You could then add a new property to differentiate students from instructors and update the data model. No code in the web application would need to change except where you wanted to show a hire date for students. Another benefit of adding Instructor and Student entities is that your code is more readily understandable than when it referred to Person objects that were actually students or instructors. You've now seen one way to implement an inheritance pattern in the Entity Framework. In the following tutorial, you'll learn how to use stored procedures in order to have more control over how the Entity Framework accesses the database.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 6: Implementing Table-per-Hierarchy Inheritance
87
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures
88
Copy the following SQL statements and paste them into the stored procedure window, replacing the skeleton stored procedure.
CREATE PROCEDURE [dbo].[InsertStudent] @LastName nvarchar(50), @FirstName nvarchar(50), @EnrollmentDate datetime AS INSERT INTO dbo.Person (LastName, FirstName, EnrollmentDate) VALUES (@LastName, @FirstName, @EnrollmentDate); SELECT SCOPE_IDENTITY() as NewPersonID;
Student entities have four properties: PersonID, LastName, FirstName, and EnrollmentDate. The
database generates the ID value automatically, and the stored procedure accepts parameters for the
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures 89
other three. The stored procedure returns the value of the new row's record key so that the Entity Framework can keep track of that in the version of the entity it keeps in memory. Save and close the stored procedure window. Create an InsertInstructor stored procedure in the same manner, using the following SQL statements:
CREATE PROCEDURE [dbo].[InsertInstructor] @LastName nvarchar(50), @FirstName nvarchar(50), @HireDate datetime AS INSERT INTO dbo.Person (LastName, FirstName, HireDate) VALUES (@LastName, @FirstName, @HireDate); SELECT SCOPE_IDENTITY() as NewPersonID;
Create Update stored procedures for Student and Instructor entities also. (The database already has a DeletePerson stored procedure which will work for both Instructor and Student entities.)
CREATE PROCEDURE [dbo].[UpdateStudent] @PersonID int, @LastName nvarchar(50), @FirstName nvarchar(50), @EnrollmentDate datetime AS UPDATE Person SET LastName=@LastName, FirstName=@FirstName, EnrollmentDate=@EnrollmentDate WHERE PersonID=@PersonID; CREATE PROCEDURE [dbo].[UpdateInstructor] @PersonID int, @LastName nvarchar(50), @FirstName nvarchar(50), @HireDate datetime AS UPDATE Person SET LastName=@LastName, FirstName=@FirstName, HireDate=@HireDate WHERE PersonID=@PersonID;
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures
90
In this tutorial you'll map all three functions -- insert, update, and delete -- for each entity type. The Entity Framework version 4 allows you to map just one or two of these functions to stored procedures without mapping the others, with one exception: if you map the update function but not the delete function, the Entity Framework will throw an exception when you attempt to delete an entity. In the Entity Framework version 3.5, you did not have this much flexibility in mapping stored procedures: if you mapped one function you were required to map all three. To create a stored procedure that reads rather than updates data, create one that selects all Course entities, using the following SQL statements:
CREATE PROCEDURE [dbo].[GetCourses] AS SELECT CourseID, Title, Credits, DepartmentID FROM dbo.Course
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures
91
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures
92
The Mapping Details window appears, in which you can specify stored procedures that the Entity Framework should use for inserting, updating, and deleting entities of this type.
Set the Insert function to InsertStudent. The window shows a list of stored procedure parameters, each of which must be mapped to an entity property. Two of these are mapped automatically because the names are the same. There's no entity property named FirstName, so you must manually select FirstMidName from a drop-down list that shows available entity properties. (This is because you changed the name of the FirstName property to FirstMidName in the first tutorial.)
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures
93
In the same Mapping Details window, map the Update function to the UpdateStudent stored procedure (make sure you specify FirstMidName as the parameter value for FirstName, as you did for the Insert stored procedure) and the Delete function to the DeletePerson stored procedure.
Follow the same procedure to map the insert, update, and delete stored procedures for instructors to the Instructor entity.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures
94
For stored procedures that read rather than update data, you use the Model Browser window to map the stored procedure to the entity type it returns. In the data model designer, right-click the design surface and select Model Browser. Open the SchoolModel.Store node and then open the Stored Procedures node. Then right-click the GetCourses stored procedure and select Add Function Import.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures
95
In the Add Function Import dialog box, under Returns a Collection Of select Entities, and then select Course as the entity type returned. When you're done, click OK. Save and close the .edmx file.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures
96
Run the Students.aspx page and the new student appears in the list.
Change the name to verify that the update function works, and then delete the student to verify that the delete function works.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures
97
The page now uses the GetCourses stored procedure to retrieve the list of all courses. Run the page to verify that it works as it did before. (Navigation properties of entities retrieved by a stored procedure might not be automatically populated with the data related to those entities, depending on ObjectContext default settings. For more information, see Loading Related Objects in the MSDN Library.) In the next tutorial, you'll learn how to use Dynamic Data functionality to make it easier to program and test data formatting and validation rules. Instead of specifying on each web page rules such as data format strings and whether or not a field is required, you can specify such rules in data model metadata and they're automatically applied on every page.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 7: Using Stored Procedures
98
Fields are automatically formatted for display based on their data type. Fields are automatically validated based on their data type. You can add metadata to the data model to customize formatting and validation behavior. When you do this, you can add the formatting and validation rules in just one place, and they're automatically applied everywhere you access the fields using Dynamic Data controls.
To see how this works, you'll change the controls you use to display and edit fields in the existing Students.aspx page, and you'll add formatting and validation metadata to the name and date fields of the Student entity type.
99
<asp:DynamicControl ID="LastNameTextBox" runat="server" DataField="LastName" Mode="Edit" /> <asp:DynamicControl ID="FirstNameTextBox" runat="server" DataField="FirstMidName" Mode="Edit" /> </EditItemTemplate> <ItemTemplate> <asp:DynamicControl ID="LastNameLabel" runat="server" DataField="LastName" Mode="ReadOnly" />, <asp:DynamicControl ID="FirstNameLabel" runat="server" DataField="FirstMidName" Mode="ReadOnly" /> </ItemTemplate> </asp:TemplateField> <asp:DynamicField DataField="EnrollmentDate" HeaderText="Enrollment Date" SortExpression="EnrollmentDate" />
This markup uses DynamicControl controls in place of TextBox and Label controls in the student name template field, and it uses a DynamicField control for the enrollment date. No format strings are specified. Add a ValidationSummary control after the StudentsGridView control.
<asp:ValidationSummary ID="StudentsValidationSummary" runat="server" ShowSummary="true" DisplayMode="BulletList" Style="color: Red" />
In the SearchGridView control replace the markup for the Name and Enrollment Date columns as you did in the StudentsGridView control, except omit the EditItemTemplate element. The Columns element of the SearchGridView control now contains the following markup:
<asp:TemplateField HeaderText="Name" SortExpression="LastName"> <ItemTemplate> <asp:DynamicControl ID="LastNameLabel" runat="server" DataField="LastName" Mode="ReadOnly" />, <asp:DynamicControl ID="FirstNameLabel" runat="server" DataField="FirstMidName" Mode="ReadOnly" /> </ItemTemplate> </asp:TemplateField> <asp:DynamicField DataField="EnrollmentDate" HeaderText="Enrollment Date" SortExpression="EnrollmentDate" />
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 8: Using Dynamic Data Functionality to Format and Validate Data
100
This code specifies that Dynamic Data will provide formatting and validation in these data-bound controls for fields of the Student entity. If you get an error message like the following example when you run the page, it typically means you've forgotten to call the EnableDynamicData method in Page_Init:
Could not determine a MetaTable. A MetaTable could not be determined for the data source 'StudentsEntityDataSource' and one could not be inferred from the request URL.
In the Enrollment Date column, the time is displayed along with the date because the property type is DateTime. You'll fix that later. For now, notice that Dynamic Data automatically provides basic data validation. For example, click Edit, clear the date field, click Update, and you see that Dynamic Data automatically makes this a required field because the value is not nullable in the data model. The page displays an asterisk after the field and an error message in the ValidationSummary control:
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 8: Using Dynamic Data Functionality to Format and Validate Data
101
You could omit the ValidationSummary control, because you can also hold the mouse pointer over the asterisk to see the error message:
Dynamic Data will also validate that data entered in the Enrollment Date field is a valid date:
As you can see, this is a generic error message. In the next section you'll see how to customize messages as well as validation and formatting rules.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 8: Using Dynamic Data Functionality to Format and Validate Data
102
In the DAL folder, create a new class file, name it Student.cs, and replace the template code in it with the following code.
using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace ContosoUniversity.DAL { [MetadataType(typeof(StudentMetadata))] public partial class Student { } public class StudentMetadata The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 8: Using Dynamic Data Functionality to Format and Validate Data
103
{ [DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)] public DateTime EnrollmentDate { get; set; } [StringLength(25, ErrorMessage = "First name must be 25 characters or less in length.")] [Required(ErrorMessage="First name is required.")] public String FirstMidName { get; set; } [StringLength(25, ErrorMessage = "Last name must be 25 characters or less in length.")] [Required(ErrorMessage = "Last name is required.")] public String LastName { get; set; } } }
This code creates a partial class for the Student entity. The MetadataType attribute applied to this partial class identifies the class that you're using to specify metadata. The metadata class can have any name, but using the entity name plus "Metadata" is a common practice. The attributes applied to properties in the metadata class specify formatting, validation, rules, and error messages. The attributes shown here will have the following results:
EnrollmentDate will display as a date (without a time).
Both name fields must be 25 characters or less in length, and a custom error message is provided. Both name fields are required, and a custom error message is provided.
Run the Students.aspx page again, and you see that the dates are now displayed without times:
Edit a row and try to clear the values in the name fields. The asterisks indicating field errors appear as soon as you leave a field, before you click Update. When you click Update, the page displays the error message text you specified.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 8: Using Dynamic Data Functionality to Format and Validate Data
104
Try to enter names that are longer than 25 characters, click Update, and the page displays the error message text you specified.
Now that you've set up these formatting and validation rules in the data model metadata, the rules will automatically be applied on every page that displays or allows changes to these fields, so long as you use DynamicControl or DynamicField controls. This reduces the amount of redundant code you have
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 8: Using Dynamic Data Functionality to Format and Validate Data
105
to write, which makes programming and testing easier, and it ensures that data formatting and validation are consistent throughout an application. This concludes this series of tutorials on Getting Started with the Entity Framework. For more resources to help you learn how to use the Entity Framework, visit the following sites:
The Entity Framework Team Blog Entity Framework in the MSDN Library Entity Framework in the MSDN Data Developer Center EntityDataSource Web Server Control Overview in the MSDN Library EntityDataSource control API reference in the MSDN Library Entity Framework Forums on MSDN Julie Lerman's blog
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Part 8: Using Dynamic Data Functionality to Format and Validate Data
106
Disclaimer
This document is provided as-is. Information and views expressed in this document, including URL and other Internet website references, may change without notice. You bear the risk of using it. Some examples depicted herein are provided for illustration only and are fictitious. No real association or connection is intended or should be inferred. This document does not provide you with any legal rights to any intellectual property in any Microsoft product. You may copy and use this document for your internal, reference purposes. This document is confidential and proprietary to Microsoft. It is disclosed and can be used only pursuant to a nondisclosure agreement. 2011 Microsoft. All Rights Reserved. Microsoft is a trademark of the Microsoft group of companies. All other trademarks are property of their respective owners.
The Entity Framework and ASP.NET Getting Started 2011 Microsoft Corporation Disclaimer
107