Entity Framework Query Samples
Devart offers you to try Entity Framework functionality of dotConnect for MySQL with
Entity Framework Query Samples.
The Entity Framework Query Samples application is a test framework for different
databases and data providers within ADO.NET Entity Framework. Its goal is to help
you learn the features of the different query languages available in the Entity
Framework, and to let you visualize how the results and the translated store queries
look like.
This package is based on the original work by Microsoft available here:
http://code.msdn.microsoft.com/EFQuerySamples.
Requirements
MySQL 5.0 or higher is required for Entity Framework Query Samples.
To connect to MySQL you will need a Trial or Professional version of the
corresponding dotConnect for MySQL installed on your
system. ADO.NET Entity Framework requires .NET Framework 3.5 Service Pack 1
installed. Visual Studio 2008 Service Pack 1 is required to compile the sample.
Installation and Configuration
First, create a new copy of NortwhindEF database. The sample contains
folder DB, which includes subfolders for every database with scripts or
ready-to-use database files.
Second, open the App.config file and adjust connection strings for the
databases you are going to use.
After that you can compile and run the sample.
Running Samples
There are 2 ways to run samples: interactive mode and batch mode.
To run samples in an interactive mode, compile and run the project without any command-line arguments.
Using the tree on the left hand side, locate the interesting sample and click Run.
You can run samples against different providers, by selecting them from the drop-down
menu.
To run samples in the batch mode, run "SampleQueries.exe /runall /pause". It
will open the console where each sample is executed sequentially and any exceptions
are reported.
You can also pass "/log filename.txt" which will save results of all samples to a file.
To specify different provider, pass "/connectionString cs" where "cs" is the name of the
connection string defined in "SampleQueries.exe.config"
For example:
SampleQueries.exe /log log.txt /connectionString "NorthwindEF (MySQL)" /runall /pause
MySQL Server does not support some keywords in SQL statements, such as
EXCEPT and MINUS. Therefore some queries that are translated to
SQL with usage of these clauses fail (some tests for relational and set
operators).
Creating Entity Framework Query Samples Model with Entity Developer
You may build the same model yourself with our product
Entity Developer.
Here all steps are described, that you should perform to get the same model
from a database.
Generating model
After you have created needed database, run Entity Developer and import the
database to the model. To do it, perform the following steps.
- Select Create From Database from the File menu.
- Select Entity Framework and click Next.

- Choose database provider in the Provider drop-down
list and set the required connection parameters, then click the Next button.
- Select the check box of the Tables node to select all tables
and check boxes of EmployeesInsert, EmployeesUpdate, and
EmployeesDelete functions, then click the Next button.

- Select Unchanged in the both Case drop-down list
for classes, methods, and their members and click the Next button.

- Enter namespace (for example, NorthwindEFModel), to place the
model in and its name and click the Next button.

- Click the Finish button.

Now we have the model, but it is still not the same as Entity Framework
Query Samples Model. It is not possible to create inheritances and complex
types automatically from database, we should create them manually.
Creating Complex Type
Lets create complex type CommonAddress instead of Address, City, Region,
PostalCode, and Country properties. To do this perform the following actions:
- Click the Address property in the Employee class, then press SHIFT and
click the Country property holding the SHIFT key.
- Drag the selected properties out of the Employee class and drop them on
the empty diagram space.

- In the appeared Choose Action dialog box click New complex type
and then click OK.

- Double-click the complex type, appeared on the diagram.
- In the appeared editor enter CommonAddress in the Name field and click OK.

After this we should remap these properties in the Customer, Supplier,
Order, and PreviousEmployee classes to the CommonAddress complex type.
Perform the following actions:
- Click the Address property in the Customer class, then press SHIFT and
click the Country property holding the SHIFT key.
- Right-click the selection and choose Migrate from the popup menu.
- In the appeared Choose Action dialog box click Existing complex type
and then click OK.

- Right-click the appeared property "CommonAddress" in the Customer class
and choose Edit from the popup menu.
- In the appeared editor enter Address in the Name field and click OK.
- Repeat steps 1-5 for Supplier and PreviousEmployee classes.
Inheritances
Now let's create inheritance relationships like in the Entity Framework
Query Samples Model. This model contains three types of inheritances:
Table Per Type with Order and InternationalOrder classes, Table Per Hierarchy
with Product and DiscontinuedProduct classes, and Table Per Concrete type with
Employee, CurrentEmployee, and PreviousEmployee classes.
Table Per Concrete Type hierarchy
In Table Per Concrete type hierarchy each non-abstract hierarchy class
is mapped to one table. CurrentEmployee is mapped to the Employee
table, PreviousEmployee class is mapped to the PreviousEmployee table.
They have the same parent abstract class Employee.
To create Table Per Concrete type hierarchy perform the folowing steps:
- Double-click the Employee class
- In the appeared editor choose Abstract in the Inheritance Modifier drop-down list and click OK.

- Click the PreviousEmployee class on the diagram and press the DELETE key.
- Click the Add Class to Context button on the
Entity Framework Project toolbar or press ALT+C.
- Right-click the appeared class and choose Rename from the popup menu.
- Enter "PreviousEmployee" and press ENTER.
- Click the Employee class and then PreviousEmployee class while holding down the SHIFT key.
- Click the Add Inheritance to ContextModel button on the
Entity Framework Project toolbar or press ALT+I
- Choose Table Per Concrete Type in the Type drop-down
list and click OK.

- Click the Add Class to Context button on the
Entity Framework Project toolbar or press ALT+C.
- Right-click the appeared class and choose Rename from the popup menu.
- Enter "CurrentEmployee" and press ENTER.
- Click the Employee class and then CurrentEmployee class while holding down the SHIFT key.
- Click the Add Inheritance to ContextModel button on the
Entity Framework Project toolbar or press ALT+I
- Choose Table Per Concrete Type in the Type drop-down
list and click OK.
- Right-click the Employee class and choose Mapping Details from the popup menu.
- In the appeared Mapping Details dialog box select <Delete>
from the drop-down list in the first row of the Storage Entities
grid and click OK.
- Right-click the CurrentEmployee class and choose Mapping Details from the popup menu.
- In the appeared Mapping Details dialog box select Employee
from the drop-down list in the first row of the Storage Entities
grid and click OK.
- Right-click the PreviousEmployee class and choose Mapping Details from the popup menu.
- In the appeared Mapping Details dialog box select PreviousEmployee
from the drop-down list in the first row of the Storage Entities
grid and click OK.

- Drop the association between Employee and Territory entities. Select it and click DELETE button.
- Select a table EmployeesTerritories in the Project Explorer (Storage model node). Right-click it and choose Add class from the popup menu. The new association will be created.

Table Per Type hierarchy
In Table Per Type hierarchy each hierarchy class is mapped to one table.
Order class is mapped to the Order table, InternationalOrder class is
mapped to the InternationalOrder table.
To create Table Per Type hierarchy perform the folowing steps:
- Click the Add Inheritance to ContextModel button on the
Entity Framework Project toolbar or press ALT+I
- In the appeared editor choose Order in the Base
Class drop-down list and InternationalOrder in the
Derived Class drop-down list click OK.

- Choose Table Per Type in the Type drop-down
list and click OK
- Remove an association between Order and InternationalOrder classes. This association becomes redundant after inheritance releationship is defined.
- Drop the OrderId field from the InternationalOrder class.
- Right-click the InternationalOrder class and choose Mapping Details from the popup menu.
- Map the OrderId column to the following property.
Table Per Hierarchy hierarchy
In Table Per Hierarchy hierarchy all hierarchy classes are mapped to
the same table.
To create Table Per Hierarchy hierarchy perform the folowing steps:
- Click the Discontinued property in the Product class and press the DELETE key.
- Drag the DiscontinuedDate property out of the Product class and drop it on
the empty diagram space.
- In the appeared Choose Action dialog box click New derived class
and then click OK.
- Double-click the class, appeared on the diagram.
- In the appeared editor enter DiscontinuedProduct in the Name field and click OK.
- Right-click the Product class and choose Mapping Details from the popup menu.
- In the appeared Mapping Details dialog box select Discontinued in the drop-down list
of the Column column in the Conditions grid.
- Enter "false" into the Value column of the Conditions grid
and click OK.

- Right-click the DiscontinuedProduct class and choose Mapping Details from the popup menu.
- In the appeared Mapping Details dialog box select Discontinued in the drop-down list
of the Column column in the Conditions grid.
- Enter "true" into the Value column of the Conditions grid
and click OK.
Configuring behavior
Now we have almost the same model as Entity Framework Query Samples Model.
All we need to change is to assign these methods to Employee insert, update, and delete actions.
Perform the following steps to complete Entity Framework Query Samples Model:
- Right-click the CurrentEmployee class and choose Configure
behavior from the popup menu.
- Choose Insert in the Behavior drop-down list.
- Choose the EmployeesInsert stored procedure in the Stored
Procedure drop-down list.
- Assign the corresponding CurrentEmployee properties to the
parameters in the grid below by selecting the corresponding property
from the drop-down list in the Property column.

- Choose Update in the Behavior drop-down list.
- Repeat the steps 3 - 4 with the EmployeesUpdate procedure.
- Choose Delete in the Behavior drop-down list.
- Choose the EmployeesDelete stored procedure in the Stored
Procedure drop-down list.
- Assign Employeeid property to the only parameter in the grid
below by selecting the corresponding property from the drop-down
list in the Property column.
- Click OK.
Now look at the Error List window. It displays error messages
and warnings of your model. After we configured behavior of the
CurrentEmployee class, the following error appeared:
"If an EntitySet mapping includes a function binding,
function bindings must be included for all types. The following
types do not have function bindings:
NorthwindEFModel.PreviousEmployee."
This error means that if we assigned functions to insert,
update, and delete operations for one hierarchy class, we
should do the same for all non-abstract classes in hierarchy.
So we will create the stub function and assign it to
insert, update, and delete the operations of the
PreviousEmployee class.
- Right-click the Stored Procedures node in the Project Explorer
and choose Add Procedure from the popup menu
- Enter "NullFunction" into the Name box
and click OK.

- Right-click the PreviousEmployee class and choose Configure
behavior from the popup menu.
- Choose Insert in the Behavior drop-down list.
- Choose the NullFunction stored procedure in the Stored
Procedure drop-down list.
- Repeat the steps 4-5 for Update and Delete operations and click OK.
Generating code
The model is complete now. Let's generate code for it.
Perform the following actions to generate the code.
- Double-click the NorthwindEFModel node in the Project Explorer.
- In the appeared Project Properties dialog box switch to the Generation tab.

- Specify the template for the code generation and folder to place generated files to.
-
Click the Generate Code for Context Model button on the Project toolbar.
A Generation Progress dialog appears, displaying the progress of the code
generation.
Code is generated to the specified folder in a file with the same name as
Entity Container. To add it to your Visual Studio project perform the
following actions. In the Solution Explorer right-click the project, point
to Add and click Existing Item. Then select the generated file in the
Add Existing Item dialog.