共用方式為


Generating an Entity Data Model

Entity Framework applications and services are based on the Entity Data Model (EDM). This model represents application data as a set of entities and relationships that are mapped to a defined data source. An EDM consists of a conceptual model expressed as entities and relationships, a storage model that represents the schema of the logical model, and a mapping between the two. The development phase of a project usually starts with the conceptual model, and then the logical model is derived from the conceptual model.

To meet the needs of developers who want to derive an Entity Data Model from an existing database, the Entity Framework provides a set of tools that generate an EDM, validate an EDM, and create programmable classes based on the conceptual model. The EDM Generator (EdmGen.exe) command prompt utility enables you to generate a simple model with a one-to-one mapping between entities and tables in a data source. You can also use EdmGen.exe to generate the data classes that are based on entity types and to validate an EDM. EdmGen.exe is part of the Entity Framework runtime components in the .NET Framework 3.5 Service Pack 1(SP1).

Visual Studio 2008 SP1 includes an integrated set of Entity Data Model tools that generates a model that includes selected objects from a data source. You can then modify the conceptual model and mapping using the Entity Data Model Designer to achieve the conceptual model that is required by the application. For more information, see Entity Data Model Tools.

Considerations

The following considerations apply when you generate an EDM:

  • All entities are required to have keys. If the database has a table without a primary key, the EDM tools try to infer a key for the corresponding entity. In addition, the EDM tools generate a DefiningQuery element in the store schema that makes the data for this entity read-only. To make the entity data updatable, you must verify that the generated key is a valid key and then remove the DefiningQuery element.

  • A table that represents a many-to-many relationship between two tables in the database may not have an equivalent entity in the conceptual schema. When the EDM tools encounter such a table with no columns other than the two that are foreign keys, the mapping table is represented in the conceptual schema as a many-to-many association instead of an entity. The CourseInstructor association in the School model is an example of this behavior. For more information, see Generating the School Entity Data Model (Entity Framework Quickstart).

  • In this release, the Entity Data Model tools only support automatic generation of an EDM based on an existing data source. You cannot automatically generate a data source, such as a relational database, based on the conceptual model.

The School Model

The topics in this Getting Started section refer to a sample database named School. See Creating the School Sample Database (Entity Framework Quickstart) for the script that generates the School database on SQL Server.

School contains the following tables:

  • Course

  • CourseGrade

  • CourseInstructor

  • Department

  • OfficeAssignment

  • OnlineCourse

  • OnsiteCourse

  • Person

This example database highlights many of the complex modeling scenarios supported by the Entity Data Model tools. The script creates the relationships between the tables and inserts sample data so that you can run the samples to see how they work. The following shows the School EDM displayed in the ADO.NET Entity Data Model Designer:

School EDM in the Entity Designer

See Also

Concepts

Mapping a Conceptual Model to a Storage Schema
Data Modeling in the Entity Framework

Other Resources

Configuring the Entity Framework (Entity Framework Tasks)
Getting Started (Entity Framework)
Entity Data Model Tools
EDM Specifications
Schemas and Mapping Specification (Entity Framework)