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
- SQL database and tables should be created and ready for use.
- You will need to have Visual Studio 2012 installed to complete this process.
Create the C# Application
- Open Visual Studio 2012.
- Click File -> New -> Project.
- Select Windows from the left menu and Console Application
- Enter EDMXApplication as the name.
- Select OK.
Create Entity Model
We’re going to use Entity Framework Designer that is included as a part in Visual Studio.
- Click on Project -> Add New Item.
- Select Data from the left menu and then ADO.NET Entity Data Model.
- Enter TestModel as the name and click OK.
- This launches the Entity Data Model Wizard.
- Select "Generate from database" and click Next.
- Select the connection to the database you created in the first section. Enter TestModelContext as the name of the connection string and click Next.
- Click the checkbox next to the ‘Tables’ to import all tables and click ‘Finish’.
- 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.
- TestModel.tt file contains all your SQL table class files with all the entities.
Update the .edmx file when the Database changes
- In the Model Browser, right-click the .edmx file and select Update Model from Database.
- Expand the Tables, Views, and Stored Procedures nodes, and check the objects you want to add to the .edmx file.
- Click the Add tab.
- 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.
- Click the Refresh tab.
- 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.
- Click the Delete tab.
- 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.
- Click Finish to update the .edmx file with the database changes.
- To update the table field changes in TestModel.tt file, right on TestModel.tt file and choose 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.