Create And Update An .edmx File Using Entity Framework Data Model In Visual Studio 2012

Introduction

In this article, I will explain the process of creating and updating an .edmx file using Entity Framework Data Model in Visual Studio 2012. An .edmx file contains the conceptual model as well as the storage model along with the mappings between them. The procedure in this topic describes how to generate an .edmx file that is based on an existing database and how to generate an empty .edmx file.

Pre-Requisites

  1. SQL database and tables should be created and ready for use.
  2. You will need to have Visual Studio 2012 installed to complete this process.

Create the C# Application

  1. Open Visual Studio 2012.
  2. Click File -> New -> Project.
  3. Select Windows from the left menu and Console Application
  4. Enter EDMXApplication as the name.

    EDMXApplication

  5. Select OK.

Create Entity Model

We’re going to use Entity Framework Designer that is included as a part in Visual Studio.

  1. Click on Project -> Add New Item.

    Add New Item

  2. Select Data from the left menu and then ADO.NET Entity Data Model.

    ADO.NET Entity Data Model

  3. Enter TestModel as the name and click OK.
  4. This launches the Entity Data Model Wizard.

    Entity Data Model Wizard

  5. Select "Generate from database" and click Next.
  6. Select the connection to the database you created in the first section. Enter TestModelContext as the name of the connection string and click Next.

    TestModelContext

  7. Click the checkbox next to the ‘Tables’ to import all tables and click ‘Finish’.

    tables

  8. The new model is added to your project and opened for you to view in the Entity Framework Designer. An App.config file has also been added to your project with the connection details for the database.

    model

  9. TestModel.tt file contains all your SQL table class files with all the entities.

    entities

Update the .edmx file when the Database changes

  1. In the Model Browser, right-click the .edmx file and select Update Model from Database.

    Update Model

  2. Expand the Tables, Views, and Stored Procedures nodes, and check the objects you want to add to the .edmx file.

  3. Click the Add tab.

    add

  4. Nodes for tables, views, and stored procedures are displayed. If any objects have been added to the database (or were not included in the previous storage model), you can expand the corresponding node to view the objects that are available to add to the conceptual model.

  5. Click the Refresh tab.

    Refresh

  6. Nodes for tables, views, and stored procedures that are included in the existing storage model are displayed. Any changes that have been made to these database objects will be reflected in the updated storage model. Changes to the conceptual model are made when columns are added to a table or view.

  7. Click the Delete tab.
    delete

  8. Nodes for tables, views, and stored procedures are displayed. If an object has been deleted from the database and was included in the previous storage model, you can expand the corresponding node. The objects in these nodes will be deleted from the updated model.

  9. Click Finish to update the .edmx file with the database changes.

  10. To update the table field changes in TestModel.tt file, right on TestModel.tt file and choose Run Custom Tool.

    Run Custom Tool

Summary

Thus, you have learned how to create and update an .edmx file using Entity Framework Data Model in Visual Studio 2012.

Up Next
    Ebook Download
    View all
    Learn
    View all