Creating Entity Framework Model with Entity Developer using Model-First Approach
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:
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).
To create a model using the Model-First approach, perform the following sequence of actions:
- In the Solution Explorer window, right-click the name of your project, then point Add and click New Item.
- Under Installed Templates, click Data.
- In the central column of the Add New Item window, select Devart Entity Model.
- Click Add.
- On the Create Model Wizard welcome page, select Model First and click Next.
- On the Model Synchronization page, select Yes and click Next.
- 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.
- 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.
- 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.
- Click Next.
- 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.
- Click Next.
- 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.
- To create a class, click the empty diagram area and press Insert.
- Click the class and press F2.
- Enter “Blog” (without quotes) as the class name and press Enter.
- Right-click the created class, point to Add, and then click New Property.
- In the Name box type "BlogID" (without quotes).
- In the Type list select Int64.
- Ensure that the Entity Key check box is selected.
- From the Store Generated list, select Identity.
- Click OK.
- 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.
- 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.
- On the model diagram, click the Author class and then the RegularAuthor class while holding the Ctrl button.
- Right-click one of the selected classes, point to Add and then click New Inheritance.
- 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.
- Click New to the right of the Storage Column list.
- In the Storage Column Editor select bit in the Type list.
- Click OK.
- Set Derived Class Condition to 1.
- 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.
- On the model diagram, right-click the Blog class, point to Add, and then click New Association .
- 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.
- 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.
- 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:
- On the Model Explorer window toolbar click the Update Database From Model button.
- Click Next.
- On the Choose schemas page select the schema to update. In our sample we update the dbo schema. Click Next.
- 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.