MSDN Magazine - February 2008 - (Page 33) act with many different tables or other objects in a database, the Entity Framework does not automatically map the stored procedure to any one specific entity in the CSDL. If you want to create a method that maps to a stored procedure, you can do so by editing the XML in the .edmx file. To create a method that returns entities of a given type, I first choose a stored procedure. For this example, I will add a method called GetTenMostExpensiveProducts, which will execute the Ten Most Expensive Products stored procedure, returning results as Product objects. Since the SSDL already describes the stored procedure as a Function element, the next step is to add the method in the CSDL. This can be done by adding the FunctionImport element as a child of the EntityContainer element: Windows on EDM The Name attribute indicates the name of the method on the entity container. The EntitySet attribute indicates the EntitySet, and the ReturnType attribute refers to the EntityType that will be returned (or the collection of EntityTypes in this case). The reference to Self in this example is an alias that refers to the current namespace, which is NWModel. Note that either NWModel or Self would work here. If parameters are needed on the method, you can add them by including a tag. For example, if there was a CategoryId parameter on the stored procedure that I wanted to also in- Figure 5 EDM in the Class View clude on the method, I could add it to the method by including the following XML element inside of the FunctionImport element: Once the EDM has been designed and built, you can use various windows to examine the status of the entity model you have created. The Class View window (see Figure 5), which is not new, is helpful when you need to examine the objects that are available to be developed against. It will show, for instance, my new GetTenMostExpensiveProducts method on the NWEntities EntityContainer class, as well as all of the classes that the NWModel namespace contains. There will be a class representing each EntityType in the CSDL, plus a class for the main NWEntities model. There are also some new windows that let you look into the entity model and the associated mappings. These include the Entity Data Model Designer, the Entity mapping Details window, and the Entity Model Browser window. The Entity Model Browser shows all of the CSDL and SSDL components. These include the CSDL components EntityTypes, Associations, EntitySets, AssociationsSets, and Function Imports, as well as all of the SSDL elements. The Entity Model Designer (see Figure 6) shows a layout containing the model that you’ve generated. The entity model can be viewed and manipulated from this designer, which represents all of the conceptual model’s elements shown in a visual layout. Note that in Figure 6 there is a Product entity—this represents the Product EntityType in the CSDL. Each EntityType contains a list of properties, both scalar and navigational. But there is no parameter in my example, so I have skipped this step. At this point, the CSDL defines the method, the entity type returned by the method, and the EntitySet to which the returned entities belong, while the SSDL defines the stored procedure. Now the CSDL has to be mapped to the SSDL so that the conceptual method knows what stored procedure to execute. This mapping is done by inserting the following FunctionImportMapping into the EntityContainerMapping section of the MSL: Here, the FunctionImportMapping element uses the FunctionName attribute to refer to the fully qualified name of the Function element in the SSDL. The FunctionImportName attribute refers to the name of Figure 6 Entity Data Model Designer the FunctionImport element in the CSDL. Data Points february2008 33
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.