MSDN Magazine - February 2008 - (Page 32) database. You can also see in Figure 2 that an entity can be mapped to a database’s view or an entity can obtain a method that will invoke a stored procedure. Entities can also be derived from other entities using inheritance in the conceptual model. These are just some of the ways in which the entity model can be designed using the EDM designer tools. To create an entity model, you start by adding a new ADO. NET Entity Data Model file to your project (see Figure 3). Once you do this, the Entity Data Model Wizard will prompt you to either generate a model from a database or begin with an empty model. Generating the model from existing database tables is a good way to start, as long as you have access to the database. Some development methodologies, such as domain driven design, advocate designing the entity domain model before you design the database. If this is your plan, you may want to create an empty model and then create your entities through the EDM visual designer. For my samples, I have built an entity model using the Northwind database as a starting point. The next screen of the wizard prompts you to enter the database connection information. Then, the wizard asks you to choose the database objects to include in the model. You can see in Figure 4 that I have selected all of the tables and stored procedures in the Northwind database, except for the ones that help with diagramming. The tables are initially mapped directly to entities and the stored procedures can be mapped to methods on the generated container. After you’ve specified the database objects that you want to include in the model, the EDM wizard generates the .edmx file that defines the model and mappings and adds the appropriate references to the project that are required by the Entity Framework. The .edmx file is an XML file that contains four major sections: information about the visual layout of the conceptual model in the designer, the conceptual model’s CSDL, the mapping layer’s MSL, and the physical model’s SSDL. All of this information is contained in a single file. The .edmx file’s designer information is used to assist Visual Studio in laying out the entity model in the designer; this is only used at design time. The CSDL, MSL, and SSDL are used at compile time to generate classes that will represent the EDM. Building an EDM with the Wizard Figure 3 Add an EDM File to Your Project Services. You can use Object Services to interact with the EDM, either by using Entity SQL or by using LINQ to Entities. Object Services allows you to take advantage of the conceptual model’s generated classes, which offer features such as strongly typed objects and persistence (see Figure 1). These data access techniques allow you to interact with the conceptual entities defined within the entity model rather than the objects of a physical store such as a relational database. The data model and associated mappings are created by using either the visual designer tool or by manually editing the XML that defines them. The Entity Framework, shown in Figure 2, provides the glue between an application and its database. The EDM is used to describe the business entities through the conceptual entity model and the Entity Framework, through the mapping specification, then translates that to the physical storage of the database’s tables, view, functions, and procedures. The application’s entity model is described using the Conceptual Schema Definition Language (CSDL). CSDL is an XML Format that defines the entities and the associations between entities that developers will interact with via an API, such as LINQ to Entities. The Entity Framework also uses the Storage Schema Definition Language (SSDL), an XML format that defines the storage schema of the relational database and the Mapping Schema Language (MSL) to translate how the entities of the CSDL map to the storage schema described by the SSDL. The CSDL is where the developer has the most influence since this is where the entities that she will most often interact with are defined. Figure 2 illustrates that some entities can be mapped directly to a single table in a database while others can be mapped to multiple tables. This entity determination is made by the development team based on the business model. The business model often operates on a single entity that exists in multiple physical tables in the Figure 4 Choose Database Objects 32 msdnmagazine Data Points Stored Procedures in the Entity Model I’ve chosen to add the stored procedures to the entity model that I created, but the stored procedures have only been added to the SSDL definition. Since a stored procedure may inter-
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.