共用方式為


How to: Create a New Entity Data Model

This topic describes how to create a new Entity Data Model (EDM) by using the Entity Data Model Wizard. The procedures in this topic describe how to generate a model that is based on an existing database (this is recommended for this version of the ADO.NET Entity Data Model Designer), and how to generate an empty model.

Note

You typically generate an empty model when you want to model the data for an application before you design the storage schema. Modeling data first requires deploying the application with a data source that fits the model.

An EDM can be included in the following Visual Studio project categories:

  • Application projects

  • Class libraries

  • Web projects

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

To create an EDM from an existing database

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

  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 model (<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.

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

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

    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.

  10. Click Finish to create the EDM.

    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 EDM 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 ADO.NET Entity Data Model Designer Scenarios.

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

To create an empty model

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

  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 model (<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 add to your model by modifying this file, see Entity Data Model, EDM Specifications, and Schemas and Mapping Specification (Entity Framework).

    • 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 an Entity Data Model

Other Resources

Entity Data Model Tools Tasks