Creating an Entity Data Model
There are two ways to create an entity data schema.
- By manually hand-coding XML mapping files and using them directly from a client application.
- Through the EDM Designer Prototype tool. Although this is a prototype, it can give you an idea of the types of tools Microsoft seeks to provide in the final release version. Currently, this tool has limited functionality, but it should be a much better product by release time.
This article discusses how to use the EDM Designer Prototype tool to create EDM schemas. Then, after creating the EDM schemas with the tool, I'll provide a brief look at the XML mapping files it creates.
To follow along you'll need to have Visual Studio Professional, and you'll need to download and install these three items:
The installations are straightforward, so I won't discuss them here. After you've installed the products, create a new project named EntityDataModelSample (see Figure 2
|Figure 2. Creating a New Project: By installing the ADO.NET vNext CTP, you can use the new project template wizard to create a new ADO.NET vNext project.|
|Figure 3. Adding a new Entity Data Model: By choosing the Entity Data Model item template, you also get a basic designer that allows you to create EDM schemas.||
After creating the project, add a new Entity Data Model named AdventureWorksModel
to the project using the Project->Add New Item menu as shown in Figure 3
Generate or Import the Model Contents
Note that the system saves entity data models with an .edm
extension. After creating the model file, you need to choose one of two options to begin generating the model contents. The options are:
- Generate from Database
- Import Model File
Both options are available when you right click on the model designer (see Figure 4
|Figure 4. Entity Data Model Wizard Step 1: The wizard's first step lets you specify whether you want to generate the model from an empty model or from a database.||
|Figure 5. Entity Data Model Wizard Step 2: In this step, you specify the connection string information for the database from which you want to generate the models.||
|Figure 6. Entity Data Model Wizard Step 3: In this step, you specify the tables in the database from which you want to generate the models.||
For this example, select the "Generate from database" option, and click "Next." You'll see the screen shown in Figure 5
From the screen in Figure 5, select the server name and the database to use to generate the model. When you click "Next", you'll see the screen in Figure 6, from which you select the tables in the database that you want to include in the entity data model.
In this example, select the ProductCategory, ProductSubcategory, and Product tables from the list of tables in the AdventureWorks sample database, and then click "Finish." At this point, save the entire project, then open up the Solution Explorer which should look something like Figure 7.
|Figure 7. Files Generated by the EDM Wizard: After generating the model, you'll have three XML files and a class file that provides an object representation of the EDM schema.||
|Figure 8. AdventureWorks Model: The EDM model contains all the entities and the relationships between them.||
If you now open up the file AdventureWorksModel.edm
by double clicking on it, you'll see the EDM model diagram shown in Figure 8
This should look familiarmuch like a standard database diagram, but underneath, Visual Studio uses the three XML files mentioned earlier. These files are used by the EDM->Database mapping engine to figure out the information required to execute queries. The generated XML files are:
- CSDL file. This is the entity data model that provides a specific view of the application. Right under the CSDL file, you'll see a .cs (C# source) file that provides the object representation of the entity data model so that you can work with the EDM schema as if you are working with an object model. I'll cover this feature more fully in future installments of this article series.
- SSDL file. This file holds schema information from the three database tables (selected in the EDM Wizard) from the AdventureWorks database expressed in EDM terms.
- MSL file. This file holds mapping information that describes how the various entity/association sets and tables connect with each other.
Now that you've created the mapping files, you're ready to program against the EDM schema.