Back to FAQs

Creating Entity Framework Model with Entity Developer using Database-First Approach

In this tutorial we create a model based on the NorthwindEF database from the Entity Framework Query Samples project.

Generating Model

After you have created the required database, run Entity Developer and import the database to the model. To do it, perform the following steps.

  1. Select New Model from the File menu.
        

    The New Project page is displayed.

  2. Select Entity Model, specify its Name and click Create.

    Create Model Wizard is displayed.

  3. Click Next.

    The Set up data connection properties page is displayed.

  4. Select a database provider in the Provider list and set the required connection parameters, then click Next.

    The Choose Model Contents page is displayed.

  5. Select Generate From Database and click Next.

    The Select Database Objects page is displayed.

  6. Select the check box of the Tables node and check boxes of the EmployeesInsert, EmployeesUpdate, and EmployeesDelete procedures, then click Next.

    The Set up naming rules page is displayed.

  7. Select Unchanged in both Case lists under Class and Method Names and under Class Properties' Names and click Next.

    The Model properties page is displayed.

  8. Enter the model name and namespace (for example, NorthwindEFModel) to place the model in. After this click Next.

    The Choose Model Diagram Contents page is displayed.

  9. Select the entities to be displayed on the diagram and click Next. In our tutorial we select All Entities.

    The Choose Code Generation Template page is displayed.

  10. Keep the default code generation template, just click Next.

    The final page of Create Model Wizard is displayed.

  11. Click the Finish button.

Now the model is generated:

Note that Entity Developer automatically detected and generated a many-to-many association between the Employee and Territory classes, based on the EmployeesTerritories table.

Creating Complex Type

Several generated classes have a common subset of properties, containing address information. We will create a complex type AddressData from them. With powerful Model Refactoring feature in Entity Developer (Devart's ORM designer) it can be done in a few simple steps:

  1. Select the entities, having the properties that store address information: Supplier, Employee, PreviousEmployee, and Customer (though the Order entity also has a similar set of properties, we will edit it later, because its properties have different names). For this, click these classes while holding the Ctrl key.
  2. Right-click one of the selected classes, and then click Model Refactoring.
  3. In the Model Refactoring Wizard enter AddressData in the New complex type box, click Next, and then click Finish.

    The complex type is created and added to the diagram.

  4. Click the ShipAddress property in the Order class, then press Shift and click the ShipCountry property holding down the Shift key.
  5. Drag the selected properties out of the Order class and drop them onto the empty diagram space.
  6. In the Model Refactoring dialog box click Existing complex type.
  7. In the corresponding list select AddressData.

Creating 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 following steps:

  1. Double-click the Employee class

    The Class Editor dialog box is displayed.

  2. In the editor, select Abstract in the Inheritance Modifier list and click OK.
  3. Click the PreviousEmployee class on the diagram and press the Delete key.
  4. Click Yes in the opened dialog box, where you are asked whether to delete the class from the model.
  5. Click No in another opened dialog box, where you are asked whether to delete the corresponding storage objects.
  6. Click the New Class button on the Model toolbar or press ALT+C.
  7. Enter "PreviousEmployee" in the Name box of the opened Class Editor dialog box and press Enter.
  8. Click the Employee class and then PreviousEmployee class while holding down the SHIFT key.
  9. Click the New Inheritance button on the Model toolbar or press ALT+I
  10. Select Table Per Concrete Type in the Type list and click OK.
  11. Click the New Class to Context button on the Entity Framework Model toolbar or press ALT+C.
  12. Double-click the displayed class.
  13. Enter "CurrentEmployee" in the Name box of the opened Class Editor dialog box and press Enter.
  14. Click the Employee class and then the CurrentEmployee class while holding down the SHIFT key.
  15. Click the New Inheritance button on the Model toolbar or press Alt+I.
  16. Select Table Per Concrete Type in the Type list and click OK.
  17. Right-click the Employee class and select Mapping Details from the shortcut menu.
  18. In the displayed Mapping Details dialog box, select <delete> from the drop-down list in the first row of the Storage Entities grid and click OK.
  19. Right-click the CurrentEmployee class and select Mapping Details from the shortcut menu.
  20. In the displayed Mapping Details dialog box, select Employees from the drop-down list in the first row of the Storage Entities grid and click OK.
  21. Right-click the PreviousEmployee class and select Mapping Details from the shortcut menu.
  22. In the displayed Mapping Details dialog box, select PreviousEmployees from the drop-down list in the first row of the Storage Entities grid and click OK.

Table Per Type hierarchy

In the Table Per Type hierarchy, each hierarchy class is mapped to one table. The Order class is mapped to the Order table, the InternationalOrder class is mapped to the InternationalOrder table. To create the Table Per Type hierarchy, perform the following steps:

  1. Click the New Inheritance button on the Model toolbar or press ALT+I.
  2. In the editor, select Order in the Base Class drop-down list and InternationalOrder in the Derived Class drop-down list.
  3. Select and click OK.
  4. Select Table Per Type in the Type drop-down list and click OK
  5. Remove the association between the Order and InternationalOrder classes. This association becomes redundant after the inheritance releationship is defined.
  6. Drop the OrderId field from the InternationalOrder class.
  7. Right-click the InternationalOrder class and select Mapping Details from the shortcut menu.
  8. Map the OrderId column to the corresponding property.

Table Per Hierarchy hierarchy

In the Table Per Hierarchy hierarchy, all hierarchy classes are mapped to the same table. To create the Table Per Hierarchy hierarchy, perform the following steps:

  1. Click the Discontinued property in the Product class and press the Delete key.
  2. Drag the DiscontinuedDate property out of the Product class and drop it onto the empty diagram space.
  3. In the displayed Choose Action dialog box, click New derived class.
  4. Enter DiscontinuedProduct in the New derived class box and click OK.
  5. In the opened Inheritance Editor dialog box select Discontinued in the Storage column list.
  6. Enter "false" (without quotes) into the value box near Base class condition.
  7. Enter "true" (without quotes) into the value box near Derived class condition.
  8. Click OK.

Configuring Stored Procedure Mapping

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:

  1. Right-click the CurrentEmployee class and then click Stored Procedure Mapping.
  2. Select Insert in the Commands list.
  3. Select the EmployeesInsert stored procedure in the Stored Procedure list.
  4. 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.
  5. Select Update in the Commands list.
  6. Repeat steps 3 - 4 with the EmployeesUpdate procedure.
  7. Select Delete in the Commands list.
  8. Select the EmployeesDelete stored procedure in the Stored Procedure list.
  9. Assign the Employeeid property to the only parameter in the grid below by selecting the corresponding property from the list in the Property column.
  10. Click OK.

Now look at the Error List window. It displays error messages and warnings of your model. After we configured the behavior of the CurrentEmployee class, the following error is displayed: "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 the hierarchy.

So we will create the stub function and assign it to insert, update, and delete the operations of the PreviousEmployee class.

  1. Right-click the Stored Procedures node in the Model Explorer (in the storage part of the model), point to Add, and then click New Procedure.
  2. Enter "NullFunction" into the Name box and click OK.
  3. Right-click the PreviousEmployee class and then click Stored Procedure Mapping.
  4. Select Insert in the Commands list.
  5. Select the NullFunction stored procedure in the Stored Procedure list.
  6. Clear the Hide mapped check box.
  7. Repeat steps 4-5 for the Update and Delete operations and click OK.

Testing Model

Entity Developer allows you to test the model without compiling the whole project. You can preview entity data and execute LINQ to Entites and Entity SQL queries.

To preview entity data, right-click an entity and then click Retrieve Data. For example, let's retrieve data from the Supplier entity:

Note that you can see the data of related entities and complex types and the generated SQL.

To execute a query, click the Create New Query button on the Entity Developer Standard toolbar or press Ctrl+Q. Paste, for example the following query from the EntityFrameworkQuerySamples solution to a query editor:

Employees.OfType<currentemployee>().ToList().Select(p => new { type = p.GetType().ToString(), p })

Query language can be selected in the list on the Query toolbar. Click the Run Query button on the Query toolbar or press F5 to run the query:

Generating Code

Code generation can be tweaked in template properties. By default a single cs r vbfile is generated. However, all templates have the FilePerClass option, that enables generating each class in a separate file, as well as the EntitiesOutput and ContextOutput. EntitiesOutput and ContextOutput provide a capability that slightly differs in Visual Studio and in the stand-alone version.

If you are using Entity Developer as a Microsoft Visual Studio add-in, you can define the project and project folder to store generated code for contexts and entities of your model. In the Entity Developer stand-alone application, it is possible to define folders, in which generated code for entities and contexts is to be stored.

To generate code, switch back to the model and click the Generate Code button on the Model toolbar. After this, you may add thegenerated code files to your projects.

Visit next article to learn Creating Entity Framework Model with Entity Developer using Model-First Approach