Share via


Generating Models and Mappings

Entity Framework applications and services are based on model and mapping information that is expressed in three XML-based languages:

CSDL represents application data as a set of entities and relationships in a conceptual model, and it is an implementation of Entity Data Model. A storage model is expressed in SSDL and represents the schema of the data store. The mapping between the two models is expressed in MSL.

The CSDL, SSDL, and MSL content for an application can be automatically generated by the ADO.NET Entity Data Model Tools. The Entity Data Model Wizard generates model and mapping information as well as data classes from an existing database. The Entity Data Model Designer (Entity Designer) can then be used to graphically modify model and mapping information. Or, you can graphically create a conceptual model with the Entity Designer, and then use the Generate Database Wizard to automatically generate the storage model, mappings, data classes, and a database that supports the conceptual model. In either scenario, you can use the Update Model Wizard to update model and mapping information when the underlying database changes.

The EDM Generator (EdmGen.exe) is a command-line tool that enables you to generate a simple conceptual model with a one-to-one mapping between entities and tables in a data source. You can also use EdmGen.exe to validate a conceptual model and to generate the data classes that are based on entity types in the model. EdmGen.exe is part of the Entity Framework runtime components that are included in the .NET Framework starting with version 3.5 Service Pack 1 (SP1).

Considerations

The following considerations apply when you use the Entity Data Model Tools to generate a conceptual model from an existing database: 

  • All entities are required to have keys. If the database has a table without a primary key, the Entity Data Model tools try to infer a key for the corresponding entity. In addition, the Entity Data Model 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 contains only foreign keys (sometimes called a pure join table) and represents a many-to-many relationship between two tables in the database will not have a corresponding entity in the conceptual model. When the Entity Data Model tools encounter such a table, the table is represented in the conceptual model 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 .edmx File

The School Model

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

School contains the following tables:

  • Course

  • StudentGrade

  • 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 model 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 Model

Other Resources

Configuring the Entity Framework
Getting Started (Entity Framework)
Entity Data Model Tools
CSDL, SSDL, and MSL Specifications