<docbook><section><title>VirtEntityFrameworkSchoolDbWinFormApp</title><bridgehead class="http://www.w3.org/1999/xhtml:h2">Virtuoso Entity Framework School DB Windows Form Application</bridgehead>
<para>This tutorial demonstrates how  Visual Studio 2008 can be used to generate mapping files for an Entity Data Model (EDM), based on a 1:1 mapping to tables in the School database.
 This will be done by creating a Windows Forms application in Visual Studio, create queries that access data in the School model, bind the query results to display controls to show the results of the queries, and then make updates to objects and persist the changes to the database.</para><bridgehead class="http://www.w3.org/1999/xhtml:h3">Pre-requisites</bridgehead>
<para>In order to create an Entity Framework  Application in your own environment you will need:</para><para>1.
 Microsoft Visual Studio 2008 SP1, the ADO.NET Entity Framework runtime and associated tools are included in Visual Studio 2008 SP1.</para><para>2.
 A running Virtuoso Universal Server instance.</para><bridgehead class="http://www.w3.org/1999/xhtml:h3">Create the School database and schema</bridgehead>
<para>1.
 Download the <ulink url="http://download.openlinksw.com/packages/5.0/virtuoso/schools_db_dav.vad">Schools Database</ulink>  VAD (<computeroutput>schools_db_dav.vad</computeroutput>) package.</para><para>2.
 Navigate to the <emphasis>System Admin</emphasis> -&gt; <emphasis>Packages</emphasis> tab of the Virtuoso Conductor.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_1.png" /></figure></para><para>3.
 Scroll down to the <emphasis>Install Package</emphasis> section of the tab, use the <emphasis>Upload Package</emphasis> option <emphasis>Browse</emphasis> button to locate the <span style="color: red">
      UNKNOWN tag:
      http://www.w3.org/1999/xhtml:nowikischools_db_dav.vad</span> package and click <emphasis />proceed<emphasis />.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_2.png" /></figure></para><para>4.
 Click the <emphasis>Proceed</emphasis> button to begin the installation process.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_3.png" /></figure></para><para>5.
 Once complete return to the <emphasis>Packages</emphasis> tab and scroll down to confirm the <emphasis>schools_db</emphasis> package is listed as installed.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_4.png" /></figure></para><bridgehead class="http://www.w3.org/1999/xhtml:h4">Create the Course Manager application using Visual Studio</bridgehead>
<para>1.
 Launch the Visual Studio 2008 SP1 IDE.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtAdoNetDataServices_1.png" /></figure></para><para>2.
 On the <emphasis>File</emphasis> menu click <emphasis>New Project</emphasis>.</para><para>3.
 Choose either <emphasis>Visual Basic</emphasis> or <emphasis>Visual C#</emphasis> in the <emphasis>Project Types</emphasis> pane.</para><para>4.
 Select <emphasis>Windows Forms Application</emphasis> in the <emphasis>Templates</emphasis> pane.</para><para>5.
 Enter <emphasis>CourseManager</emphasis><emphasis> for the project name, and then click </emphasis>OK<emphasis>.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_5.png" /></figure></emphasis></para><bridgehead class="http://www.w3.org/1999/xhtml:h4">Create the Course Viewer form</bridgehead>
<para>1.
 In the <emphasis>CourseManager</emphasis> project, select the default form (Form1).</para><para>2.
 In the <emphasis>File Properties</emphasis> pane, change the File Name to <emphasis>CourseViewer.vb</emphasis> or <emphasis>CourseViewer.cs</emphasis>.</para><para>3.
 In <emphasis>Solution Explorer</emphasis>, double-click <emphasis>CourseViewer.vb</emphasis> or <emphasis>CourseViewer.cs</emphasis> to open the form.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_6.png" /></figure></para><para>4.
 In the <emphasis>Toolbox</emphasis>, expand <emphasis>Common Controls</emphasis>, drag the <emphasis>ComboBox</emphasis> control to the form, and change the name of the control to <emphasis>departmentList</emphasis>.</para><para>5.
 In the <emphasis>Toolbox</emphasis>, drag the <emphasis>Button</emphasis> control to the form, change the <emphasis>Name</emphasis> of the control to <emphasis>closeForm</emphasis>, and change the <emphasis>Text</emphasis> value to <emphasis>Close</emphasis>.</para><para>6.
 In the <emphasis>Toolbox</emphasis>, expand <emphasis>Data</emphasis>, drag the <emphasis>DataGridView</emphasis> control to the form, and change the <emphasis>Name</emphasis> of the control to <emphasis>courseGridView</emphasis>.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_7.png" /></figure></para><para>7.
 Double-click the <emphasis>closeForm</emphasis> button control.
 This opens the code page for the form and creates the <emphasis>closeForm_Click</emphasis> event handler method.</para><para>8.
 In the <emphasis>closeForm_Click</emphasis> event handler method, type the following code that closes the form:</para><programlisting>Visual Basic

&#39; Close the form.
Me.Close()

C#

// Close the form.
this.Close();
</programlisting><para> </para><bridgehead class="http://www.w3.org/1999/xhtml:h3">Generating the School Entity Data Mode</bridgehead>
<bridgehead class="http://www.w3.org/1999/xhtml:h4">Add the ADO.NET Entity Data Model item template</bridgehead>
<para>1.
 Select the <emphasis>CourseManager</emphasis> project in <emphasis>Solution Explorer</emphasis>, right-click, point to <emphasis>Add</emphasis>, and then click <emphasis>New Item</emphasis>.</para><para>2.
 Select <emphasis>ADO.NET Entity Data Model</emphasis> in the <emphasis>Templates</emphasis> pane.</para><para>3.
 Type <emphasis>School.edmx</emphasis> for the model name and click <emphasis>Add</emphasis>.
 The opening page of the Entity Data Model Wizard is displayed.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_8.png" /></figure></para><bridgehead class="http://www.w3.org/1999/xhtml:h4">Generate the EDM</bridgehead>
<para>1.
 Select <emphasis>Generate from database</emphasis> in the <emphasis>Choose Model Contents</emphasis> dialog box.
 Then click <emphasis>Next</emphasis>.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_9.png" /></figure></para><para>2.
 Click the <emphasis>New Connection</emphasis> button.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_10.png" /></figure></para><para>3.
 Choose the <emphasis>OpenLink Virtuoso Data Source</emphasis> and click <emphasis>Continue</emphasis>.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_11.png" /></figure></para><para>4.
 In the <emphasis>Connection Properties</emphasis> dialog specify the <emphasis>hostname</emphasis>, <emphasis>portno</emphasis>, <emphasis>username</emphasis> and password for the target Virtuoso Server and click the <emphasis>Advanced</emphasis> button.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_12.png" /></figure></para><para>5.
 In the <emphasis>Advanced Properties</emphasis> dialog set the <emphasis>Database</emphasis> paramter to <emphasis>School</emphasis> and click OK.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_13.png" /></figure></para><para>6.
 Press the <emphasis>Test Connection</emphasis> dialog to verify the database is accessible.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_14.png" /></figure></para><para>7.
 Set the <emphasis>entity connect string</emphasis> name to <emphasis>SchoolEntities</emphasis> and click <emphasis>Next</emphasis>.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_15.png" /></figure></para><para>8.
 In the <emphasis>Choose your Database Objects</emphasis> page select the <emphasis>Tables</emphasis> check box to select all table in the <emphasis>School</emphasis> database for addition to the EDM, set the <emphasis>Model Namespace</emphasis> to <emphasis>SchoolModel</emphasis> and click <emphasis>Finish</emphasis>.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_16.png" /></figure></para><bridgehead class="http://www.w3.org/1999/xhtml:h4">View the EDM in the ADO.NET Entity Data Model Designer</bridgehead>
<para>1.
 In the <emphasis>Solution Explorer</emphasis>, double-click the <emphasis>School.edmx</emphasis> file.
 This displays the School model in the ADO.NET Entity Data Model Designer window.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_17.png" /></figure></para><para>2.
 From the <emphasis>View</emphasis> menu, select <emphasis>Other Windows</emphasis>, and then click <emphasis>Model Browser</emphasis> to display the <emphasis>Entity Model Browser</emphasis> window.</para><para>3.
 Expand the <emphasis>SchoolModel</emphasis> and <emphasis>SchoolModel.Store</emphasis> nodes to view conceptual and store definitions, respectively.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_20.png" /></figure></para><para>4.
 From the <emphasis>View</emphasis> menu, select <emphasis>Other Windows</emphasis>, click <emphasis>Mapping Details</emphasis>, and then click an entity (Person for example) or association in the Entity Designer.</para><para>5.
 This displays the Entity Mapping Details window with information about the object-relational mapping for the selected object.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_21.png" /></figure></para><bridgehead class="http://www.w3.org/1999/xhtml:h3">Querying Entities and Associations</bridgehead>
<para>This section creates strongly-typed queries against the CLR objects that represent entities and associations in the School model, and bind display controls to the object collections returned from these queries.</para><bridgehead class="http://www.w3.org/1999/xhtml:h4">Query the departments in the School database</bridgehead>
<para> 1.
 At the beginning of the code file for the <emphasis>CourseViewer</emphasis> form, add the following <emphasis>using</emphasis> (C#) or <emphasis>Imports</emphasis> (Visual Basic) statements to reference the model created from the School database and the entity namespace.</para><programlisting>Visual Basic

Imports System.Data.Objects
Imports System.Data.Objects.DataClasses

C#

using System.Data.Objects;
using System.Data.Objects.DataClasses;
</programlisting><para> 2.
 At the top of the partial class definition for the <emphasis>CourseViewer</emphasis> form, add the following code that creates an <emphasis>ObjectContext</emphasis> instance.</para><programlisting>Visual Basic

&#39; Create an ObjectContext instance based on SchoolEntity.
Private schoolContext As SchoolEntities

C#

// Create an ObjectContext instance based on SchoolEntity.
private SchoolEntities schoolContext;
</programlisting><para> 3.
 In the <emphasis>CourseViewer</emphasis> form designer, double-click the <emphasis>CourseViewer</emphasis> form.
 This opens the code page for the form and creates the <emphasis>courseViewer _Load</emphasis> event handler method.</para><para>4.
 In the <emphasis>courseViewer _Load</emphasis> event handler method, copy and paste the following code that defines the <emphasis>DataGridView</emphasis>, executes a query that returns a collection of departments (ordered by <emphasis>Name</emphasis>), and binds the collection of <emphasis>Department</emphasis> objects to the departmentList control.</para><programlisting>Visual Basic

&#39; Initialize the ObjectContext.
schoolContext = New SchoolEntities()

&#39; Define a query that returns all Department objects and related
&#39; Course objects, ordered by name.
Dim departmentQuery As ObjectQuery(Of Department) = _
    schoolContext.Department.Include(&quot;Course&quot;).OrderBy(&quot;it.Name&quot;)

Try
    &#39; Bind the ComboBox control to the query, which is 
    &#39; executed during data binding.
    Me.departmentList.DisplayMember = &quot;Name&quot;
    Me.departmentList.DataSource = departmentQuery
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

C#

// Initialize the ObjectContext.
schoolContext = new SchoolEntities();

// Define a query that returns all Department objects and related
// Course objects, ordered by name.
ObjectQuery&lt;Department&gt; departmentQuery =
    schoolContext.Department.Include(&quot;Course&quot;).OrderBy(&quot;it.Name&quot;);

try
{
    // Bind the ComboBox control to the query, which is
    // executed during data binding.
    this.departmentList.DisplayMember = &quot;Name&quot;;
    this.departmentList.DataSource = departmentQuery;
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
</programlisting><para> </para><bridgehead class="http://www.w3.org/1999/xhtml:h4">Display courses for the selected department </bridgehead>
<para>1.
 In the <emphasis>CourseViewer</emphasis> form designer, double-click the <emphasis>departmentList</emphasis> control.
 This creates the <emphasis>departmentList_SelectedIndexChanged</emphasis> event handler method.</para><para>2.
 Paste the following code that loads the courses that are related to the selected department.</para><programlisting>Visual Basic

Try
    &#39; Get the object for the selected department.
    Dim department As Department = _
        CType(Me.departmentList.SelectedItem, Department)

    &#39; Bind the grid view to the collection of Course objects 
    &#39; that are related to the selected Department object.
    courseGridView.DataSource = department.Course

    &#39; Hide the columns that are bound to the navigation properties on Course.
    courseGridView.Columns(&quot;Department&quot;).Visible = False
    courseGridView.Columns(&quot;CourseGrade&quot;).Visible = False
    courseGridView.Columns(&quot;OnlineCourse&quot;).Visible = False
    courseGridView.Columns(&quot;OnsiteCourse&quot;).Visible = False
    courseGridView.Columns(&quot;Person&quot;).Visible = False

    courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

C#

try
{
    // Get the object for the selected department.
    Department department = 
        (Department)this.departmentList.SelectedItem;

    // Bind the grid view to the collection of Course objects 
    // that are related to the selected Department object.
    courseGridView.DataSource = department.Course;

    // Hide the columns that are bound to the navigation properties on Course.
    courseGridView.Columns[&quot;Department&quot;].Visible = false;
    courseGridView.Columns[&quot;CourseGrade&quot;].Visible = false;
    courseGridView.Columns[&quot;OnlineCourse&quot;].Visible = false;
    courseGridView.Columns[&quot;OnsiteCourse&quot;].Visible = false;
    courseGridView.Columns[&quot;Person&quot;].Visible = false;

    courseGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
</programlisting><para> </para><bridgehead class="http://www.w3.org/1999/xhtml:h3">Inserting and Updating Data</bridgehead>
<para>In this section the changes made to Course objects bound are saved to the DataGridView control to the database and also run the completed Course Manager application.</para><bridgehead class="http://www.w3.org/1999/xhtml:h4">Save changes made to objects</bridgehead>
<para>1.
 In the <emphasis>Toolbox</emphasis>, expand <emphasis>Common Controls</emphasis>, drag the <emphasis>Button</emphasis> control to the <emphasis>CourseViewer</emphasis> form designer, change the <emphasis>name</emphasis> of the control to <emphasis>saveChanges</emphasis>, and change the <emphasis>Text</emphasis> value to <emphasis>Update</emphasis>.</para><para>2.
 In the <emphasis>CourseViewer</emphasis> form designer, double-click the <emphasis>saveChanges</emphasis> control.
 This creates the <emphasis>saveChanges_Click</emphasis> event handler method.</para><para>3.
 Paste the following code that saves object changes to the database.</para><programlisting>Visual Basic

Try
    &#39; Save object changes to the database, display a message, 
    &#39; and refresh the form.
    schoolContext.SaveChanges()
    MessageBox.Show(&quot;Changes saved to the database.&quot;)
    Me.Refresh()
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

C#

try
{
    // Save object changes to the database, display a message,
    // and refresh the form.
    schoolContext.SaveChanges();
    MessageBox.Show(&quot;Changes saved to the database.&quot;);
    this.Refresh();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
</programlisting><para> 4.
 In the <emphasis>closeForm_Click</emphasis> event handler method, type the following code.
 This code disposes of the object context before the form is closed.</para><programlisting>Visual Basic

   &#39; Dispose the object context.
    schoolContext.Dispose()

C#

    // Dispose the object context.
    schoolContext.Dispose();
</programlisting><para> </para><bridgehead class="http://www.w3.org/1999/xhtml:h4">Build and run the Class Scheduling application</bridgehead>
<para>1.
 From the <emphasis>Debug</emphasis> menu, select <emphasis>Start Debugging</emphasis> or <emphasis>Start Without Debugging</emphasis>, to build and run the application.</para><para>2.
 When the form loads, select a department from the <emphasis>ComboBox</emphasis> control to display the courses that belong to that department.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_18.png" /></figure></para><para>3.
 In the <emphasis>DataGridView</emphasis>, update course information or add a new course and then click <emphasis>Update</emphasis> to save changes to the database and displays a message box that declares the number of saved changes.
<figure><graphic fileref="VirtEntityFrameworkSchoolDbWinFormApp/VirtuAdoNetEFApp_19.png" /></figure></para><para>The process is now complete.
</para></section></docbook>