How to: Create a New .edmx File

[This page is specific to the latest version of the Entity Framework. The latest version is available as the 'Entity Framework' NuGet package. For more information, see Entity Framework Releases and Versioning.]

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.

noteNote:
After generating an empty .edmx file, you can use the 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.

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.

    noteNote:
    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 Connections Dialog Box.

  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.

    noteNote:
    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.

  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.

    noteNote:
    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 Entity Designer.

Important Note:
Starting with Entity Framework 4.1, we recommend that you use the DbContext code generator. For more information, see the last section in this topic.

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.

    noteNote:
    The generated .edmx file has no storage, conceptual, or mapping information. For information about generating a database from a conceptual model, see How to: Generate a Database from a Conceptual Model.

    • 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.

    noteNote:
    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.

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

    • Starts the Entity Designer, which provides a design surface for editing your model. For information about how to build your model with the Entity Designer, see Modeling and Mapping with Entity Framework Designer.

Important Note:
Starting with Entity Framework 4.1, we recommend that you use the DbContext code generator. For more information, see the last section in this topic.

To use the DbContext Generator Template to Generate Object Layer Code

When generating the conceptual model by using the Entity Framework tools, the object layer code is automatically generated. By default, the generated object layer code contains the following: definitions for persistence aware entity types that derive from EntityObject , and a context type that derives from ObjectContext and contains ObjectSet<TEntity> properties for each type in the model.

In this section you will replace the default code with code that contains: persistence ignorant entity types, also known as "plain-old" CLR objects (POCO), and a DbContext derived context type that contains DbSet<TEntity> properties. DbContext and DbSet<TEntity> wrap ObjectContext and ObjectSet<TEntity> types and provide a simplified API for carrying out the most common tasks. Starting with Entity Framework 4.1, we recommend that you use the DbContext API.

  1. Open the .edmx file in the Entity Designer.

  2. Right-click an empty area on the Entity Designer surface and point to Add Code Generation Item .

    In the Add New Item dialog, select Online Templates and type DBContext in the Search Online Templates text box.

    Select the appropriate version for your template (5.0, if you want to target the Entity Framework 5.0).

    Click OK .

    The wizard performs the following actions:

    • Disables the default code generation for the .edmx file and sets the CodeGenerationStrategy property to None .

    • Creates the ModelName.Context.tt and ModelName.tt project folders. Under the ModelName.Context.tt folder, there is a file that defines a context type that derives from DbContext . Under the ModelName.tt folder, there are files that define POCO entity types.

    If you are working with the Visual Basic project, you might need to click Show All Files in Solution Explorer to see all files in the project.

See Also




Build Date:

2013-06-19
Show: