236 Microsoft ADO.NET 4 Step by Step If not already available in your Visual Studio Integrated Development Environment (IDE), access the Mapping Details panel by right-clicking the model’s visual design surface and selecting Mapping Details from the shortcut menu. To use the Mapping Details panel, select an entity from the visual design surface. The panel displays all column mappings already defined for the entity, with storage model properties on the left half and conceptual model properties to their right. To modify the conceptual property for a storage model property, click in the Value / Property column to the right of the storage model property name and use its drop-down list to select a different conceptual model property. Chapter 14 Visualizing Data Models 237 The Mapping Details panel lets you link properties from multiple storage layer entities (that is, from multiple tables, views, or table-emitting stored procedures from the target database) to a single conceptual model entity. With an entity still selected on the visual design surface, click the ghosted <Add A Table Or View> row in the Mapping Details panel. Select one of the available entities from the drop-down list. After the new storage layer properties appear in the panel, modify each property as needed to define the proper data relationships. By selecting an association within the visual designer surface, the Mapping Details panel will also let you modify the mapped settings for that association. Note This ability to edit the association mapping does not apply to associations tied to import- ed foreign key relationships. Another useful feature of the Mapping Details panel is the ability to define conditions for mapped entities. For example, you might want to limit the loaded orders for a customer to just those that have not yet been shipped. By adding a condition to the OrderEntry entity that looks for non-NULL values in the ShipDate property, the Entity Framework will automati- cally limit the orders managed by the model’s application. Note If a storage layer property is used as a condition, it cannot be used as a standard mapped property within the conceptual model. Adding a Mapping Condition to an Entity Note This exercise continues the previous exercise in this chapter. 1. If you haven’t yet displayed the Mapping Details panel, open it by right-clicking the Entity Data Model Designer’s design surface and choosing Mapping Details from the shortcut menu. 238 Microsoft ADO.NET 4 Step by Step 2. Select the OrderEntry entity on the visual design surface. The mapping details for that entity should appear in the Mapping Details panel. 3. On the column mapping for the ShipDate : date storage layer property, click the ShipDate : DateTime value in the Value / Property column. You’ll see a drop-down list of options. Select <Delete> from this list to clear the mapping. 4. Near the top of the Mapping Details panel, just below the Maps To OrderEntry row, click the ghosted <Add A Condition> item and select ShipDate from the drop-down list. 5. In the Operator column of the new When ShipDate row, select Is. Chapter 14 Visualizing Data Models 239 6. In the Value / Property column of the When ShipDate row, select Not Null. 7. On the visual design surface, click the ShipDate property in the OrderEntry entity. Press Delete to remove the ShipDate property. 8. Save changes to the model to generate the new object layer content. The upper-left corner of the Mapping Details panel includes two toolbar buttons. Map Entity to Tables / Views Map Entity to Functions The top button lets you update the mappings for an entity using the storage layer tables and other similar storage items. The bottom button lets you specify database-level stored proce- dures, exposed as entity functions, that manage the insert, update, or deletion of individual entities within its entity set. Use a Stored Procedure to Manage Entity Data Note This exercise continues the previous exercise in this chapter. 1. Select the OrderEntry entity on the visual design surface. 2. Click the Map Entity To Functions toolbar button (the lower button) on the Mapping Details panel. 3. Select the <Select Delete Function> row and then select CancelOrder from the drop- down list. 240 Microsoft ADO.NET 4 Step by Step 4. The Mapping Details panel detects the properties required for the selected function. In this case, a single orderID integer must be mapped to an entity value. In the Property column of the CancelOrder row, choose ID : Int64 for the parameter property. ID is the primary key for the OrderEntry entity. 5. Save changes to the model to generate the new object layer content. Using the Model Browser Visual Studio’s Model Browser panel is a hierarchical item selection panel similar to the Solution Explorer panel. When an Entity Framework visual model is active, the Model Browser displays the various components of both the conceptual and storage layers. By browsing and selecting the entities, properties, and other features of a model through this panel, you can view and modify the settings of each selected item through the standard Visual Studio Properties panel. Chapter 14 Visualizing Data Models 241 The Model Browser is an essential part of the visual model designer, because some features can be created and managed only through the browser. For example, you can add, edit, and delete individual properties from an existing complex type only by accessing that complex type through the Model Browser panel. Managing the Object Layer Visual Studio generates the source code for an entity model in your project each time you save changes to that model. The generated object layer is a set of Entity Framework-aware classes that exist as standard C# or Visual Basic source code. By default, Visual Studio generates the model source code using its “default code genera- tor.” You can view this setting within the Entity Data Model Designer by selecting the design surface of the visual model and then viewing the Code Generation Strategy property in the Properties panel. For wizard-generated models, this is set to Default. For advanced needs, Visual Studio allows you to fully manage the code generation process by adding a code generation item to your project. These standard Visual Basic or C# language files are also known as text templates and include a .tt file extension. Visual Studio provides two types of code-generation items for use with Entity Framework models: ADO.NET EntityObject Generator This is the default type, and the same type Visual Studio uses internally by default to generate the object layer for a model. ADO.NET Self-Tracking EntityObject Generator This advanced generator is useful for n-tier projects in which the code that modifies EF-managed data exists in a differ- ent layer from the code that manages the structural interactions between entities and model layers. To add a custom code generator to your project, use Visual Studio’s Project | Add New Item menu command, and choose either ADO.NET EntityObject Generator or ADO.NET Self- Tracking EntityObject Generator as the new item type. You can also add these items by right- clicking the visual modeler design surface and selecting Add Code Generation Item from the shortcut menu. 242 Microsoft ADO.NET 4 Step by Step When you add a new code generation item to your project, Visual Studio makes two Entity Framework-related changes: 1. The Code Generation Strategy property for your model changes from Default to None. 2. Within the new .tt file, a reference to the CSDL portion of your model appears near the top of the code generation source code. The modification of text templates is beyond the scope of this book. For details on the content of code generation items and how to modify them, search for “Generated Code Overview” within the Visual Studio online help. Summary This chapter continued the overview of the Entity Framework by introducing the ADO.NET Entity Data Model Designer and its associated database import wizard. These tools simplify one of the most common tasks performed when developing applications that use the Entity Framework: importing existing database logical objects into a new Framework model. Now that you have an accessible model, you can write code that queries the data managed by that model. The next two chapters introduce two common methods of querying EF data: retrieving entity data using Entity SQL and accessing objects directly through the Entity Framework’s Object Services layer. Chapter 14 Visualizing Data Models 243 Chapter 14 Quick Reference To Do This Add a new Entity Framework model to a project Open or create a Visual Studio project. Select Project | Add New Item. Select ADO.NET Entity Data Model as the new item type then click Add. When the wizard appears, select either Generate From Database or Empty Model. Complete the wizard steps and modify the model as needed. Use a stored procedure to update database-side content from entity changes Include the stored procedure in the model, either through the initial wizard import or by using the Designer’s Add | Function Import shortcut command. Select the entity in the visual designer. Open the Mapping Details panel. Click the Map Entities To Functions toolbar button (the lower button) within the panel. In the panel’s list of functions, click the <Select Update Function> row then select the stored procedure. If needed, assign entity properties to the stored proce- dure’s parameters. Create a new complex type for later use in an entity Open the Model Browser panel. Expand the conceptual model portion of the browser tree. Right-click the Complex Types branch then select Create Complex Type from the shortcut menu. Rename the new complex type. Right-click the complex type to add new properties using the various Add | Property shortcut menus. Refresh the model after making database-level structural changes Right-click the model’s design surface then select Update Model From Database from the shortcut menu. 245 Chapter 15 Querying Data in the Framework After completing this chapter, you will be able to: Describe the Entity SQL language and its purpose Create basic queries using Entity SQL Use the Entity Provider to access data based in an entity model The Entity Framework (EF) is a markedly different way of interacting with data traditionally found in relational databases and similar external data stores. With its focus on coercing everything into .NET objects, it brings a familiarity to the data management process. But for those who are used to retrieving data through databases such as SQL Server and their SQL- based data query languages, moving to an object-centric paradigm doesn’t necessarily feel like an improvement. Fortunately, the Entity Framework includes a tool that helps bridge the data query gap between SQL-based systems and Framework model-based objects: Entity SQL. This query language has all the flavor and feeling of SQL, but it runs its queries against the entities and properties of an Entity Data Model (EDM). This chapter provides a brief overview of Entity SQL, including examples of how to use it in your code. Visual Studio’s online help includes full documentation on the language. If you are already comfortable with SQL, it should take you no time at all to retrieve model data us- ing this new yet familiar language. Note This chapter assumes you have some familiarity with the SQL language, especially as ex- pressed in SQL Server’s Transact-SQL (T-SQL) language. The exercises in this chapter all use the same sample project, a tool that queries Entity Framework data using Entity SQL. Although you will be able to run the application after each exercise, the expected results for the full application might not appear until you complete all exercises in the chapter. . Details panel. 3. Select the <Select Delete Function> row and then select CancelOrder from the drop- down list. 240 Microsoft ADO. NET 4 Step by Step 4. The Mapping Details panel detects the properties. Mapping Details panel, open it by right-clicking the Entity Data Model Designer’s design surface and choosing Mapping Details from the shortcut menu. 238 Microsoft ADO. NET 4 Step by Step 2 236 Microsoft ADO. NET 4 Step by Step If not already available in your Visual Studio Integrated Development Environment (IDE), access the Mapping Details panel by right-clicking the