Tutorial: Creating a Conceptual Model using Tools (Entity Framework 4.1)

This walkthrough creates a conceptual model using the Entity Framework tools. It demonstrates how to map the inheritance using the table-per-hierarchy (TPH) strategy. In TPH, the inheritance tree is created through one table only. The TPH inheritance depends on a conditional mapping which is defined by a condition such as a discriminator database field. Entity Framework model first comes with only TablePerTypeStrategy but you can use the extension manager in Visual Studio 2010 in order to download the Entity Designer Database Generation Power Pack. It includes TablePerHierarchyStrategy and some other strategies. Go to the following link for more details: visualstudiogallery.msdn.microsoft.com/.../df3541c3-d833-4b65-b942-989e7ec74c87.

Prerequisites

To define conceptual model using Entity Designer

  1. Create a new class library project. Type SchoolModelLib for the project and SchoolModel_ModelFirstWithTools for the solution name.

  2. Remove the default source code file that was added to the project (Class1.cs or .vb).

  3. Add a new ADO.NET Entity Data Model item to the project, by doing the following:

    1. Right-click on the project and select Add, then New Item.

    2. Select Data in the Installed Templates pane, and then select ADO.NET Entity Data Model template.

    3. Type SchoolModel.edmx for the model name.

    4. Select Empty Model in the Choose Model Contents dialog box.

    5. Then click Finish.

  4. Create new entity types, map TPH inheritance and associations by doing the following:

    1. Add a new entity. Right-click an empty area of the design surface, point to Add, and then click Entity.

    2. In the New Entity dialog box, specify Person for the entity type name and PersonID for the Property name.

    3. Right-click the Person entity and point to Add, and then click Scalar Property. Specify LastName for the property name. In the Properties Window set Fixed Length to False, Max Length to 50, and Unicode to True; leave the rest of the properties with the default values. Then, add another scalar property called FirstName and specify the same property values.

    4. Add another entity. In the New Entity dialog box, specify Student for the entity type name and Person for the base type.

    5. Right-click the Student entity and add a scalar property called EnrollmentDate. In the Properties Window set Nullable to False, Type to DateTime; leave the rest of the properties with the default values.

    6. Add another entity. In the New Entity dialog box, specify Instructor for the entity type name and Person for the base type.

    7. Right-click the Instructor entity and add a scalar property called HireDate. In the Properties Window set Nullable to False, Type to DateTime; leave the rest of the properties with the default values.

    8. Add another entity. In the New Entity dialog box, specify OfficeAssignment for the entity type name and None for the base type.

    9. Creating an Association Between Instructor and OfficeAssignment. Right-click an empty area of the design surface, point to Add, and then click Association. For one End of the association, select Instructor from the Entity drop-down list, and select 1 (One) from the Multiplicity drop-down list. For the other End of the association, select OfficeAssignment from the Entity drop-down list, and select 0..1 (Zero or One) from the Multiplicity drop-down list. Click OK.

    10. Right-click the new association between Instructor and OfficeAssignment and select Properties. In the Properties window, click Referential Constraint, then click the ellipses button (…) that appears in the value column. .Select Instructor from the Principal drop-down list. Click OK.

  5. Note

    When you build the conceptual model, warnings about unmapped entities and associations may appear in the Error List. You can ignore these warnings because the Create Database Wizard will add the storage model and mapping information (see step 3).

To generate a database from a conceptual model

  1. Use Management Studio to create a new database instance. Name the database School_ModelFirstWithTools.

  2. Entity Framework model first comes with only TablePerTypeStrategy but you can use the extension manager in Visual Studio 2010 in order to download the Entity Designer Database Generation Power Pack which includes TPH strategy. Go to the following link for more details: visualstudiogallery.msdn.microsoft.com/.../df3541c3-d833-4b65-b942-989e7ec74c87.

  3. Select TPH strategy. Right-click an empty space on the Entity Designer surface and select Properties. In the Properties Window set Database Generation Workflow to Generate T-SQL Via T4 (TPH).xaml (VS)

  4. Right-click an empty space on the Entity Designer surface and select Generate Database from Model.

  5. Click the New Connection button or select an existing connection button from the drop-down list to provide a database connection.

    You must supply a database connection so that column types for the target database can be determined based on property types in your model, and so that connection string information can be added to your application. Note that supplying connection information does not initiate data definition language (DDL) generation.

  6. Click Next.

    The Create Database Wizard generates data definition language for creating a database. The generated DDL is displayed in the Summary and Settings Dialog Box.

  7. Click Finish.

    Upon completion, the Create Database Wizard does the following:

    • Generates the store schema definition language (SSDL) and mapping specification language (MSL) that correspond to the provided conceptual schema definition language (CSDL). The .edmx file is updated with the generated SSDL and MSL. Note that the wizard overwrites existing SSDL and MSL.

    • Saves the generated DDL in the location specified in the Save DDL As text box. For more information about the generated DDL, see Database Generation Rules.

      Note

      If a storage model is already defined when you run the Create Database Wizard, the generated DDL will contain a DROP TABLE statement and DROP CONSTRAINT statement for each EntitySet and each AssociationSet (respectively) that are inferred from the storage model.

    • Adds connection string information to your App.config or Web.config file.

    It is important to note that the Create Database Wizard does not execute the generated DDL. To create the database schema that corresponds to your conceptual model, you must execute the generated DDL independently (for example, execute the DDL in SQL Server Management Studio).

To test the model with Windows Forms client application

  1. Add a new Windows Forms Application project to the SchoolModel_ModelFirstWithTools solution. Type SchoolModelWinFormsTest for the project name.

  2. Add a reference to the EntityFramework dll.

  3. Add a reference to the SchoolModelLib class library project.

  4. Copy the App.config file from the SchoolModelLib class library project and paste it into this project.

    Or, add a new App.config file to the project, by doing the following:

    1. Right-click on the project and select Add, then New Item.

    2. Select Application Configuration File template.

    Open the App.config file that was added by the tools to the SchoolModelLib project; copy the connectionStrings element and paste in the App.config that you just added.

    Alternatively, you can create the connection string programmatically. This walkthrough does not demonstrate how to configure connection string in your code.

  5. You can now delete the App.config file for the SchoolModelLib project because it is never used.