Understanding Entity Data Models

The best way to understand Entity Data Models (from here on just EDMs for brevity) is to create one. First, create a new Visual Basic project for the Console and name it EntityFramework. The next steps require the Northwind database that you installed in Chapter 25, “LINQ to SQL.” Right-click on the project name in Solution Explorer and select Add New Item. When the Add New Item dialog appears, move to the Data Node, select the ADO.NET Entity Data Model item template, and name it Northwind.edmx, as shown in Figure 27.1.

Figure 27.1 Adding a new Entity Data Model.

image

When you click Add, the Entity Data Model Wizard starts. In the first screen you need to specify the source for the EDM. With EF 4 you can create EDMs starting from an existing database or modeling custom EDMs from scratch. Select the existing database options, as shown in Figure 27.2.

Figure 27.2 Creating an EDM from an existing database.

image

The next screen is important because it requires the specification of the database. You can click New Connection or select one of the favorite connections from the appropriate combo box. Figure 27.3 shows how on my machine the connection points to Northwind as it is available on SQL Server.

Figure 27.3 Choosing the database and connection settings.

image

Notice how the connection string is represented in the dialog box. Also notice that this is not the usual connection string, because it contains metadata information that will be clearer when the EDMs’ infrastructure is explained. You decide whether to save the string in the configuration file. The next step is crucial, because you have to select what database objects you want to be mapped into the EDM. Figure 27.4 shows the dialog box.

Figure 27.4 Selecting objects for the new EDM.

image

Foreign Key Columns Support

The ADO.NET Entity Framework 4 supports mapping foreign keys from the database into the model. This is the reason why you find a new checkbox in the Entity Data Model wizard, as shown in Figure 27.4. Simply select the checkbox in order to add the foreign key’s support.

Also notice how you are required to specify a model namespace. This is important because the namespace stores Visual Basic definitions for objects that are mapped to database objects, which are explained later. You can write your own or leave the default identifier unchanged. At the moment, just choose the Categories and Products tables and then click Finish. After a few moments, when Visual Studio generates the code for the object model, the EDM Designer opens as shown in Figure 27.5.

Figure 27.5 The EDM Designer for Visual Studio.

image

The object model available in the Visual Studio designer is, behind the scenes, defined by a new Xml document that is the schema for the Entity Data Model. This Xml file is the one with the .edmx extension, in our case Northwind.edmx. The schema is divided into three sections that are summarized in Table 27.1.

Table 27.1 Sections of the Entity Data Model

image

Much work is accomplished behind the scenes by a command-line tool named EdmGen.exe that is invoked by Visual Studio. To understand how an EDM is composed, in Solution Explorer right-click the Northwind.edmx file; select Open With, and when the Open With dialog appears, double-click the Xml editor option. At this point Visual Studio shows the content of the EDM as an Xml file instead of the designer. The file contains three sections as described in Table 27.1. Listing 27.1 shows the CDSL definition (which is actually the second section in the XML file).

Listing 27.1 Conceptual Schema Definition Language

image

image

You can notice how entities (EntityType) are defined along with scalar properties (Property), relationships (Association), entity sets (EntitySet), and a container named NorthwindEntities. The next section that we consider is the SSDL, which is constituted by the Xml markup code shown in Listing 27.2 and which is actually the first section in the XML file.

Listing 27.2 The Store Schema Definition Language

image

image

Basically this schema is similar to the previous schema, except that it represents the database structure as you can see from type definition within Property elements. The last schema is the Mapping Definition Language that is illustrated in Listing 27.3.

Listing 27.3 Mapping Definition Language

image

image

The content of the MDL is quite simple, in that each ScalarProperty represents an entity’s property and establishes mapping between the property and the related column name in the database table.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.113.80