8.12.5. Using EDM to create Entity Framework based applications
Now that a Microsoft Entity Data Model has been created for the Firebird employee database, Entity Framework applications can be created to make use of it.
Entity Frameworks based ADO.NET Data Service
An ADO.Net Data Service for the Firebird tables can be created using the Entity Data Model created in the Creating EDM in Visual Studio 2008 section .
-
Open the
employee
project created in the Creating EDM in Visual Studio 2008 section .
-
Right click on the employee project name in the Solution Explorer pane, then select the Project -> Add New Item menu option.
Figure 8.620. employee
-
The
Add New Item
dialog will appear. Choose the
ADO.NET Data Service template
. Give it the name
WebDataService1.svc
, and click
Add
to create the ADO.Net Data Service.
Figure 8.621. Add New Item
-
In the newly created
WebDataService1.svc.cs
Data Service file, add the data source class name of
employeeEntities
(note this is the name set in the Creating EDM in Visual Studio 2008 section) as the
DataService
name. Enable the access to the Data Service by adding the entry
config.SetEntitySetAccessRule("*", EntitySetRights.All);
in the
InitializeService
method.
// C# using System; using System.Web; using System.Collections.Generic; using System.ServiceModel.Web; using System.Linq; using System.Data.Services; namespace SimpleDataService { public class Northwind : DataService<employeeEntities> { public static void InitializeService(IDataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.All); } } }
Figure 8.622. WebDataService1.svc.cs
-
To test the Data Service, simply hit
Ctrl+F5
within Visual Studio. This will start the development web server, run the Data Services server inside and load a Web browser page displaying the list of available tables/entities for the employee database catalog.
Figure 8.623. Data Service test
-
To access a specific entity instance like the
EMPLOYEE
table employee number
2
record, use this convention http://host/vdir/WebDataService1.svc/EMPLOYEE(2) .
Figure 8.624. EMPLOYEES
Notes:
-
Important
- To view
Atom
(the default format returned by an ADO.NET Data Service) in Internet Explorer, you must first ensure that
Feed Reading View
is turned
off
. This can be done on the
Content tab
of
Tools in Internet Options
.
-
If a Data Services entity instance URI page fails to load you can turn
Verbose
errors on by adding
config.UseVerboseErrors = true;
in the
virtuoso.svc.cs InitializeService
method to obtain more detailed information from the server as to why the page failed to load:
public static void InitializeService(IDataServiceConfiguration config) { config.UseVerboseErrors = true; config.SetEntitySetAccessRule("*", EntitySetRights.All); }
Visual Studio Windows DataGrid Form Application
This section details the steps required to create a simple Visual Studio 2008 Windows Form application, with associated DataGridView control for displaying data in selected tables from the target database.
-
Launch the Visual Studio 2008 SP1 IDE.
Figure 8.625. Visual Studio 2008 SP1 IDE
-
Create a
Web Application
project by going to the
File
menu in Visual Studio and choosing
New Project
.
Figure 8.626. Web Application
-
When the New Project window appears, choose either
Visual Basic
or
Visual C#
as your programming language.
-
Within the language category, click on
Windows
and select
Windows Form Application
from the right-hand panel.
-
Choose a name for the project, for example
VirtuosoDataGridApp
, and click
OK
.
Figure 8.627. Web Application
-
In the
Toolbox
, expand
Data Controls
, and drag the
DataGridView
control onto the form.
Figure 8.628. Toolbox
-
Click on the little
arrow
in the top right of the
DataGridView
control. This loads the
DataGridView Task
menu.
Figure 8.629. DataGridView Task
-
Click on the
Choose Data Source
list box.
Figure 8.630. Choose Data Source
-
Click on the
Add Project Data Source
link to connect to a data source.
Figure 8.631. Add Project Data Source
-
In the
Data Source Configuration Wizard
dialog
Choose Data Source Type
page select the
Database
data source type and click
Next
.
Figure 8.632. Data Source Type
-
In the
Data Source Configuration Wizard
dialog
Choose your Data Connection
page, select the
New Connection
button
Figure 8.633. Data Source Configuration Wizard
-
In the
Choose Data Source
dialog, select the OpenLink
Virtuoso Data Source
from the list and click
Continue
.
Figure 8.634. Data Source
-
In the
Add Connection
dialog, specify the
hostname, portno, username and password
for the target Virtuoso Server and check the Save Password check box.
Figure 8.635. Connection Properties
-
Select the
Select Database From List
radio button and choose the
employee
database from the drop down list.
Figure 8.636. Add connection
-
Click OK to add the connection.
-
Press the
Test Connection
dialog to verify that the database is accessible.
Figure 8.637. Test Connection
-
Leave the default connect string
employeeConnectionString
and click
Next
Figure 8.638. employeeConnectionString
-
From the list of available tables returned for the employee database, select the
COUNTRY
table to be associated with the
DataGridView
control.
Figure 8.639. employee database
-
A DataSet for the employee database object is created
Figure 8.640. DataSet
-
From the drop down list box next to the COUNTRY table ensure the DataGridView item is selected
Figure 8.641. DataGridView
-
Drag the COUNTRY DataSet item onto the Form to create a scrollable and editable association of the COUNTRY table object with the Data Grid View automatically.
Figure 8.642. association
-
To test the application, simply hit
Ctrl+F5
within Visual Studio or select
Start Debugging
from the
Debug
menu.
Figure 8.643. Start Debugging
-
The data from the
COUNTRY
table will be displayed in the
DataGrid
.
Figure 8.644. DataGrid
-
A new row can be inserted (updated or deleted) as indicated for the new "1111" record inserted below and the Save button clicked to save the change to the database.
Figure 8.645. new row
-
The Virtuoso Interactive SQL tab of the Conductor can be used to run the query select * from "employee"."fire"."COUNTRY"
Figure 8.646. Interactive SQL
-
To verify the change has been successfully made in the database.
Figure 8.647. verify
The task is now complete.