Udostępnij za pośrednictwem


How to: Create a New .edmx File (Entity Data Model Tools)

This topic describes how to create a new .edmx file by using the Entity Data Model Wizard. An .edmx file contains the the conceptual model, as well as a storage model and the mappings between them. The procedures in this topic describe how to generate an .edmx file that is based on an existing database and how to generate an empty .edmx file.

Note

After generating an empty .edmx file, you can use the ADO.NET Entity Data Model Designer (Entity Designer) to create entities and relationships. You can then use the Create Database Wizard to generate a database from the .edmx file. For more information, see How to: Generate a Database from a Conceptual Model (Entity Data Model Tools).

For more information about .edmx files, see .edmx File Overview (Entity Framework). An .edmx file can be included in the following Visual Studio project categories:

  • Application projects

  • Class libraries

  • Web projects

Multiple .edmx files can be added to the same project, and each file can target a different database, different tables in the same database, or both.

To create an .edmx file from an existing database

  1. Open or create the project for which you want to create an .edmx file.

  2. Right-click the project name in Solution Explorer, point to Add, and then click New Item.

  3. Select ADO.NET Entity Data Model in the Templates pane.

  4. Enter the name for the file (<project name>.edmx), and then click Add.

    The first page of the Entity Data Model Wizard appears.

  5. Select Generate from database in the Choose Model Contents dialog box, and then click Next.

  6. Click the New Connection button.

    The Connection Properties dialog box appears.

  7. Enter the server name, select the authentication method, and enter the name of the database for which the model is being created. Click OK.

    The Choose Your Data Connections dialog box is updated with the database connection settings.

    Note

    By default, entity connection settings are saved in the App.config or Web.config file (depending on the project type). Type a name for the connection or use the provided default name. Clear the Save entity connection settings check box if you do not want connection information saved in a configuration file. For more information about saving connection settings, see Choose Your Data Connection Dialog Box (Entity Data Model Wizard).

  8. Click Next to continue.

    The Choose Your Database Objects dialog box appears. By default, no objects in the database are selected for inclusion in the .edmx file.

  9. Expand the nodes for Tables, Views, and Stored Procedures. Cancel the selection of any tables, views, and stored procedures that you do not want included in the .edmx file.

    Note

    Classes generated from the conceptual model will be in the Model Namespace. Type a name for the namespace or use the provided default name. For more information, see Generated Code Overview (Entity Data Model Designer).

  10. Click Finish to create the .edmx file.

    The Entity Data Model Wizard does the following:

    • Adds references to the System.Data, System.Data.Entity, System.Core, System.Security, and System.Runtime.Serialization assemblies to the project, if they do not already exist.

    • Generates an .edmx file that encapsulates the storage model, the conceptual model, and mappings.

    • Creates a source code file that contains classes generated from the conceptual model. You can view the source code file by expanding the .edmx node in Solution Explorer. To see the source code file in Visual Basic projects, you must click the Show All Files button in Solution Explorer before you expand the .edmx node.

    Note

    The source code file is generated from the .edmx file. Any manual changes to the source code file are overwritten when the corresponding .edmx file changes. To prevent custom code from being overwritten, you can write partial classes that are stored in separate files. For more information, see Customizing Objects (Entity Framework).

    • Creates an App.Config or Web.Config file (depending on the project type).

    • For ASP.NET Website projects, defines a build provider in the Web.config file.

    • Displays the conceptual model in the ADO.NET Entity Data Model Designer (Entity Designer). For information about how to modify your model with the Entity Designer, see Entity Data Model Tools Tasks and Entity Data Model Tools Scenarios.

For an example of creating an actual model from an existing database, see Quickstart (Entity Framework).

To create an empty .edmx file

  1. Open or create the project for which you want to create an .edmx file.

  2. Right-click the project name in Solution Explorer, point to Add, and then click New Item.

  3. Select ADO.NET Entity Data Model in the Templates pane.

  4. Enter the name for the file (<project name>.edmx), and then click Add.

    The first page of the Entity Data Model Wizard appears.

  5. Select Empty Model in the Choose Model Contents dialog box, and then click Finish.

    The Entity Data Model Wizard does the following:

    • Adds references to the System.Data, System.Data.Entity, System.Core, System.Security, and System.Runtime.Serialization assemblies to the project, if they do not already exist.

    • Generates an .edmx file to encapsulate the storage model, the conceptual model, and mappings.

    Note

    The generated .edmx file has no storage, conceptual, or mapping information. For information about how to manually modify this file, see CSDL, SSDL, and MSL Specifications. For information about generating a database from a conceptual model, see Generate Database Wizard (Entity Data Model Tools) and How to: Generate a Database from a Conceptual Model (Entity Data Model Tools).

    • Creates an empty source code file to contain classes that are generated from the conceptual model. You can view the source code file by expanding the .edmx node in Solution Explorer. To see the source code file in Visual Basic projects, you must click the Show All Files button in Solution Explorer before you expand the .edmx node.

    Note

    The source code file is generated from the .edmx file. Any manual changes to the source code file are overwritten when the corresponding .edmx file changes.

See Also

Concepts

Creating an Application Using a Conceptual Model (Entity Data Model Tools)

Other Resources

Entity Data Model Tools Tasks