Back to FAQs

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

Model Overview

In this tutorial we will create a sample Entity Framework model for a blog. It will contain classes, representing blog, blog post, post author, and post category. Authors will be divided in two categories: regular authors and guest authors, and we create an inheritance hierarchy, representing them, with Author, RegularAuthor, and GuestAuthor classes.

We will create only some basic necessary properties for these classes in order to simplify our tutorial. The model will include the following classes and their properties:

Blog:
Name Type Entity Key
BlogID Int64 Entity key
BlogTitle String
Post:
Name Type Entity Key
PostID Int64 Entity key
Title String
Status String
Category:
Name Type Entity Key
CategoryID Int64 Entity key
Name String
Author:
Name Type Entity Key
AuthorID Int64 Entity key
Name String
Address String
RegularAuthor:
Name Type Entity Key
DateofFirstBlogPost Datetime
GuestAuthor:
Name Type Entity Key
OriginalBlogAddress String

There will be also the following associations in the model (with the corresponding navigation properties):

  • Blog - Post (one-to-many);
  • Blog - Author (one-to-one);
  • Category - Post (many-to-many).

Creating Model

To create a model using the Model-First approach, perform the following sequence of actions:

  1. In the Solution Explorer window, right-click the name of your project, then point Add and click New Item.
  2. Under Installed Templates, click Data.
  3. In the central column of the Add New Item window, select Devart Entity Model.
  4. Click Add.
  5. On the Create Model Wizard welcome page, select Model First and click Next.
  6. On the Model Synchronization page, select Yes and click Next.
  7. On the Model Synchronization Settings page, specify the target database server and its version and the target schema, and click Next. In our tutorial we will use SQL Server 2005.
  8. The Set up naming rules page allows you to tweak naming rules for the generated names of database objects. Specify the appropriate naming rules (if required) and click Next.
  9. On the Model properties page, specify the properties of the model:

    Use the Enter the namespace for model objects field to define the namespace where model objects will be placed in when generating code.

    Use the Enter the namespace for model objects field to specify the name of the entity container to contain all instances of entities for the Entity Data Model; you can easily change it later.

    In the Entity Framework version list specify the version of Entity Framework that will be used for your project. One of the following versions can be selected (depending on Entity Framework installed on your computer): 3.5, 4.x, 5 and 6.

  10. Click Next.
  11. The Choose Code Generation page allows you to select code generation templates you want to add to the new model. By default this page contains the most frequently used template. The properties area of this page allows you to configure the properties of the selected template. Three buttons at the top of this page allow you to add more templates from gallery, add existing templates from disk and remove templates from the list.
  12. Click Next.
  13. The final page of the wizard is displayed. This page may differ depending on the options selected in previous steps. The download 'Entity Framework' NuGet package check box is displayed, if Entity Framework 4.x or 5 is selected and the list of code generation templates contains the DbContext template, or if Entity Framework 6 is selected (irrespective of the selected templates). Select this check box if necessary and click Finish.

After this, an empty model is created, and we can proceed to creating model classes.

Creating Classes

  1. To create a class, click the empty diagram area and press Insert.
  2. Click the class and press F2.
  3. Enter “Blog” (without quotes) as the class name and press Enter.
  4. Right-click the created class, point to Add, and then click New Property.
  5. In the Name box type "BlogID" (without quotes).
  6. In the Type list select Int64.
  7. Ensure that the Entity Key check box is selected.
  8. From the Store Generated list, select Identity.
  9. Click OK.
  10. Repeat 4 – 6 for the BlogTitle property (enter "BlogTitle" instead of "BlogID" and select String instead of Int64). Note that steps 7 and 8 are necessary only for an entity key property.
  11. Click OK.

Now the Blog class is created. Create other classes with their properties in the similar manner. Note that the RegularAuthor and GuestAuthor classes must not have an entity key because they are child classes of the Author class. Their properties must also be nullable. Thus, don't forget to clear the Entity Key check box and select the Nullable check box when creating their properties. Don't worry about errors in the model caused by missing entity keys – they disappear after we add the necessary inheritance relationships to the model.

Creating Inheritances

  1. On the model diagram, click the Author class and then the RegularAuthor class while holding the Ctrl button.
  2. Right-click one of the selected classes, point to Add and then click New Inheritance.
  3. Make sure that the following values are selected:
    • In the Base Class list: Author.
    • In the Derived Class list: RegularAuthor.
    • In the Type list: Table Per Hierarchy.
  4. Click New to the right of the Storage Column list.
  5. In the Storage Column Editor select bit in the Type list.
  6. Click OK.
  7. Set Derived Class Condition to 1.
  8. Click OK.

Repeat the steps for the GuestAuthor class, setting Derived Class Condition equal to 0. Now our inheritance hierarchy is created.

Suppose we want to divide all the authors into regular authors or guest authors. For this purpose we will make the Author class abstract. For this, double-click the Author class and in the Inheritance Modifier list select Abstract, then click OK.

Creating Associations

  1. On the model diagram, right-click the Blog class, point to Add, and then click New Association .
  2. From the Relation Class list in the End 1 area select Blog, from the Multiplicity list select 1 (One), and leave other settings set to their default values.
  3. From the Relation Class list in the End 2 area select Post, from the Multiplicity list select * (Many), and leave other settings set to their default values.
  4. Click OK.

Repeat these steps, selecting the necessary classes and multiplicity to create a many-to-many association between Post and Category classes and one-to-one association between Blog and Authorclasses. After this our model is ready.

Save the model, and Entity Developer generates the code for it automatically, using the previously selected template.

Creating Database Objects

With Entity Developer creating database objects for our model can be done in few easy steps. First, we need to specify the connection settings. For this, in the Database Explorer window right-click Database Connection and then click Edit Conneсtion Properties. In the opened Connection properties dialog box specify the connection settings and click OK.

After the model connection is specified, perform the following steps to create database objects:

  1. On the Model Explorer window toolbar click the Update Database From Model button.
  2. Click Next.
  3. On the Choose schemas page select the schema to update. In our sample we update the dbo schema. Click Next.
  4. On the Choose change actions page, select the appropriate action(s). You can use the context menu of the root nodes to select or clear all added, dropped, or changed objects. Click Execute.

After this, Entity Developer creates the necessary database tables.