Download from Library of Wow! eBook
Download from Library of Wow! eBook
SECOND EDITION
Programming Entity Framework
Julia Lerman
Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo
Download from Library of Wow! eBook
Programming Entity Framework, Second Edition by Julia Lerman Copyright © 2010 Julia Lerman. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or
[email protected].
Editors: Mike Hendrickson and Laurel Ruma Production Editor: Loranah Dimant Copyeditor: Audrey Doyle Proofreader: Sada Preisch
Indexer: Ellen Troutman Zaig Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Robert Romano
Printing History: February 2009: August 2010:
First Edition. Second Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Programming Entity Framework, the image of a Seychelles blue pigeon, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. .NET is a registered trademark of Microsoft Corporation. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
ISBN: 978-0-596-80726-9 [SB] 1281106344
Download from Library of Wow! eBook
Table of Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii 1. Introducing the ADO.NET Entity Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 The Entity Relationship Model: Programming Against a Model, Not the Database The Entity Data Model: A Client-Side Data Model Entities: Blueprints for Business Classes The Backend Database: Your Choice Database Providers Access and ODBC Entity Framework Features: APIs and Tools Metadata Entity Data Model Design Tools Object Services POCO Support Change Tracking Relationship Management and Foreign Keys Data Binding n-Tier Development EntityClient The Entity Framework and WCF Services What About ADO.NET DataSets and LINQ to SQL? DataSets LINQ to SQL Entity Framework Pain Points Are Fading Away Programming the Entity Framework
2 3 6 7 8 9 9 10 10 11 12 12 13 13 14 14 15 15 15 16 16 17
2. Exploring the Entity Data Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Why Use an Entity Data Model?
19
iii
Download from Library of Wow! eBook
The EDM Within the Entity Framework Walkthrough: Building Your First EDM Inspecting the EDM in the Designer Window Entity Container Properties Entity Properties Entity Property Properties The Model’s Supporting Metadata Viewing the Model in the Model Browser Viewing the Model’s Raw XML CSDL: The Conceptual Schema EntityContainer EntitySet EntityType Associations Navigation Property Navigation Properties That Return Collections SSDL: The Store Schema MSL: The Mappings Database Views in the EDM Summary
20 21 24 26 26 27 29 31 31 33 34 35 36 38 41 42 43 45 46 47
3. Querying Entity Data Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Query the Model, Not the Database Your First EDM Query Where Did the Context and Classes Come From? Querying with LINQ to Entities Writing Your First LINQ to Entities Query Querying with Object Services and Entity SQL Why Another Way to Query? Entity SQL The Parameterized ObjectQuery Querying with Methods Querying with LINQ Methods Querying with Query Builder Methods and Entity SQL The Shortest Query ObjectQuery, ObjectSet, and LINQ to Entities Querying with EntityClient to Return Streamed Data EntityConnection and the Connection String EntityCommand ExecuteReader Forward-Only Access to the Fields Translating Entity Queries to Database Queries Pay Attention to the .NET Method’s Impact on Generated SQL
iv | Table of Contents
Download from Library of Wow! eBook
49 50 51 55 55 57 57 58 60 61 61 64 66 66 68 70 71 71 71 71 72
Avoiding Inadvertent Query Execution Summary
74 75
4. Exploring LINQ to Entities in Greater Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Getting Ready with Some New Lingo Projections in Queries Projections in LINQ to Entities VB and C# Syntax Differences LINQ Projections and Special Language Features Projections with LINQ Query Methods Using Navigations in Queries Navigating to an EntityReference Filtering and Sorting with an EntityReference Navigating to Entity Collections Projecting Properties from EntityCollection Entities Filtering and Sorting with EntityCollections Aggregates with EntityCollections Aggregates in LINQ Methods Joins and Nested Queries Joins Nested Queries Grouping Naming Properties When Grouping Chaining Aggregates Filtering on Group Conditions Shaping Data Returned by Queries Limiting Which Related Data Is Returned Loading Related Data Controlling Lazy Loading Explicitly Loading Entity Collections and Entity References Using the Include Method to Eager-Load Pros and Cons of Load and Include Retrieving a Single Entity Retrieving a Single Entity with GetObjectByKey Finding More Query Samples Summary
78 78 79 79 80 84 84 84 86 86 87 88 88 89 90 90 91 93 94 95 95 97 99 100 101 101 103 106 107 108 109 109
5. Exploring Entity SQL in Greater Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Literals in Entity SQL Expressing a DateTime Literal Expressing a Decimal Literal Using Additional Literal Types Projecting in Entity SQL
111 112 112 112 113 Table of Contents | v
Download from Library of Wow! eBook
DbDataRecords and Nonscalar Properties Projecting with Query Builder Methods Using Navigation in Entity SQL Queries Navigating to an EntityReference Filtering and Sorting with an EntityReference Filtering and Sorting with EntityCollections Aggregating with EntityCollections Using Entity SQL SET Operators Aggregating with Query Builder Methods Using Joins Nesting Queries Grouping in Entity SQL Returning Entities from an Entity SQL GROUP BY Query Filtering Based on Group Properties Shaping Data with Entity SQL Using Include with an ObjectQuery and Entity SQL Understanding Entity SQL’s Wrapped and Unwrapped Results Entity SQL Rules for Wrapped and Unwrapped Results Digging a Little Deeper into EntityClient’s Results Summary
114 115 115 115 116 116 117 117 118 118 119 120 121 121 122 123 124 126 126 127
6. Modifying Entities and Saving Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Keeping Track of Entities Managing an Entity’s State Saving Changes Back to the Database Inserting New Objects Inserting New Parents and Children Deleting Entities Summary
129 130 131 134 135 137 139
7. Using Stored Procedures with the EDM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Updating the Model from a Database Working with Functions Function Attributes Mapping Functions to Entities Mapping Insert, Update, and Delete Functions to an Entity Inspecting Mappings in XML Using Mapped Functions Using the EDM Designer Model Browser to Import Additional Functions into Your Model Mapping the First of the Read Stored Procedures: ContactsbyState Using Imported Functions Avoiding Inadvertent Client-Side Processing vi | Table of Contents
Download from Library of Wow! eBook
142 143 144 146 148 152 153 155 156 158 159
Mapping a Function to a Scalar Type Mapping a Function to a Complex Type Summary
159 160 163
8. Implementing a More Real-World Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Introducing the BreakAway Geek Adventures Business Model and Legacy Database Creating a Separate Project for an EDM Inspecting and Cleaning Up a New EDM Modifying the Names of Entities and Properties Resolving Collisions Between Property Names and Entity Names Cleaning Up Navigation Property Names Setting Default Values Mapping Stored Procedures Using the Use Original Value Checkbox in Update Mappings Working with Many-to-Many Relationships Inspecting the Completed BreakAway Model Building the BreakAway Model Assembly Looking at the Compiled Assembly Splitting Out the Model’s Metadata Files Summary
166 168 168 170 172 172 174 175 176 178 181 182 183 184 185
9. Data Binding with Windows Forms and WPF Applications . . . . . . . . . . . . . . . . . . . 187 Data Binding with Windows Forms Applications Creating a Windows Forms Application Using Windows Forms Data Sources Creating an Object Data Source for a Customer Entity Getting an Entity’s Details onto a Form Adding Code to Query an EDM When a Form Loads Binding Without a BindingSource Adding an EntityCollection to the Form Displaying the Properties of Related Data in the Grid Allowing Users to Edit Data Editing Navigation Properties (and Shrinking the Query) Replacing the Navigation Property TextBoxes with ComboBoxes Adding New Customers Deleting Reservations Data Binding with WPF Applications Creating the WPF Form Creating the WPF Project Adding the Necessary Data Source Objects Inspecting the XAML and Code Generated by the Automated Data Binding
187 188 189 190 191 194 196 198 199 201 202 204 208 211 213 213 214 215 215
Table of Contents | vii
Download from Library of Wow! eBook
Adding Code to Query the EDM When the Window Loads Customizing the Display of the Controls Selecting an Entity and Viewing Its Details Adding Another EntityCollection to the Mix Editing Entities and Their Related Data Using SortDescriptions to Keep Sorting in Sync with Data Modifications Adding Items to the Child EntityCollection The Last Task: Adding New Trips to the Catalog Summary
216 218 219 222 224 225 226 227 230
10. Working with Object Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 Where Does Object Services Fit into the Framework? Processing Queries Parsing Queries: From Query to Command Tree to SQL Understanding Query Builder Methods Analyzing a Query with ObjectQuery Methods and Properties Executing Queries with ToList, ToArray, First or Single Executing Queries with the Execute Method Overriding a Default Connection with ObjectContext.Connection Handling Command Execution with EntityClient Materializing Objects Managing Object State Using EntityKey to Manage Objects Merging Results into the Cache with MergeOptions Inspecting ObjectStateEntry Maintaining EntityState Managing Relationships Attaching and Detaching Objects from the ObjectContext Taking Control of ObjectState ObjectStateManager Methods ObjectStateEntry State Methods for Managing State ObjectSet State Methods Sending Changes Back to the Database ObjectContext.SaveChanges Affecting SaveChanges Default Behavior Overriding SaveChanges Completely Data Validation with the SavingChanges Event Concurrency Management Transaction Support Implementing Serialization, Data Binding, and More Object Services Supports XML and Binary Serialization Object Services Supports Data Binding
viii | Table of Contents
Download from Library of Wow! eBook
231 233 234 235 238 241 242 242 244 244 246 246 247 248 249 252 253 257 257 258 259 259 259 260 261 261 261 262 263 263 265
Summary
266
11. Customizing Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Partial Classes Using Partial Methods The OnContextCreated Method The On[Property]Changed and On[Property]Changing Methods Using PropertyChanged to Calculate Database-Computed Columns Locally Subscribing to Event Handlers The ObjectContext.ObjectMaterialized Event The ObjectContext.SavingChanges Event The EntityObject.PropertyChanging and EntityObject.PropertyChanged Events The AssociationChanged Event Creating Your Own Partial Methods and Properties Overriding the Object Constructor Overriding ObjectContext.SaveChanges Creating Custom Properties Overloading Entity Creation Methods Using Partial Classes for More Than Just Overriding Methods and Events Overriding Default Code Generation Switching to a Template Reading the Template Modifying the Template Customizing a Template for Major Class Modifications Switching Between the Default Template and a Custom Template Summary
267 269 269 271 273 274 275 276 280 282 284 284 285 286 289 290 291 292 292 293 295 295 296
12. Data Binding with RAD ASP.NET Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Using the EntityDataSource Control to Access Flat Data Creating the Hello Entities Project Creating a GridView and an EntityDataSource Concurrently Configuring an EntityDataSource with Its Wizard Formatting the GridView Testing the Web Application Understanding How the EntityDataSource Retrieves and Updates Your Data EntityDataSource and Its Query EntityDataSource and Its ObjectContext EntityDataSource Context Events EntityDataSource and ViewState
298 298 299 299 301 303 304 304 305 306 306
Table of Contents | ix
Download from Library of Wow! eBook
Accessing Foreign Keys When There Is No Foreign Key Property Working with Related EntityReference Data Using EntityDataSource.Include to Get Related Data Displaying Data That Comes from EntityReference Navigation Properties Using a New EntityDataSource Control to Enable Editing of EntityReference Navigation Properties Editing EntityReferences That Cannot Be Satisfied with a Drop-Down List Binding an EntityDataSource to Another Control with WhereParameters Editing Related Data Concurrently with Multiple EntityDataSource Controls Working with Hierarchical Data in a Master/Detail Form Setting Up the Web Application Specifying Your Own Entity SQL Query Expression for an EntityDataSource Binding a DropDownList to an EntityDataSource Control Creating a Parent EntityDataSource That Is Controlled by the DropDownList and Provides Data to a DetailsView Using the EntityDataSource.Where Property to Filter Query Results Displaying Read-Only Child Data Through the Parent EntityDataSource Using a New EntityDataSource to Add a Third Level of Hierarchical Data to the Master/Detail Form Using the EntityDataSource.Inserting Event to Help with Newly Added Entities Testing the Application Exploring EntityDataSource Events Building Dynamic Data Websites Summary
308 309 309 310 312 313 314 316 317 317 318 319 320 321 321 323 325 326 327 329 332
13. Creating and Using POCO Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Creating POCO Classes Creating an ObjectContext Class to Manage the POCOs Change Tracking with POCOs Understanding the Importance of DetectChanges Loading Related Data with POCOs Loading from the Context Lazy Loading from a Dynamic Proxy Exploring and Correcting POCOs’ Impact on Two-Way Relationships Using the DetectChanges Method to Fix Relationships Enabling Classes to Fix Their Own Relationships
x | Table of Contents
Download from Library of Wow! eBook
336 339 341 341 341 342 342 342 343 344
Using Proxies to Enable Change Notification, Lazy Loading, and Relationship Fix-Up Change Notification by Proxy Lazy Loading by Proxy Exploring the Proxy Classes Synchronizing Relationships by Proxy Using T4 to Generate POCO Classes Modifying the POCO Template Creating a Model That Works with Preexisting Classes Code First: Using Entity Framework with No Model at All Summary
345 346 346 347 348 350 354 358 359 359
14. Customizing Entity Data Models Using the EDM Designer . . . . . . . . . . . . . . . . . . . . 361 Mapping Table per Type Inheritance for Tables That Describe Derived Types Mapping TPT Inheritance Querying Inherited Types POCO Classes and Inherited Objects Inserting TPT Inherited Types Specifying or Excluding Derived Types in Queries Creating New Derived Entities When the Base Entity Already Exists TPT with Abstract Types Mapping Unique Foreign Keys Mapping an Entity to More Than One Table Merging Multiple Entities into One Querying, Editing, and Saving a Split Entity Mapping Stored Procedures to Split Tables and More Splitting a Single Table into Multiple Entities Filtering Entities with Conditional Mapping Creating a Conditional Mapping for the Activity Entity Querying, Inserting, and Saving with Conditional Mappings Filtering on Other Types of Conditions Removing the Conditional Mapping from Activity and Re-creating the Category Property Implementing Table per Hierarchy Inheritance for Tables That Contain Multiple Types Creating the Resort Derived Type Setting a Default (Computed) Value on the Table Schema Testing the TPH Mapping Choosing to Turn a Base Class into an Abstract Class Creating Complex Types to Encapsulate Sets of Properties Defining a Complex Type Reusing Complex Types
362 363 365 366 366 368 370 371 373 375 376 378 380 381 383 385 385 387 388 389 390 391 392 393 393 394 396
Table of Contents | xi
Download from Library of Wow! eBook
Querying, Creating, and Saving Entities That Contain Complex Types Removing the Complex Types from the Model Using Additional Customization Options Using GUIDs for EntityKeys Mapping Stored Procedures Mapping Multiple Entity Sets per Type Mapping Self-Referencing Associations Summary
397 398 399 399 399 399 400 401
15. Defining EDM Mappings That Are Not Supported by the Designer . . . . . . . . . . . . . 403 Using Model-Defined Functions Using Model-Defined Functions to Return More Complex Results Consuming the Complex Results Reading the Results from a Complex Function Mapping Table per Concrete (TPC) Type Inheritance for Tables with Overlapping Fields Using QueryView to Create Read-Only Entities and Other Specialized Mappings Finding a Common Use Case for QueryView Creating a CustomerNameAndID Entity Creating a QueryView Mapping for CustomerNameAndID Testing the QueryView Deconstructing the QueryView Summary
403 407 408 408 409 411 413 413 414 416 416 417
16. Gaining Additional Stored Procedure and View Support in the Raw XML . . . . . . . 419 Reviewing Procedures, Views, and UDFs in the EDM Working with Stored Procedures That Return Data Using Functions That Match an Entity Whose Property Names Have Been Changed Query Stored Procedures and Inherited Types Composing Queries Against Functions Replacing Stored Procedures with Views for Composability Queries That Return Multiple Result Sets Executing Queries on Demand with ExecuteStoreQuery Querying to a Class That Is Not an Entity Querying into an Entity Adding Native Queries to the Model Defining a Complex Type in the Model Browser Adding Native Views to the Model DefiningQuery Is Already in Your Model Using DefiningQuery to Create Your Own Views Implementing a DefiningQuery xii | Table of Contents
Download from Library of Wow! eBook
419 420 420 421 423 423 424 424 424 425 426 427 429 429 431 433
Creating Associations with the New Entity Using DefiningQuery to Solve More Complex Problems Using Commands That Affect the Database Executing SQL on the Fly with ExecuteStoreCommand Using Functions to Manipulate Data in the Database Mapping Insert/Update/Delete to Types Within an Inheritance Structure What If Stored Procedures Affect Multiple Entities in an Inheritance Structure? Implementing and Querying with User-Defined Functions (UDFs) Summary
437 438 440 440 441 444 445 445 447
17. Using EntityObjects in WCF Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449 Planning for an Entity Framework–Agnostic Client Assessing the Pros and Cons of an Entity Framework–Agnostic Consumer Building a Simple WCF Service with EntityObjects Creating the Service Defining the Service Operations Defining Extra Service Classes Exposing Custom Properties Implementing the Service Interface Adding Graphs to ObjectContext Deleting Objects Updating the Object Graph Client Rules for Identifying Changes in an EntityCollection The UpdateCustomer Method Handling New and Existing Reservations Deleting Reservations Building a Simple Console App to Consume an EntityObject Service Enabling the Client Application to Receive Large Messages from the Service Creating Methods to Test the Service Operations Analyzing the GetAndUpdateCustomer Method Testing Out the Other Service Operations Creating WCF Data Services with Entities Putting WCF Data Services in Perspective Creating a WCF Data Service Filtering at the Service Level Using QueryInterceptor Anticipating Exceptions Exposing Related Data Through the Service Preparing for WCF Data Services’ Limitations Modifying Data Through a Service Learning More About Creating and Consuming WCF Data Services
450 451 452 453 454 455 456 457 460 461 463 463 463 465 466 467 468 469 473 474 474 475 475 480 481 481 483 484 485
Table of Contents | xiii
Download from Library of Wow! eBook
Understanding How WCF RIA Services Relates to the Entity Framework Summary
485 487
18. Using POCOs and Self-Tracking Entities in WCF Services . . . . . . . . . . . . . . . . . . . . . . 489 Creating WCF-Friendly POCO Classes Updating the POCO Classes Based on the Current BreakAway Model Isolating the POCO Entities in Their Own Project Adding Custom Logic to the POCO Entities with a Base Class Following WCF Collection Rules Preventing Properties from Being Marked As Virtual Building a WCF Service That Uses POCO Classes Implementing the Interface Using the Service Using the Self-Tracking Entities Template for WCF Services Creating and Exploring the Self-Tracking Entities Putting the Change-Tracking Logic Where It’s Needed Creating a WCF Service That Uses Self-Tracking Entities Watching Self-Tracking Entities Under the Covers Inspecting the Generated Context Class and Extensions Using POCO Entities with WCF Data and RIA Services Preparing for WCF Data Services Using POCO Entities in WCF RIA Services Sorting Out the Many Options for Creating Services Summary
490 490 491 493 495 496 497 498 500 503 503 505 506 507 513 515 515 517 519 520
19. Working with Relationships and Associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521 Deconstructing Relationships in the Entity Data Model Understanding How the Entity Data Model Wizard Creates the Association Understanding Additional Relationship Items Handling Nonessential Navigation Properties Understanding the Major Differences Between Foreign Key Associations and Independent Associations Defining Associations in Metadata Detecting Associations at Runtime Deconstructing Relationships Between Instantiated Entities Understanding Relationship Manager and the IRelatedEnd Interface Late-Binding Relationships Taking a Peek Under the Covers: How Entity Framework Manages Relationships Understanding Navigation Properties Understanding Referential Integrity and Constraints Implementing Deletes and Cascading Deletes xiv | Table of Contents
Download from Library of Wow! eBook
522 523 525 526 527 528 528 529 530 530 531 534 537 540
Defining Relationships Between Entities The CLR Way: Setting a Navigation Property to an Entity Setting a Foreign Key Property Setting an EntityReference Using an EntityKey Loading, Adding, and Attaching Navigation Properties Lazy Loading EntityReference.Load and EntityCollection.Load Loading from Detached Entities: Lazy and Explicit Using EntityCollection.Add Using Attach and Remove Moving an Entity to a New Graph Learning a Few Last Tricks to Make You a Relationship Pro Using CreateSourceQuery to Enhance Deferred Loading Getting a Foreign Key Value in an Independent Association Summary
542 543 544 544 545 545 547 547 548 549 550 551 551 552 553
20. Real World Apps: Connections, Transactions, Performance, and More . . . . . . . . . . 555 Entity Framework and Connections Overriding EntityConnection Defaults Working with Connection Strings Programmatically Opening and Closing Connections Getting the Store Connection from EntityConnection Disposing Connections Pooling Connections Fine-Tuning Transactions Why Use Your Own Transaction? Understanding Implicit Entity Framework Transactions Specifying Your Own Read/Write Transactions Specifying Your Own Read-Only Transactions Rolling Back Transactions Understanding Security Guarding Against SQL Injection Guarding Against Connection Piggybacks Fine-Tuning Performance Measuring Query Performance Measuring Startup Performance Reducing the Cost of Query Compilation Caching for Entity SQL Queries Precompiling Views for Performance Precompiling LINQ to Entities Queries for Performance Fine-Tuning Updates for Performance? Lacking Support for Full Text Searches Exploiting Multithreaded Applications
555 556 557 560 562 562 563 564 564 565 566 569 570 571 571 573 574 575 579 580 580 582 585 589 590 591
Table of Contents | xv
Download from Library of Wow! eBook
Forcing an ObjectContext to Use Its Own Thread Implementing Concurrent Thread Processing Exploiting .NET 4 Parallel Computing Summary
591 593 596 596
21. Manipulating Entities with ObjectStateManager and MetadataWorkspace . . . . . 597 Manipulating Entities and Their State with ObjectStateManager Refreshing Your High-Level Understanding of ObjectStateEntry Getting an ObjectStateManager and Its Entries Building Extension Methods to Overload GetObjectStateEntries Building a Method to Return Managed Entities Using GetObjectStateEntry and TryGetObjectStateEntry Mining Entity Details from ObjectStateEntry Leveraging the ObjectStateManager During Saves Using ObjectStateManager to Build an EntityState Visualizer Retrieving an ObjectStateEntry Using an EntityKey Reading the OriginalValues and CurrentValues of an ObjectStateEntry Determining Whether a Property Has Been Modified Displaying the State and Entity Type Getting ComplexType Properties Out of ObjectStateEntry Modifying Values with ObjectStateManager Working with Relationships in ObjectStateManager Using the MetadataWorkspace Loading the MetadataWorkspace Clearing the MetadataWorkspace from Memory Understanding the MetadataWorkspace ItemCollections Retrieving Metadata from the MetadataWorkspace Querying the Metadata with LINQ to Objects Building Dynamic Queries and Reading Results Building Entity SQL Queries Dynamically Using Metadata Creating Queries on the Fly with CreateObjectSet and Query Builder Methods Reading the Results of a Dynamically Created Query Creating and Manipulating Entities Dynamically Creating EntityObjects Without Entity Classes Creating Entities and Graphs Dynamically Summary
598 599 599 600 602 603 604 609 611 612 613 614 614 615 619 620 622 622 623 624 625 628 629 629 632 634 637 637 640 643
22. Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645 Preparing for Exceptions Handling EntityConnectionString Exceptions
xvi | Table of Contents
Download from Library of Wow! eBook
645 647
Connection String Can’t Be Found or Is Improperly Configured: System.ArgumentException Metadata Files Cannot Be Found: System.Data.MetadataException Handling Connection String Exceptions Handling Query Compilation Exceptions Invalid LINQ to Entities Query Expressions: System.NotSupportedException Invalid Entity SQL Query Expressions: EntitySqlException EntityCommandCompilationException Thrown by the Store Provider Creating a Common Wrapper to Handle Query Execution Exceptions Handling Exceptions Thrown During SaveChanges Command Execution UpdateException: Thrown When Independent Association Mapping Constraints Are Broken UpdateException: Thrown by Broken Constraints in the Database Relying on Entity Framework to Automatically Roll Back When an UpdateException Occurs Gleaning Details from UpdateException Planning for Other Exceptions Related to the Entity Framework Handling Concurrency Exceptions Summary
648 648 649 649 649 650 652 652 654 654 655 656 656 657 658 658
23. Planning for Concurrency Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 659 Understanding Database Concurrency Conflicts Understanding Optimistic Concurrency Options in the Entity Framework Ignoring Concurrency Conflicts Forcing the User’s Data to the Server (ClientWins) Refreshing the User’s Data with Server Data (StoreWins) Determining the Scope of Changes Using rowversion (a.k.a. timestamp) for Concurrency Checks Implementing Optimistic Concurrency with the Entity Framework Flagging a Property for Concurrency Checking How the Entity Framework Uses the ConcurrencyMode Property Concurrency Checking Without a rowversion Field Concurrency Checking on a Checksum in the Data Store Concurrency Checks for EntityReference Navigation Properties Concurrency Checks and Inherited Types Concurrency Checks and Stored Procedures Handling OptimisticConcurrencyExceptions Using ObjectContext.Refresh Using Refresh with ClientWins Using Refresh with StoreWins
660 660 661 661 661 662 662 663 664 665 666 666 667 667 668 670 671 671 673
Table of Contents | xvii
Download from Library of Wow! eBook
Refreshing Collections of Entities Refreshing Related Entities in a Graph Rewinding and Starting Again, and Maybe Again After That Reporting an Exception Handling Concurrency Exceptions at a Lower Level Handling Exceptions in a Granular Way Without User Intervention Handling Multiple Conflicts Handling Exceptions When Transactions Are Your Own Summary
673 675 676 678 678 678 680 682 683
24. Building Persistent Ignorant, Testable Applications . . . . . . . . . . . . . . . . . . . . . . . . 685 Testing the BreakAway Application Components Getting Started with Testing Writing an Integration Test That Hits the Database Inspecting a Failed Test Writing a Unit Test That Focuses on Custom Logic Creating Persistent Ignorant Entities Planning the Project Structure Starting with the Model and Its POCO Entities Building an Interface to Represent a Context Modifying the BAEntities ObjectContext Class to Implement the New Interface Creating the IEntityRepository Interface Creating the Repository Classes Testing GetReservationsForCustomer Against the Database Creating a Fake Context Creating a FakeObjectSet Class Completing the Fake Context Building Tests That Do Not Hit the Database Adding Validation Logic to the POCO Class Adding Validation Logic to the Context Providing ManagedEntities in the FakeContext Hiding the Context from the Lower Layers with Unit of Work Testing UnitOfWork Against the Database Enabling Eager Loading in IContext Leveraging Precompiled Queries in Your Repositories Using the New Infrastructure in Your Application Adding a UI Layer That Calls the Repository Application Architecture Benefits from Designing Testable Code Considering Mocking Frameworks? Summary
xviii | Table of Contents
Download from Library of Wow! eBook
686 687 687 689 689 693 695 697 698 699 702 703 706 708 710 712 714 714 716 716 718 720 721 722 723 723 724 725 725
25. Domain-Centric Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727 Creating a Model and Database Using Model First Creating a Conceptual Model in the Designer Creating the Entities Creating Association and Inheritance Hierarchies Generating Database Schema from the Model Creating the Database and Its Schema Overriding the DDL Generation Using the Feature CTP Code-First Add-On Understanding Code-First Design Installing the Feature CTP Exploring Some Configuration Examples Testing the Code-First Application and Database Using SQL Server Modeling’s “M” Language Using M Metadata in Entity Framework Applications Summary
728 728 730 734 738 744 745 747 749 751 751 753 755 758 759
26. Using Entities in Layered Client-Side Applications . . . . . . . . . . . . . . . . . . . . . . . . . . 761 Isolating the ObjectContext Freeing Entities from Change Tracking Enabling Change Tracking Across Tiers Moving Other ObjectContext-Dependent Logic to the DataBridge Ensuring That Lazy Loading Doesn’t Negatively Impact the Layered Application Noting Additional Benefits of the Layered Application Separating Entity-Specific Logic from ObjectContext Logic Working with POCO Entities Providing EntityState Providing Logic in Place of Other EntityObject Behavior Summary
762 764 766 768 772 773 774 778 779 781 782
27. Building Layered Web Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 783 Understanding How ObjectContext Fits into the Web Page Life Cycle Return Results, Not Queries, from the DataBridge Class Using Entities in Read-Only Web Pages Exploring Options for Updating Entities in an ASP.NET Web Forms Application Comparing ASP.NET’s State Solutions to the Needs of the Entity Framework Building an N-Tier Web Forms Application Designing the Application Using the Existing Repositories
783 785 786 788 789 793 794 795
Table of Contents | xix
Download from Library of Wow! eBook
Building an Entity Manager to Act As a DataBridge Retrieving Data for Display and for Future Updates Making the Related Data Accessible to the Client Getting Data from the Manager to the Client Adding Lists for User Selection Controls Allowing a User to Modify Related Data Building an ASP.NET MVC Application Replacing the Context with Repositories Editing Entities and Graphs on an MVC Application Creating a Repository for Payments Interacting with the ReservationController Summary
795 797 799 800 803 805 806 813 814 817 817 818
A. Entity Framework Assemblies and Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 821 B. Data-Binding with Complex Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825 C. Additional Details About Entity Data Model Metadata . . . . . . . . . . . . . . . . . . . . . . 831 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 839
xx | Table of Contents
Download from Library of Wow! eBook
Foreword
I first met Julie Lerman (rhymes with “German”) while she was visiting the Microsoft campus for a Software Design Review (SDR). An SDR is an event where we invite customers we trust to be representative of a much larger crowd. In this particular case, I was new to the SQL Server division and trying hard to catch up on the raft of technologies Microsoft shipped in the data space for developers. Julie, on the other hand, was a seasoned veteran and not only knew the answers to all of my Entity Framework questions but had already written a book on the topic. That book, Programming Entity Framework, was the first edition of the book you’re now holding in your hands. Or, if you are a .NET programmer, you know it simply as “THE book on EF.” As the months went on, I ran into Julie more and more. She was researching the second edition of her famous EF book. And by “researching,” I mean “pointing out our mistakes.” Julie was not only invaluable for teaching customers the real-world ins and outs of EF, she had a way of asking questions about alphas and betas that made us rethink what we were doing in many cases to improve the version of EF that ships with .NET 4 as well as the supporting functionality in Visual Studio 2010. And she was so well respected because of her first EF book that anything she said received extra attention from the EF team in ways I don’t see for many senior architects, let alone lowly program managers. Julie had become an ad hoc member of the EF team itself. My most recent encounter with Julie was by far the most fun. At a talk at the 2010 TechEd in New Orleans, I had the privilege of being Julie’s “code monkey,” which meant mostly that I fetched her coffee, carried her bags, and wrote her code while she entertained and educated a packed room. In 60 minutes, she did a tour de force tour through nearly all the new features in EF 4.0, driving me through one complete demo every 4 minutes. Normally, this would make an audience’s heads spin, but she has such a grasp of the material and such a clear way of presenting it that she had everyone’s rapt attention. It’s this same completeness and clarity that you’ll find in this book, in chapters ranging from the basics in the details you’ll need to write actual applications for your actual business needs. If there is more material to lead you through the basics of the Entity Framework and to be a continuing reference, I don’t know what it is.
xxi
Download from Library of Wow! eBook
During her presentation, Julie fielded questions on all manner of EF details and related topics, but the one that made me cringe under the weight of history is the one I always get, too: “Why should we use EF when Microsoft has already given us so many other data access technologies?” Julie’s answer came without hesitation: “Because it’s the best!” Now, as a Microsoft employee sensitive to the needs of a wide-range of customers across a wide-range of needs, I have to say that officially you should use the technology that best fits your specific business problem. I can also say that the Entity Framework is the .NET technology against which we’re placing all of our future bets and making all of our biggest investments, which means that it’s the technology that we hope meets most of your needs now and will meet more of your needs in the future. But, I have to say, I do like Julie’s answer a great deal. —Chris Sells, SQL Server division, Microsoft Corporation
xxii | Foreword
Download from Library of Wow! eBook
Preface
In June 2006, I was invited to attend a meet-up for data geeks at Tech Ed North America. As the meet-up was early enough not to compete with the many fun evening parties at Tech Ed, I happily crossed the lovely bridge between the Convention Center and the hotel where the meeting was to take place. Little did I know I was about to see a new technology from Microsoft’s Data Programmability team that was going to be the focus of my attention for the next few years. In addition to other geeky discussions about data access, Pablo Castro, Mike Pizzo, and Britt Johnson (all from Microsoft) talked to us about a new technology that was coming in the next version of ADO.NET. It would allow developers to create their own views of their database and query against these views rather than against the database. As usual, Tech Ed was overwhelming, so as interesting as this new way of working with data looked to me, I had to put it in a back corner of my mind and let it simmer for a few months. I finally downloaded the preview and began playing with it. What was most fun to me when I started exploring this technology, called Entity Framework, was the lack of serious documentation, which forced me to play with all of its knobs and dials to figure out what was in there and how it worked. Unlike many in-development technologies from Microsoft, the Entity Framework did not start off with a cool name as did WCF (née Indigo) and ADO.NET Data Services (Astoria). Although it is often hard to give up these early technology nicknames for their final (and relatively boring) names, the Entity Framework has had its “grown-up name” since the beginning.
Over this time, it also became clear how important the Entity Framework and its underlying Entity Data Model are to Microsoft. They are a critical part of Microsoft’s strategy for the data access that all of its products perform, whether this is the data that Reporting Services uses to enable us to build reports, the data that comprises Workflow, data in the cloud, or data that we developers want our .NET applications to access.
xxiii
Download from Library of Wow! eBook
As the Entity Framework evolved and further CTPs were released, followed by betas, I became quite fond of working against a data model and no longer having to think about the structure of the database I was working against. I began to peel away the top layers of the Entity Framework and discovered that I could make it do nearly anything I wanted as I gained a better understanding of how it worked. When I hit a wall, I asked the Entity Framework team how to get past it, and if there wasn’t a way to do so, I camped out on their virtual doorstep until they modified the framework or Designer to enable me to do what I wanted and what I knew other developers would need. During this time, I was excited to share what I was learning with other developers through the MSDN forums, my blog, conference sessions, and articles. However, I constantly felt restrained by the time or space allotted to me. Conference sessions are generally 75–90 minutes long. Magazine articles tend to be 5–10 pages. I felt as though I was going to self-combust if I couldn’t share all of this new information, and so I contacted O’Reilly to ask whether I could write a book about the Entity Framework. My editor on the first edition, John Osborn, was a bit taken aback because for years I had shunned publishers, saying I would have to have lost my mind to write a book. It’s not a simple undertaking. But I knew that if I didn’t write a book about ADO.NET Entity Framework, I certainly would lose my mind. The time had come. I was so excited, and of course, I had no idea what I was in for! I spent almost a year writing the book that ended up at a little over 800 pages and more than two pounds on the scale. When the book was released in February 2009, Microsoft was already well underway on the next version of Entity Framework, which was going through major changes. After spending some time with the early releases of what was to become Entity Framework 4, and with some trepidation, I finally decided to revise the book. This was after having enlisted many friends to “please, just shoot me” if I ever talked about writing another book. Thankfully, nobody took me up on the request. They admitted it was because they wanted a new version of my book targeted at the new version of Entity Framework. Once again, I had no idea what I was in for. This edition has been much more than a revision. I have had to rethink every sentence in the book, throw out entire chapters, add new chapters, rethink and rewrite most of the code samples, and of course, learn about a slew of major features that have been added to Entity Framework. I spent over nine months of constant effort writing this new edition, and now here it is.
Who This Book Is For This book is written for developers who are familiar with .NET programming, whether they are entirely new to the Entity Framework or have been using it and want to solidify their current understanding as well as go deeper. Throughout the book, I highlight notable changes for developers who have been using the first version of Entity Framework. The first half of the book (Chapters 1–12) covers introductory topics, and the latter half (Chapters 13–27) dives under the covers to give you a deep understanding xxiv | Preface
Download from Library of Wow! eBook
of what you’ll find in the Entity Framework and how it works, as well as how to get the most out of it. The early walkthroughs, which demonstrate the use of the Entity Framework in a variety of applications (Windows Forms, Windows Presentation Foundation, ASP.NET, WCF services, and WCF Data Services), are written so that you can follow them even if you have never created a particular application type before. The goal of this book is to help developers not only get up and running with the Entity Framework, but also be empowered to gain granular control over the model and the objects that result through use of the core Entity Framework APIs. This second edition focuses on the version of Entity Framework in Visual Studio 2010 and .NET 4. Although the book will provide some guidance for using the Entity Framework in your application architecture, it is not a book about architecture. Instead, the book attempts to provide you with the information and knowledge you need to use the Entity Framework to solve your specific domain problems. Because of the vast scope of the Entity Framework, many topics on tools that leverage the Entity Framework, such as WCF RIA Services (a.k.a. Astoria) and SQL Modeling, are touched on but not covered in depth. Some of the Entity Framework’s features are comparable to LINQ to SQL and other object relational models such as NHibernate and LLBLGen Pro. Apart from a few paragraphs in Chapter 1, this book does not directly position the Entity Framework against these object relational models. All of the code samples in Programming Entity Framework, Second Edition, are provided in C#. Where there are significant syntax differences, Visual Basic is included as well.
How This Book Is Organized Programming Entity Framework, Second Edition, focuses on two ways for you to learn. If you learn best by example, you’ll find many walkthroughs and code samples throughout the book; if you’re always looking for the big picture, you’ll also find chapters that dive deep into conceptual information. I have tried to balance the walkthroughs and conceptual information I provide so that you will never get too much of one at a time. The first half of the book is introductory, and the second half digs much deeper. Following is a brief description of each chapter: Chapter 1, Introducing the ADO.NET Entity Framework This chapter provides an overview of the ADO.NET Entity Framework—where it came from, what problems it attempts to solve, and the classic “10,000-foot view” of what it looks like. The chapter also addresses the most frequently asked
Preface | xxv
Download from Library of Wow! eBook
questions about the Entity Framework, such as how it fits into the .NET Framework, what databases it works with, what types of applications you can write with it, how it differs from object relational models, and how it works with the rest of ADO.NET. Chapter 2, Exploring the Entity Data Model The Entity Data Model (EDM) lies at the core of the Entity Framework. This chapter explains what the EDM is, and teaches you how to create one using the EDM Wizard and then manipulate your model using the Designer. You will also get a walkthrough of the various parts of the EDM, viewing it through the Designer or through its raw XML. Chapter 3, Querying Entity Data Models The Entity Framework provides a number of methods for querying against the EDM—LINQ to Entities, Entity SQL with ObjectQuery, EntityClient, and a few more. Each method has its own benefits. In this chapter, you will learn the basics for leveraging the various query modes by requesting the same data using each mechanism. You will also learn the pros and cons for choosing one method over another, as well as gain an understanding of what happens behind the scenes in between query execution and the creation of objects from the data that results. Chapter 4, Exploring LINQ to Entities in Greater Depth With the query basics in hand, you can now learn how to perform different types of tricks with querying: projection, filtering, aggregates, and so forth. Because the objects you are querying are related, you can also query across these relationships. This chapter will walk you through a great variety of queries focusing on LINQ to Entities. This is by no means an exhaustive depiction of every type of query you can perform, but it will give you a huge head start. Chapter 5, Exploring Entity SQL in Greater Depth This chapter revisits the LINQ to Entities queries from Chapter 4 and shows how to express the same types of queries using Entity SQL. You’ll also learn some specific tips about working with Entity SQL in this chapter. Chapter 6, Modifying Entities and Saving Changes This chapter presents a high-level view of how the Entity Framework tracks changes to entities, processes updates, and builds the final queries that are executed at the database. By having a better understanding of the Entity Framework’s default functionality, you will be better prepared to address common concerns regarding security and performance. Additionally, understanding the default process will make the following chapter on stored procedures much more meaningful. Chapter 7, Using Stored Procedures with the EDM This chapter is the first of two to dig into using stored procedures in the Entity Framework. The EDM Designer provides support for one set of scenarios, and that is what is covered in this chapter. Chapter 16 covers the set of scenarios that require more effort.
xxvi | Preface
Download from Library of Wow! eBook
Chapter 8, Implementing a More Real-World Model Up to this point in the book, you will have been working with a very simplistic database and model so that you can focus on all of the new tools. This chapter introduces a larger model and database that support the fictitious travel adventure company BreakAway Geek Adventures and which you will use throughout the rest of the book. With this model, you will get a better understanding of building and customizing a model. Chapters 14 and 15 will go even further into customizing the model with advanced modeling and mappings. Chapter 9, Data Binding with Windows Forms and WPF Applications This chapter provides two walkthroughs for using the Entity Framework to perform data binding in Windows Forms and Windows Presentation Foundation (WPF). In the course of these walkthroughs, you’ll learn a lot of tips and tricks that are specific to doing data binding with Entity Framework objects, as well as expand your knowledge of the Entity Framework along the way. Chapter 10, Working with Object Services The Entity Framework’s Object Services API provides all of the functionality behind working with the objects that are realized from the data shaped by your Entity Data Model. Although the most critical of Object Services’ features is its ability to keep track of changes to entity objects and manage relationships between them, it offers many additional features as well. This chapter provides an overview of all of Object Services’ responsibilities, how it impacts most of the work you do with the Entity Framework, and how you can use these features directly to impact how the Entity Framework operates. Later chapters focus even more deeply on particular areas within Object Services. Chapter 11, Customizing Entities So far, the objects you will have been working with are based on the default classes that the Entity Framework generates directly from the model, but you don’t need to be limited to what’s in the objects. There are plenty of opportunities for customizing the code-generated classes. This chapter walks you through how to take advantage of these extensibility points. It is also possible to completely avoid the generated classes and use your own custom classes, an option we will cover in Chapter 13. Chapter 12, Data Binding with RAD ASP.NET Applications It’s time to create another application with the Entity Framework. There are a lot of hurdles to overcome when using the Entity Framework in an ASP.NET application that allows users to edit data. The EntityDataSource control is part of the family of ASP.NET DataSource controls that you can configure in the UI and that will automate data access and updating for you. This chapter will show you how to use this control. You’ll also get a chance to use ASP.NET Dynamic Data Controls in this chapter. Later chapters will teach you what you need to know to overcome these hurdles yourself, and Chapter 27 leverages this knowledge to address building layered ASP.NET applications rather than putting the logic in the UI.
Preface | xxvii
Download from Library of Wow! eBook
Chapter 13, Creating and Using POCO Entities A critical advancement to Entity Framework in .NET 4 is its support for Plain Old CLR Objects (POCOs). The POCO support means that entity classes are not required to inherit from Entity Framework’s EntityObject class. Building POCOs opens the door for a more agile architecture, unit testing, repositories, and persistence ignorance, all while continuing to benefit from the Entity Framework. This chapter provides an introduction to Entity Framework’s POCO support. Later chapters will leverage POCOs to show alternative patterns, build repositories and tests, and consume the POCOs in a variety of application types. Chapter 14, Customizing Entity Data Models Using the EDM Designer One of the most important features of the Entity Data Model is the ability to customize it to shape your data structure in a way that is more useful than working directly against the database schema. This chapter walks through many of the ways you can achieve this with the Designer, demonstrating how to implement a variety of inheritance mappings, create an entity that maps to multiple tables, build complex types, and more. If you are following along with the walkthroughs, most of the modifications you make to the sample model in this chapter you will use for applications you’ll build in later chapters. Chapter 15, Defining EDM Mappings That Are Not Supported by the Designer The Entity Framework model supports even more ways to map back to the database but, unfortunately, not all are supported by the Designer. In this chapter, you’ll learn about the most common types of mappings that you might want to use but will have to open up the raw XML to implement. Among these are DefiningQuery, QueryView, and even nonexistent database views and stored procedures that you can define directly in the Entity Framework metadata. Chapter 16, Gaining Additional Stored Procedure and View Support in the Raw XML Chapter 7 covers the stored procedure scenarios that the Designer supports, but you can achieve much more if you are willing to crack open the model’s raw XML and perform additional customizations. This chapter will walk you through adding “virtual” store queries and stored procedures into the model, and taking advantage of other features that will make the model work for you, rather than being constrained by the Designer. Chapter 17, Using EntityObjects in WCF Services Like ASP.NET, using the Entity Framework in web and WCF services provides a number of challenges. In this chapter, you will learn how to build and consume a WCF service that interacts solely with EntityObjects. If you have never created services before, have no fear. The walkthroughs will help you with step-by-step instructions. You will also create a WCF Data Service and get a quick look at WCF RIA Services. This chapter is the first of two that address building services. Chapter 18, Using POCOs and Self-Tracking Entities in WCF Services The new POCO support in Entity Framework 4 makes building WCF Services a lot simpler. This chapter enhances the POCO entities you built in Chapter 13 and
xxviii | Preface
Download from Library of Wow! eBook
uses them in a revised implementation of the WCF Services you created in Chapter 17. You’ll also learn about some of the differences when building WCF Data Services and WCF RIA Services with POCOs. The preceding chapters will have provided you with a solid base of understanding for working with the Entity Framework. Starting with Chapter 19, you will learn about the Entity Framework’s advanced topics: Chapter 19, Working with Relationships and Associations The Entity Data Model is based on Entity Relationship Modeling, which is about entities and relationships. Relationships are a critical part of the model and how the Entity Framework performs its functions. To really understand and control the Entity Framework and avoid hurting your head when the relationships don’t behave the way you might expect, you will benefit from a deep comprehension of how relationships work in the model and your Entity Framework code. This chapter will provide you with that understanding. Chapter 20, Real World Apps: Connections, Transactions, Performance, and More Up to this point, you have seen bits and pieces of code out of the context of realworld applications. But how does the Entity Framework fit in with the everyday concerns of software developers? This chapter will address some of the many questions developers ask after learning the basics about the Entity Framework. How do you control connections? Is there any connection pooling? Are database calls transactional? What about security? How’s the performance? Chapter 21, Manipulating Entities with ObjectStateManager and MetadataWorkspace This is another chapter where you get to dig even further into the APIs to interact with your objects in the same way that many of the internal functions do. With the two classes featured in this chapter, you can write code to generically work with entities or raw data whether you want to create reusable code for your apps or write utilities. There are some hefty samples in this chapter. Chapter 22, Handling Exceptions Hard as we try to write perfect code, things can still go wrong in our applications, which is why we need exception handling. The Entity Framework provides a set of its own exceptions to help you deal with the unique problems that may occur when working with entities—poorly written queries, entities that are missing required related objects, or even a problem in the database. Chapter 23, Planning for Concurrency Problems This chapter follows up what you learned about exception handling in Chapter 22 with details on a particular type of exception: the OptimisticConcurrencyEx ception. In addition to typical coding problems, data access highlights another arena of issues regarding concurrency: when multiple people are editing and updating data. The Entity Framework supports optimistic concurrency and uses this exception to detect these problems. The chapter will show you how to prepare for concurrency issues and take advantage of this exception.
Preface | xxix
Download from Library of Wow! eBook
Chapter 24, Building Persistent Ignorant, Testable Applications Chapter 13 introduced you to Entity Framework’s POCO support. Chapter 24 shows you where the POCO support really shines. Here you’ll get a chance to use a pattern that leverages POCO support. You will create repositories and a Unit of Work and build unit tests against your Entity Framework code. You’ll get to use the repository in some applications in later chapters. Chapter 25, Domain-Centric Modeling You’ll find more new .NET 4 and Visual Studio 2010 goodness in this chapter as well as a look to the future. You are no longer required to build database-first models. The EDM Designer in Visual Studio 2010 supports model-first design. Build your model in the Designer and then automatically create a database schema from the model. In this chapter, you’ll learn a lot more about working with the Designer. This chapter also takes a look at two not-yet-released technologies: Entity Framework’s code first and SQL Modeling’s “M.” Both of these technologies let you use Entity Framework without depending on a physical XML-based model. At this point in the book, you will have learned quite a lot about how the Entity Framework functions and how to work with the objects and the model in a granular way. The final two chapters focus on challenges and solutions for using the Entity Framework in enterprise applications. The book concludes with three appendixes: one that serves as a guide to the assemblies and namespaces of the Entity Framework, another that highlights unexpected behaviors when data-binding complex types, and a third that looks more deeply into the XML of the model’s metadata. Chapter 26, Using Entities in Layered Client-Side Applications The earlier client application walkthroughs (Windows Forms and WPF) focused on simple architectures to get you started with data binding. Most medium to large applications are not written in this way, but rather separate their logic and data layers from the UI. This chapter will look at some of the specific features you can take advantage of and challenges you might face when architecting Windows and WPF applications to keep the data access and business logic out of the user interface. The chapter focuses on a sample WPF application using the repositories from Chapter 24. Chapter 27, Building Layered Web Applications Chapter 12 focused on building RAD ASP.NET apps using the EntityDataSource control to avoid some of the issues with change tracking across tiers in the Entity Framework. Now that you have learned much more about working with entities, it is time to address these challenges head-on and learn how you can build ASP.NET application layers. This chapter begins by addressing the specific issues that the ASP.NET Page life cycle poses for entities, and then walks through two solutions that leverage the repositories from Chapter 24. The first is an ASP.NET Web Forms application that is built without the support of data source controls. The second is an ASP.NET MVC application that focuses on keeping data access code out of
xxx | Preface
Download from Library of Wow! eBook
the controller. The samples in this chapter provide a first step toward concepts that will help you architect applications to fit your own domain model. Appendix A, Entity Framework Assemblies and Namespaces This appendix is a guide to the physical files that are part of the Entity Framework and each namespace in the programming model. Appendix B, Data-Binding with Complex Types In Chapter 14, you learn how to create complex types in the model. Complex types have some interesting (and often unexpected) behavior in data-binding scenarios. This appendix will prepare you for what to expect. Appendix C, Additional Details About Entity Data Model Metadata Chapter 2 goes into plenty of detail about the model’s metadata, but if you are hardcore and want to dig a little further into the raw XML, this appendix should satisfy your cravings.
What You Need to Use This Book This book focuses on the release of Entity Framework that is part of Microsoft Visual Studio 2010 and .NET 4. You can use any of the Visual Studio 2010 versions, from Express through Ultimate. Although the Entity Framework can work with many database providers, the SqlClient provider is part of Visual Studio 2010, and therefore all of the samples here are based on SQL Server. You can use SQL Server Express or Standard, and although the Entity Framework runtime will recognize versions 2000, 2005, and 2008, none of the design tools will not work with SQL Server 2000. This book was written against SQL Server 2008 Standard. Following is a specific list of system requirements: • Windows XP with SP2, Windows Server 2003, Windows Vista and SP1, or Windows 7 • Microsoft SQL Server 2005, Microsoft SQL Server 2005 Express Edition, Microsoft SQL Server 2008, or Microsoft SQL Server 2008 Express Edition • Microsoft Visual Studio 2010
Preface | xxxi
Download from Library of Wow! eBook
This Book’s Website Visit http://www.ProgrammingEntityFramework.com/ (also available at http://www .LearnEntityFramework.com/) for downloads, errata, links to resources, and other information. In the Downloads area, you will find: • Scripts for creating the sample databases used in this book. • The sample applications from the book. I will do my best to provide Visual Basic versions of many of the book’s samples. Note that there are also hundreds of small code samples in the book. In general, you will not find these small examples replicated on the website, although I will provide some of them for varying reasons.
Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities Constant width
Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files, or the output from commands Constant width bold
Shows commands or other text that should be typed literally by the user Constant width italic
Shows text that should be replaced with user-supplied values This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
This icon indicates a Visual Basic code sample. This icon indicates a C# code sample.
xxxii | Preface
Download from Library of Wow! eBook
Using Code Examples This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Programming Entity Framework, Second Edition, by Julia Lerman. Copyright 2010 Julia Lerman, 978-0-596-80726-9.” If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at
[email protected].
Safari® Books Online Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly. With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features. O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.
Comments and Questions Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax)
Preface | xxxiii
Download from Library of Wow! eBook
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at: http://www.oreilly.com/catalog/9780596807269 To comment or ask technical questions about this book, send email to:
[email protected] For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our website at: http://www.oreilly.com/
Acknowledgments And now for the most rewarding writing task after completing over 800 pages of technical writing—thanking the Academy. My academy is a host of bright, generous, and dedicated geeks (and a few nongeeks) who have helped make this book the best it can be. First nods go to the technical reviewers. These are the folks who were willing to read the book in its roughest format and provide feedback to help me make it more useful and comprehensible to you, the readers of the final version. The award for helping to keep me from exposing myself to humiliation over my nascent C# skills goes to Wesley Bakker, a Dutch developer who does code reviews for a living. I learned a lot from Wes and am grateful for his patience and kid-glove handling of my poor ego. I also had a number of EF and EF 4 newbies on board to help ensure that I didn’t make any leaps without bringing them along. You who are new to EF should thank them as well: Camey Combs, Suzanne Shushereba, Doug Holland, and John McConnell. Ward Bell’s brilliant architectural mind was displayed in comments that nearly exceeded my own text. He kept me honest and kept me thinking. Everyone should email Ward and beg him to write a book. I don’t care what the topic is. Ward has deep EF knowledge, as does Per Okvist, whose feedback was also invaluable. Two database gurus were enormously helpful: Bob Beauchemin and Anil Das. Their meticulous minds helped me in areas that reached much further than discussions about database specifics. I also brought in some big guns to look at particular chapters in their area of expertise. Thanks so much to Greg Young, Bobby Johnson, Jarod Ferguson, and Mike Campbell for helping me with my education in persistence ignorance and related topics and for looking over the critical chapter on PI and testing to make sure that I had learned my lessons well. I was close, but they helped guide me where I had strayed. K. Scott Allenand Imar Spaanjaars, both ASP.NET gurus, provided some additional guidance and a read-through of a number of chapters. And then there was the real editing—the organization and flow of the text. John Osborn, who was the editor on the first edition of this book, was engaged to edit this edition as well. It’s hard for me to express my gratitude for the incredible dedication xxxiv | Preface
Download from Library of Wow! eBook
and expertise he provided. Even though I thought myself much more experienced this time around, John took every chapter and reorganized it, clarifying its focus and flow. He is an incredible editor and I was very lucky to have him work on my book again. Along the way, of course, I had help from so many people at Microsoft on the Entity Framework team and beyond. There is no way I can list them all, but here’s my best shot (not in any special order): Danny Simmons, Elisa Flasko, Noam Ben-Ami, Diego Vega, Kati Iceva, Srikanth Mandadi, Alex James, Jarek Kowalski, Jeff Derstadt, Rowan Miller, Craig Lee, David Annesley-DeWinter, Adi Unnithan, Andrew Peters, Shyam Pather, and Tim Laverty. Forgive me if I’ve neglected to mention someone. You’ll find that I have used (and recommended) a few additional tools throughout the book. The publishers generously provided me free licenses for which I’m grateful. The recommendations are because they are great tools, not because I didn’t have to pay for them. The tools include LINQPad, written by another O’Reilly author, Joseph Albahari; and ReSharper from JetBrains. ReSharper was my first line of defense for ensuring that my C# code wasn’t an embarrassment, while Wesley Bakker was my second. I learned so much from both of them. Entity Framework Profiler is an awesome tool for keeping track of what’s going on in your database when using Entity Framework. I also used two tools for producing images in this book. The first is Snagit from TechSmith, which was completely invaluable for capturing and editing screenshots. The second is Balsamiq Mockups, which enabled me to have a little fun creating mock-ups of application UIs in a number of chapters. Finally, thanks to Red Gate, a great company with many awesome tools. For this book, I used its .NET Reflector to inspect some assemblies, and I’ve used their SQL Packager for creating a simple-to-install version of the sample databases for you to use. My publisher has, as usual, provided great support for me. I had not one, but two editors—this is not the job of editing the book, but of counseling me and holding my hand throughout the process. Thanks to Laurel Ruma (who moved on to become O’Reilly’s über–Government 2.0 guru), and Mike Hendrickson who brings years of experience (not saying he’s old) for keeping me focused and helping me avoid being taken away in a funny white coat. I was also lucky to have Audrey Doyle as my copy editor again. She did an amazing job on the first edition, so I begged O’Reilly to contract her again. Lucky me, they did. (She is going to hate that last nonsentence; I dare you to leave it in, Audrey.) If you read the Preface of my first book, you’ll be happy to know that this time around I have no heart-wrenching pet losses to report, so you can put away the tissues you may have prepared yourself with. In fact, we adopted a teenage Newfoundland dog named Sampson just as I began to write this edition. Thank goodness for his needed afternoon walks and his constantly entertaining personality, without which I’d have gone completely mad during the time I have been writing this book. You can meet this silly boy on my blog at http://thedatafarm.com/blog/tags/Sampson.
Preface | xxxv
Download from Library of Wow! eBook
Somehow I have managed to retain my patient husband, Rich Flynn, to whom I promised “don’t worry, never again” when I finished the first edition. He has just suffered through another year of spaghetti, dirty dishes, ravaged potato chip supplies, and having to cede a little more space in bed as my waistline expanded thanks to my life in the computer chair (and all those potato chips). And finally, thanks to all of the incredible support that has come from the .NET community. I’m very proud of the first edition of the book, and each private “thank you” or complimentary public review on places like Amazon.com and your blogs has meant so much to me. This truly kept me going through what my Twitter followers know only too well was an arduous process in writing this second edition. Oh, and to anyone who gave me chocolate…thanks!
xxxvi | Preface
Download from Library of Wow! eBook
CHAPTER 1
Introducing the ADO.NET Entity Framework
At Microsoft’s November 2009 Professional Developer Conference, the legendary Don Box, a Distinguished Engineer at Microsoft, said, “If you’re a .NET developer Entity Framework is where we’re going. We’re there. Get on board, it’s time.” Yes, it’s time. Developers spend far too much of their precious time worrying about their backend database, its tables and their relationships, the names and parameters of stored procedures and views, as well as the schema of the data that they return. For .NET developers, Microsoft’s new Entity Framework changes the game so that you no longer have to be concerned with the details of the data store as you write applications. You can focus on the task of writing those applications, rather than accessing the data. The ADO.NET Entity Framework has shifted into becoming Microsoft’s core data access platform for building .NET applications. It was released in July 2008 as part of the Visual Studio 2008 Service Pack 1 and .NET 3.5 Service Pack 1, two years after Microsoft announced it at its TechEd 2006 Conference. As a version 1 product, Entity Framework was greeted at first with skepticism, and its adoption was far from sweeping. However, with the release of Visual Studio 2010 and .NET 4 in April 2010, a much improved Entity Framework finally got the attention and excited responses of many developers and .NET teams, who are now quickly jumping aboard. Although ADO.NET retains its existing data access, as Microsoft’s core data access strategy going forward the Entity Framework will receive the bulk of the innovation and resources from the Business Platform Division (which owns all of the data programmability tasks at Microsoft). It’s an important technology for Microsoft, and one that you should not ignore. Entity Framework is also being integrated into many of Microsoft’s products, whether the product uses Entity Framework to support its own features, such as with Commerce Server 2009’s Multi-Channel Commerce
1
Download from Library of Wow! eBook
Foundation,* or whether the product has support for interacting with the Entity Framework, such as with SQL Server Modeling. Why do we need a new data access technology? After forcing developers to switch from one data access technology to another—from DAO to RDO to ADO and then to ADO.NET—with ADO.NET Microsoft seemed to have finally settled on a single tool in which developers could invest. With each release of Visual Studio and the .NET Framework, ADO.NET has been enhanced and added to, but has remained backward compatible all along. Our investment has been safe. And it remains safe, even though it will be stagnant. The Entity Framework is another enhancement to ADO.NET, giving developers an added mechanism for accessing data and working with the results in addition to DataReaders and DataSets. But Microsoft went as far as it could with the DataSet paradigm. The next step was to enable developers to focus on a domain model while .NET would automate the redundant tasks of database interaction. In this chapter, you will learn about the critical pieces of the Entity Framework, the Entity Data Model, entity classes, the core .NET APIs, and Visual Studio design tools. You will also learn about how Entity Framework fits in with ADO.NET’s DataSets and LINQ to SQL. Finally, you will learn about many of the changes and additions to Entity Framework in Visual Studio 2010 and .NET 4, and how so many of the pain points in the first version have been eliminated.
The Entity Relationship Model: Programming Against a Model, Not the Database A central benefit of the Entity Framework is that it frees you from being concerned with the structure of your database. All of your data access and storage is done against a conceptual data model that reflects your own business objects. With ADO.NET DataReaders and many other data access technologies, you spend a lot of time writing code to get data from a database, read the results, pick out bits of data you want, and push them into your business classes. With the Entity Framework, you no longer query against the schema of a database, but rather against a schema that reflects your own business model. As data is retrieved, you are not forced to reason out columns and rows and push them into objects, because they are returned as objects. When it’s time to save changes back to the database, you have to save only those objects. The Entity Framework does the necessary work of translating your objects back into the rows and columns of the relational store. The Entity Framework does this part of the job for you, similar to the way an Object Relational Mapping (ORM) tool works.
* See http://msdn.microsoft.com/en-us/library/dd327929(v=CS.90).aspx.
2 | Chapter 1: Introducing the ADO.NET Entity Framework
Download from Library of Wow! eBook
The Entity Framework uses a model called an Entity Data Model (EDM), which evolved from Entity Relationship Modeling (ERM), a concept that has been used in database development for many years.
The Entity Data Model’s Roots Microsoft’s Entity Framework evolved from a methodology known as Entity Relationship Modeling (ERM), which has been trapped on whiteboards for more than 30 years. An ERM defines a schema of entities and their relationships with one another. Entities are not the same as objects. Entities define the schema of an object, but not its behavior. So, an entity is something like the schema of a table in your database, except that it describes the schema of your business objects. Developers have drawn ERMs for years to help us figure out how to transpose the structured tabular data of a database into business objects we can program against. No mention of ERM is complete without a nod to Dr. Peter Chen, who is credited with the first definitive paper on ERM in 1976: “The Entity-Relationship Model—Toward a Unified View of Data” (http://csc.lsu.edu/news/erd.pdf). With a host of database gurus in its ranks, Microsoft Research began to devise a way to automate the process of bridging a conceptual model and database schemas. And it needed to be a two-way street so that developers could retrieve data from the database, populate entities, and persist changes back into the database. In June 2006, Microsoft Research published its first paper on the EDM, its answer to ERM. The paper’s authors include database legend Jim Gray, who tragically disappeared while sailing off the coast of San Francisco Bay in 2007.
The Entity Data Model: A Client-Side Data Model An Entity Data Model (EDM) is a client-side data model and it is the core of the Entity Framework. It is not the same as the database model, which belongs to the database. The data model in the application describes the structure of your business objects. It’s as though you were given permission to restructure the database tables and views in your enterprise’s database so that the tables and relationships look more like your business domain rather than the normalized schema that is designed by database administrators. Figure 1-1 shows the schema of a typical set of tables in a database. PersonalDetails provides additional information about a Person that the database administrator has chosen to put into a separate table for the sake of scalability. SalesPerson is a table that is used to provide additional information for those who are salespeople. Working with this data from an application requires queries that are full of inner joins and outer joins to access the additional data about Person records. Or you will access a variety of predefined stored procedures and views, which might each require a different set of parameters and return data that is shaped in a variety of ways. The Entity Data Model: A Client-Side Data Model | 3
Download from Library of Wow! eBook
Figure 1-1. Schema of normalized database tables
A T-SQL query to retrieve a set of SalesPerson records along with their personal details might look something like this: SELECT FROM
SalesPerson.*, PersonalDetails.*, Person.* Person INNER JOIN PersonalDetails ON Person.PersonID = PersonalDetails.PersonID INNER JOIN SalesPerson ON Person.PersonID = SalesPerson.PersonID
Imagine that a particular application could have its own view of what you wish the database looked like. Figure 1-2 reshapes the schema.
Figure 1-2. Person data shaped to match your business objects
4 | Chapter 1: Introducing the ADO.NET Entity Framework
Download from Library of Wow! eBook
All of the fields from PersonalDetails are now part of Person. And SalesPerson is doing something that is not even possible in a database: it is deriving from Person, just as you would in an object model. Now imagine that you can write a LINQ query that looks like this: from p in People.OfType select p
In return, you will have a set of SalesPerson objects with all of the properties defined by this model (see Figure 1-3).
Figure 1-3. The SalesPerson object
LINQ exists only in the C# and Visual Basic languages. With the Entity Framework there is another way to express queries, which not only allows you to use other languages, but also provides additional benefits that you can take advantage of as necessary. It’s called Entity SQL, and you will learn much more about it and LINQ to Entities in Chapters 3 through 5.
This is the crux of how the Entity Framework can remove the pain of having not only to interact with the database, but also to translate the tabular data into objects. .NET is but one tool that uses an EDM. The next version of SQL Server will use an EDM for Reporting Services and you will soon begin to see other Microsoft applications that will adopt the EDM concept as well. In fact, you will find that model-driven development in general is getting more and more attention from Microsoft. When working with the Entity Framework, you will implement an EDM that is particular to the Entity Framework. In the Entity Framework, an EDM is represented by a single XML file at design time that is split into a set of three XML files at runtime, The Entity Data Model: A Client-Side Data Model | 5
Download from Library of Wow! eBook
only one of which represents a conceptual model. The other two provide metadata that enables Entity Framework to interact with a database. You’ll learn much more about this metadata in Chapter 2.
Entities: Blueprints for Business Classes The items described by an EDM are known as entities. Classes that are generated from the model entities, along with their instantiated object, are also referred to as entities but are often called entity classes or entity objects. The generated entity classes differ from typical business classes in that they have properties but no behavior apart from methods to enable change tracking. Figure 1-4 shows the class diagram for the Person and SalesPerson classes that the model shown in Figure 1-2 would generate automatically. Each class has a factory method (e.g., CreatePerson) as well as methods used to notify the Entity Framework when a property changes. With the classes the Entity Framework generates, you can add your own business logic, pull the results into business objects of your own, and even link your business objects to the EDM, replacing the generated classes. But by definition, the entity classes describe only their schema. In addition to being able to reshape the entities in a data model as with the inheritance hierarchy shown in Figure 1-2, you can define relationships between entities. Figure 1-5 adds a Customer entity to the model which also derives from Person as well as an Order entity. Notice the relationship lines between SalesPerson and Order, showing a one-to-many relationship between them. There is also a one-to-many relationship between Customer and Order. When you write queries against this version of the model, you don’t need to use JOINs. The model provides navigation between the entities. The following LINQ to Entities query retrieves order information along with information about the customer. It navigates into the Customer property of the Order to get the FirstName and LastName of the Customer. from o in context.Orders select new {o.OrderID,o.OrderNumber,o.Customer.FirstName,o.Customer.LastName}
Once that data is in memory, you can navigate through each object and its properties, such as myOrder.Customer.LastName, just as readily. The Entity Framework also lets you retrieve graphs, which means you can return shaped data such as a Customer with all of its Order details already attached. These are some of the major benefits to querying against a data model, rather than directly against the database.
6 | Chapter 1: Introducing the ADO.NET Entity Framework
Download from Library of Wow! eBook
Figure 1-4. Class diagrams for the Person and SalesPerson entities
The Backend Database: Your Choice You may have noticed that I have not mentioned the actual data store that owns the data being queried. The model doesn’t have any knowledge of the data store—what type of database it is, much less what the schema is. And it doesn’t need to. The database you choose as your backend will have no impact on your model or your code. The Entity Framework communicates with the same ADO.NET data providers that ADO.NET already uses, but with a caveat. The provider must be updated to support the Entity Framework. The provider participates in reshaping the Entity Framework’s
The Backend Database: Your Choice | 7
Download from Library of Wow! eBook
Figure 1-5. SalesPerson and Customer entities, each with a relationship to Order entities
queries and commands into native queries and commands. All you need to do is identify the provider and a database connection string so that the Entity Framework can get to the database. This means that if you need to write applications against a number of different databases, you won’t have to learn the ins and outs of each database. You can write queries with the Entity Framework’s syntax (either LINQ to Entities or Entity SQL) and never have to worry about the differences between the databases. If you need to take advantage of functions or operators that are particular to a database, Entity SQL allows you to do that as well.
Database Providers Microsoft’s SqlClient APIs that are included with Visual Studio 2008 SP1 and Visual Studio 2010 support the Entity Framework. They will allow you to use SQL Server 2000, 2005, and 2008. You can use the full or Express version of SQL Server 2005 and 2008 and the full version of SQL Server 2000. Note that the Entity Data Model Designer cannot interact with SQL Server 2000. This is not a limitation of Entity Framework’s design tools but Visual Studio 2010 itself. None of Visual Studio’s tools recognizes SQL 8 | Chapter 1: Introducing the ADO.NET Entity Framework
Download from Library of Wow! eBook
Server 2000. However, the Entity Framework runtime can. SQL Server CE version 3.5 and 4 support the Entity Framework as well. Check out the July 7, 2010, blog post from the SQL Server CE team about SQL Server Compact 4 at http://blogs.msdn.com/sqlser vercompact. At the time of this writing, a host of other providers are available—and more are on the way—that will allow you to use Oracle, IBM databases, SQL Anywhere, MySQL, SQLite, VistaDB, and many other databases. The providers are being written by the database vendors as well as by third-party vendors. Many of these providers were written for .NET 3.5. There is only one critical feature that they will not support until they have been updated to .NET 4: a feature called model first, which you will learn about in Chapter 25. Microsoft lists providers on the “ADO.NET Data Providers” page of the Data Developer Center at http://msdn.microsoft.com/en-us/data/ dd363565.aspx.
Microsoft provides guidance for developers who want to build Entity Framework support into their database providers. I will not be covering this topic in this book. You can see EF team blog posts about writing providers at http://blogs.msdn.com/b/adonet/archive/tags/sample+pro vider.
Access and ODBC A provider that supports the Entity Framework needs to have specific knowledge about the type of database it is connecting to. It needs to be aware of the available functions and operators for the database, as well as the proper syntax for native queries. Open Database Connectivity (ODBC) providers provide generic access to a variety of databases, including Access, and cannot furnish the necessary database particulars to act as a provider for the Entity Framework. Therefore, ODBC is not a valid provider for the Entity Framework. Unless someone creates a provider specifically for Access, you won’t be able to use it with Entity Framework applications. Microsoft does not have plans to build an Access provider, because the demand is too low.
Entity Framework Features: APIs and Tools In addition to the EDM, the Entity Framework provides a set of .NET runtime APIs that let you write .NET applications using the EDM. It also includes a set of design tools for designing the model. Following is a synopsis of the Entity Framework’s key features.
Entity Framework Features: APIs and Tools | 9
Download from Library of Wow! eBook
Metadata Although the Entity Framework is designed to let you work directly with the classes from the EDM, it still needs to interact with the database. The conceptual data model that the EDM describes is stored in an XML file whose schema identifies the entities and their properties. Behind the conceptual schema described in the EDM is another pair of XML files that map your data model back to the database. One is an XML file that describes your database and the other is a file that provides the mapping between your conceptual model and the database. During query execution and command execution (for updates), the Entity Framework figures out how to turn a query or command that is expressed in terms of the data model into one that is expressed in terms of your database. It does this by reading the metadata. When data is returned from the database, it uses the metadata to shape the database results into the entities and further materializes objects from those results. Entity Framework acquires the ability to use an in-memory model with a feature called code first that is part of the Entity Framework Community Technical Preview (CTP). It is not yet part of Entity Framework and must be downloaded separately. Code first allows you to work solely with classes, and the necessary XML metadata is generated in memory on the fly at runtime. You’ll learn more about this feature in Chapter 25, but be aware that at the time of this book’s publication, code first is still a CTP and is not yet fully developed.
Entity Data Model Design Tools The screenshots in Figures 1-2 and 1-3 are taken from the EDM Designer. It is part of Visual Studio and provides you with a way to work visually with the model rather than tangle with the XML. You will work with the Designer right away in Chapter 2, and you’ll learn how to use it to do some more advanced modeling, such as inheritance, in Chapter 14 and “model-first” design in Chapter 25. You will also learn about the Designer’s limitations, such as the fact that it does not support all of the features of the EDM. With some of the less frequently used EDM features, you’ll have to work directly with the XML after all. In Chapter 2, you will get a look at the XML and how it relates to what you see in the Designer so that when it comes time to modify it in Chapter 15, you’ll have some familiarity with the raw schema files. In Visual Studio 2010, the Designer supports many more features than it did in Visual Studio 2008 SP1. However, as you will see in Chapters 14 and 15, there are still some things you will need to do manually.
10 | Chapter 1: Introducing the ADO.NET Entity Framework
Download from Library of Wow! eBook
The Designer also allows you to map stored procedures to entities, which you’ll learn about in Chapter 6. If you are coming from Visual Studio 2008 SP1, you’ll find that the Designer’s stored procedure support has been greatly improved in Visual Studio 2010. Another notable feature of the Designer is that it will let you update the model from the database to add additional database objects that you did not need earlier or that have been added to the database since you created the model.
Database-first design One of the EDM design tools is the Entity Data Model Wizard. It allows you to point to an existing database and create a model directly from the database so that you don’t have to start from scratch. Once you have this first pass at the model, you can begin to customize the model in the Designer.
Model-first design Not every development project begins with a legacy database. One of the new features in Visual Studio 2010 is the ability to create a model directly in the Designer and then generate database schema based on that model. Although we’ll focus on a model created using database-first design through most of this book, you’ll get a chance to drill into model-first design, as well as some additional Designer features, in Chapter 25.
Code generation Once you have a model to define your domain entities, you’ll need classes to represent them at runtime. The Designer automatically generates those classes for you from the model. However, we’ve gained another critical feature in Visual Studio 2010. Rather than using the proprietary code generator that was written for Entity Framework in Visual Studio 2008 SP1, the classes are generated using Visual Studio’s Text Template Transformation Toolkit (T4). Not only does this provide a more commonly known mechanism for code generation, but also you can much more easily customize the provided templates to define exactly how you would like classes to be generated from your model. You’ll learn about the code generation capabilities beginning with Chapter 11 and work further with T4 customization in later chapters. There are some scenarios where you will be able to skip code generation completely and simply use your own classes.
Object Services The Entity Framework runtime’s most prominent feature set and that which you are likely to work with most often is referred to as Object Services. Object Services sits on top of the Entity Framework stack, as shown in Figure 1-6, and provides the functionality needed to work with objects that are based on your entities. Object Services provides a class called EntityObject and can easily manage any class that inherits from EntityObject. This includes materializing objects from the results of queries against the Entity Framework Features: APIs and Tools | 11
Download from Library of Wow! eBook
Figure 1-6. The Entity Framework stack
EDM, keeping track of changes to those objects, managing relationships between objects, and saving changes back to the database. In between querying and updating, Object Services provides a host of capabilities to interact with entity objects, such as automatically working with a lower level of the Entity Framework to do all of the work necessary to make calls to the database and deal with the results. Object Services also provides serialization (both XML and binary).
POCO Support One of the most important runtime enhancements in .NET 4 is the ability for the Entity Framework to manage entities that do not inherit from EntityObject. This is Entity Framework’s new POCO (Plain Old CLR Objects) support, and you will learn much more about this feature beginning with Chapter 13. POCO support is critical to enable a variety of different programming styles. With POCO entities, developers can more easily build unit tests as well as persistent ignorant entity classes. These capabilities are crucial to developers who follow the coding patterns recommended by domain-driven and agile development. These same developers were unable to use the Entity Framework in .NET 3.5. Now Entity Framework embraces a wider population of the development community.
Change Tracking Once an entity object has been instantiated, either as a result of data returned from a query or by instantiating a new object in code, Object Services can keep track of that object. This is the default for objects returned from queries. When Object Services manages an object, it can keep track of changes made to the object’s properties or its relationships to other entity objects. 12 | Chapter 1: Introducing the ADO.NET Entity Framework
Download from Library of Wow! eBook
Object Services then uses the change-tracking information when it’s time to update the data. It constructs Insert, Update, and Delete commands for each object that has been added, modified, or deleted by comparing the original values to the current values of the entity. If you are using stored procedures in conjunction with entities it will pass the current values (and any original values specifically identified) to those procedures.
Relationship Management and Foreign Keys Relationships are a critical piece of the EDM; however, in .NET 4, an important new feature was added: foreign key support. In a classic Entity Relationship Model, foreign keys are not exposed in the conceptual model. Entity Framework followed this paradigm in .NET 3.5 SP1, but we developers still wanted access to those foreign key values for many reasons. In fact, in the first edition of this book, I showed a variety of ways to go “under the covers” to get and set foreign keys. Now Entity Framework supports having the foreign keys in the conceptual model. However, for backward compatibility, you can still use the former mechanism which, because of the lack of foreign keys, instantiates relationships as objects. I will not spend a lot of time focusing on the older style of building relationships in this book as it is not the default and will be used minimally. If you need in-depth guidance on how to work with relationships when the foreign key is not available in the conceptual model, I recommend that you read the first edition of this book.
As you will find, especially in Chapter 19, which dives deep into relationships, even with foreign keys, you will need to have a very good understanding of how relationships work. Some of the rules of engagement when working with related data are not very intuitive, and you can write code that will raise plenty of exceptions, or worse, will return invalid results, if you break these rules. Chapter 19 will provide insight into relationships in the EDM so that you will be able to work with them in an expert manner.
Data Binding You can use entities in many .NET data binding scenarios. In Windows Forms and WPF, you can use entities as a data source for data-bound controls or as the data source for BindingSource controls, which orchestrate the binding between objects and UI controls on the form. Chapter 9 provides a well-informed walkthrough for using entities with BindingSource controls to edit and update data. Chapter 26 focuses on separating the data access and other business logic from the user interface to provide better architecture for your applications.
Entity Framework Features: APIs and Tools | 13
Download from Library of Wow! eBook
Chapter 9 also provides a walkthrough for data-binding entities in Windows Presentation Foundation (WPF) applications. Visual Studio 2010 introduced a host of enhancements for data binding in WPF, and you’ll benefit greatly from these when data-binding with entities. For ASP.NET, there is a DataSource control called EntityDataSource that works in a similar way to the ASP.NET SqlDataSource and LinqDataSource controls, allowing you to declaratively bind entity objects to your user interface. Chapter 12 is all about using EntityDataSource. You’ll also get a quick look at binding with ASP.NET Dynamic Data in that chapter.
n-Tier Development Entity Framework made significant advancements for n-tier development in .NET 4. In .NET 3.5 SP1, it was just too hard; as such, in the previous edition of this book I devoted a lot of pages to hacks and workarounds. Now we can benefit greatly from not only the foreign keys but also a slew of state management methods that make working across processes much simpler. Additionally, POCOs make n-tier development easier to achieve as you’ll see in the final chapters of this book. For layered applications, Chapter 24 and Chapter 25 focus on pulling all of the data access tasks out of the ASP.NET user interface, and you’ll see a WPF application, an ASP.NET Web Forms application, and an ASP.NET MVC application using various patterns to separate your logic.
EntityClient EntityClient is the other major API in the Entity Framework, though one that you are
less likely to work with directly. It provides the functionality necessary for working with the store queries and commands (in conjunction with the database provider) connecting to the database, executing the commands, retrieving the results from the store, and reshaping the results to match the EDM. You can work with EntityClient directly or work with Object Services, which sits on top of EntityClient. Not only is EntityClient able to perform queries, but it does this on behalf of Object Services. The difference is that when you work directly with EntityClient, you will get tabular results (though the results can be shaped). If you are working with Object Services, it will transform the tabular data created by EntityClient into objects. The tabular data returned by EntityClient is read-only. EntityClient is well suited for reporting and moving data from one persistence mechanism to another. Only Object Services provides change tracking and the ability to save changes back to the data store.
14 | Chapter 1: Introducing the ADO.NET Entity Framework
Download from Library of Wow! eBook
The Entity Framework and WCF Services You can use the Entity Framework anywhere you can use ADO.NET, including web services and WCF services. Chapters 17 and 18 walk you through the process of providing services for EntityObject entities and POCO entities. In these chapters, we’ll also take a look at WCF Data Services, WCF RIA Services, and a specialized POCO template called Self-Tracking Entities, which provides client-side change-tracking capabilities to entities, thereby allowing a simpler way to send changes to WCF services and then persist them to the database.
What About ADO.NET DataSets and LINQ to SQL? The Entity Framework is only the latest addition to the ADO.NET stack. How does that impact existing code that uses DataSets and DataReaders or LINQ to SQL? Can you continue to write new code using these technologies?
DataSets DataSets and DataReaders are not going away. All of your existing investment will con-
tinue to function and you can continue to use this methodology of retrieving data and interacting with it. The Entity Framework provides a completely different way to retrieve and work with data. You would not integrate the two technologies—for example, using the Entity Framework to query some data, and then pushing it into a DataSet; there would be no point. You should use one or the other. As you learn about the Entity Framework, you will find that it provides a very different paradigm for accessing data. You may find that the Entity Framework fits for some projects, but not others, where you may want to stick with DataSets. The Entity Framework uses DataReaders as well in the form of an EntityDataReader, which inherits the same DbDataReader as SqlDataReader. This is what a query with EntityClient returns. In fact, you’ll find that the code querying the EDM with EntityClient looks very similar to the code that you use to query the database directly with ADO.NET. It uses connections, commands, and command parameters, and returns a DbDataReader that you can read as you would any other DataReader, such as SqlDataReader. Some ADO.NET tools that are not available with the Entity Framework are query notification and ASP.NET’s SqlCacheDependency. Additionally, ADO.NET’s SqlBulkCopy requires a DataReader or DataSet to stream data into the database; therefore, you cannot do client-side bulk loading with the Entity Framework. The Entity Framework does not have an equivalent to ADO.NET’s DataAdapter.BatchUpdate. Therefore, when the Entity Framework saves changes to the database, it can send only one command at a time.
What About ADO.NET DataSets and LINQ to SQL? | 15
Download from Library of Wow! eBook
LINQ to SQL LINQ to SQL and the Entity Framework look similar on the surface. They both provide LINQ querying against a database using a data model. A frequently asked question is: why did Microsoft create two similar technologies? LINQ to SQL evolved from the LINQ project, which came out of team working with language development. The Entity Framework was a project of the Data Programmability team and was focused on the Entity SQL language. By the time each technology had come along far enough that it was being shown to other teams at Microsoft, it was clear that Microsoft had two great new technologies that could target different scenarios. The Entity Framework team adapted LINQ to work with entities, which confused developers even more because LINQ to Entities and LINQ to SQL look so much alike. LINQ to SQL eventually was brought into Microsoft’s Data Programmability team, and in November 2008 the team announced that because the technologies target the same problems, going forward they would focus on developing the Entity Framework, which supports multiple databases and aligns with many of Microsoft’s upcoming technologies through its use of an Entity Data Model. However, they will continue to maintain and tweak LINQ to SQL. This is not a happy situation for many developers who have made an investment in LINQ to SQL. Microsoft is committed to maintaining LINQ to SQL in ADO.NET and has made no statements regarding deprecating it. It has also promised to provide a migration path from LINQ to SQL to the Entity Framework and will recommend the Entity Framework over LINQ to SQL in future programmer guidelines.
Entity Framework Pain Points Are Fading Away In the first edition of this book, Chapter 1 listed two pages of pain points. Their section titles were “The Entity Framework Designer” (which focused on the lack of support for stored procedures and other EDM features), “Challenges with Change Tracking Distributed Applications,” “Domain-Driven Development,” and “Unit Testing.” I’m very happy to have removed every one of these sections thanks to the great improvements that have been made in .NET 4 and Visual Studio 2010. There are still definitely a lot of nits to pick, however. The model would benefit from support for things such as unique foreign keys, table-valued functions, enhanced manyto-many relationships, and a problem that is much more than a nit: support for very large models. Entity Framework’s state management and relationship management still have a lot of behavior that is not intuitive and will certainly bite you if you don’t study up on it. Take a look at Chapter 19 for a good study guide.
16 | Chapter 1: Introducing the ADO.NET Entity Framework
Download from Library of Wow! eBook
This book spends plenty of time looking into the depths of the Entity Framework runtime to show you how to get around some of these limitations, and attempts to point out potholes, hiccups, and omissions. Users of more mature ORM tools continue to have complaints about Entity Framework as well, such as the difficulty of providing internal transactions (database transactions, however, are supported). But if you look around the marketplace, even Entity Framework’s staunchest competitors are getting on board and leveraging their experience to provide advanced tools for working with Entity Framework. The principal player, NHibernate, created a wonderful database profiling tool for Entity Framework, and LLBLGen Pro has built a powerful designer for Entity Framework that takes a very different approach for managing an Entity Framework EDM and its metadata (http:// www.llblgen.com).
Programming the Entity Framework As you read through this book, you will gain experience in designing EDMs and using the Entity Framework to write applications, as well as dig deep into the APIs to learn how to manipulate entity objects and have granular control over much of their behavior. A lot of functionality is very accessible, and there’s a lot of hidden power. You will learn what’s under the covers so that you can realize the true benefits of the Entity Framework. Even as I wrap up this edition of Programming Entity Framework, I look forward to future versions of the framework as it continues to evolve.
Programming the Entity Framework | 17
Download from Library of Wow! eBook
Download from Library of Wow! eBook
CHAPTER 2
Exploring the Entity Data Model
An Entity Data Model (EDM) is the bridge between your application and your data store. An EDM provides you with the ability to work with a conceptual view of your data rather than the actual database schema. .NET APIs provided by the Entity Framework use an EDM for every interaction with the data store, whether it is to retrieve or to save data. The Entity Framework tools generate classes from this model that enable you to work with objects described by the EDM. In this chapter, you will create a simple EDM using the Entity Data Model Wizard, and then you will inspect the model both in the Designer and by looking at its raw XML. This chapter will stick to the basics of the model so that you can become familiar with how an EDM is structured and how the most common elements relate to one another, to your code, and to the database. In Chapter 14, you will begin to explore the more complex aspects of the EDM, such as its different inheritance capabilities and how to customize models so that they can better reflect your business logic.
Why Use an Entity Data Model? Well-designed databases can pose a problem for developers. In the data world, a database is designed for maintainability, security, efficiency, and scalability. Its data is organized in a way that satisfies the demands of good database design, yet provides challenges for the developer who needs to access that data.
19
Download from Library of Wow! eBook
Entity Data Model is a concept. The Entity Framework has a particular implementation that is realized as the EDMX file at design time. At runtime, the EDMX file is broken up into three separate XML files. For the sake of clarity, this book will simply refer to the EDM or Entity Data Model (or simply “the model”) when discussing the Entity Framework’s implementation. But keep in mind that the EDM literally refers to the concept of using some type of model to represent your entities in an application.
The EDM follows the concept of Entity Relationship Modeling discussed in Chapter 1, but in the Entity Framework, it moves the model into XML files that are used by the Entity Framework runtime. With an EDM in place, developers can focus on their business objects even when retrieving data from the database or persisting it back to the database. You, the developer, will not have to worry about the structure of the database, the names of the tables or views, or the names of stored procedures or their required parameters. Nor will you have to create the objects necessary for making connections to the database, or be concerned with the schema of the returned data and then transform the results into objects to use in your code. You will simply work against your conceptual model and the classes that represent the model’s entities. And when you do so within the scope of the Entity Framework, the Entity Framework runtime will handle database connections, database command generation, query execution, object materialization, and the details of persisting changes back to the database.
The EDM Within the Entity Framework In the Entity Framework’s implementation of the EDM, the primary XML file represents the conceptual model, which is the actual EDM. A second XML file represents the database schema, and a third represents the mapping between the first two. At design time, all three files are bundled into a single EDMX file. The build process splits the EDMX out into the three metadata files that are used at runtime. The Entity Framework then provides a framework that allows developers to write .NET applications based on this model. In Chapter 25, you will learn about alternatives to the XML schema that are included in future technologies coming from Microsoft that will enhance the Entity Framework.
20 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
As long as the EDM provides the conceptual schema, a representation of the database, a mapping file, and access to an Entity Framework-aware ADO.NET provider for the target database, the Entity Framework doesn’t care what database is being targeted. It provides a common means of interacting with the database, common query syntax, and a common method for sending changes back to the database. Although the Entity Framework provides a very rich set of features for developers, its most important capabilities are the following: • By default, it automatically generates classes from the model and updates those classes dynamically anytime the model changes. • It takes care of all of the database connectivity so that developers are not burdened by having to write lots of code for interacting with the database. • It provides common query syntax for querying the model, not the database, and then translates these queries into queries that the database can understand. • It provides a mechanism for tracking changes to the model’s objects as they are being used in applications, and handles the updates to the database. In addition, because the model’s classes are dynamically generated, minor changes to the model need not have a major impact on your application. Furthermore, modifying the model is much simpler than modifying your objects and the data access code on which they rely. All of the work you will do in this book will depend on an EDM, so in preparation for this, we’ll create a simple model and then put it under a microscope so that you’ll have a thorough comprehension of what the Entity Framework is working with.
Walkthrough: Building Your First EDM Let’s start by creating a model from the sample database, ProgrammingEFDB1. This is a simple database with only two tables, one view, and some stored procedures, and therefore it’s a great place to begin. With an EDM in hand, you’ll be able to explore its elements and their relationships, which we’ll do later in this chapter. This walkthrough will use a custom SQL Server database, ProgrammingEFDB1, which you can download from the book’s website at http: //www.learnentityframework.com. Visual Studio 2010 provides Entity Framework connectivity to SQL Server. As mentioned in Chapter 1, you can install additional providers to connect to other databases, such as SQL Server CE, MySQL, Oracle, and VistaDB.
Walkthrough: Building Your First EDM | 21
Download from Library of Wow! eBook
1. Create a new Console Application project by choosing the Console Application project template (see Figure 2-1). I’ve named mine Chapter2ConsoleApp. Be sure that the project is a .NET Framework 4 project. You can see the filter option at the top of the New Project window. Many of the features throughout this book are not available in .NET 3.5 and you will find yourself very confused!
Figure 2-1. Creating a new Console Application project
2. Add a new item to the project by right-clicking on Chapter2ConsoleApp in the Solution Explorer, clicking Add, and then clicking New Item. 3. Select ADO.NET Entity Data Model from the Templates list and click Add (see Figure 2-2). 4. On the Choose Model Contents page, select the Generate from Database option and click Next. 5. On the Choose Your Data Connection page, select ProgrammingEFDB1 from the drop-down list of available connections. If you do not have ProgrammingEFDB1 set up as a database connection in Visual Studio, click New Connection to open the Connection Properties dialog and create a new connection to the database.
22 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
Figure 2-2. Selecting ADO.NET Entity Data Model on the Add New Item page to create an EDM
6. At the bottom of this page, change the connection settings name from the lengthy default of “ProgrammingEFDB1Entities” to “SampleEntities” and then click Next. 7. On the Choose Your Database Objects page, check the Tables and Views nodes. This will select all of the tables and views in the database. Alternatively, you can expand any of the nodes and select the specific objects you want. This database has two tables (Contact and Address), one view (vOfficeAddresses), and six stored procedures. For this demo, you’ll want only the tables and the view. We are skipping over the Stored Procedures checkbox for now; we’ll come back to stored procedures in Chapter 7.
8. At the bottom of this page, change the Model Namespace from its default to “SampleModel” to align with the connection settings name. 9. Click Finish. The new model will be displayed in the Designer window, and its file, Model1.edmx, will appear in the Solution Explorer (see Figure 2-3).
Walkthrough: Building Your First EDM | 23
Download from Library of Wow! eBook
Figure 2-3. Model1.edmx added to the project, and the model automatically opened in the Designer Beginning with Visual Studio 2010, the Entity Framework also supports model-first design, whereby you can build a model from scratch and then create a database based on the model. We’ll cover model-first design in Chapter 25.
Inspecting the EDM in the Designer Window The Entity Designer window is useful for viewing a graphical representation of an EDM and its members. Otherwise, you would have to dizzy yourself with lots of raw XML, which you’ll get an opportunity to do later in this chapter, after you have had your graphical introduction. After you generate the model from the wizard, the model will be open in the Designer view. If you have closed it, you can double-click on the EDMX file in the Solution Explorer to reopen it. The designer view is the default view for an EDMX file. The Designer display of Model1.edmx shown in Figure 2-3 depicts an EDM that consists of three entities: a Contact entity, an Address entity, and a vOfficeAddress entity. The first two came from the tables in the database and the third came from the view. The Designer also displays a line connecting Contact and Address that represents a one-tomany relationship between them. Each entity has a number of scalar properties, and the entities with relationships additionally have navigation properties. Scalar properties are properties whose values are literally contained in the entity. For example, the Contact entity is described by such things as ContactID, FirstName, LastName, and Title. These correspond with the table columns.
24 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
Navigation properties are pointers to related entities. The Contact entity has an Addresses property that will enable the application to navigate from a Contact to a set of Addresses related to that Contact. The Address entity has a Contact property that allows you to navigate from an Address to the single Contact associated with the Address entity. The lines that connect the related entities represent associations, which are the relationships between the entities. Be aware that the position of the association ends, which in Figure 2-3 are nearest to Contact.LastName and Address.StateProvince, has no specific meaning. The association is only connecting the entities and is not implicating any particular properties.
Navigations and Associations: What to What? The two ends of a relationship are often described with shortcut syntax that defines how many entities can be on each end. This is referred to as describing the multiplicity of the end. Multiplicity is also known as relationship cardinality, though you won’t see this term used much within the Entity Framework. The multiplicity options are: • 1 (One) • * (Many) • 0..1 (Zero or One) The two ends are then combined to describe the relationship. For example, “1:*” means “One to Many.” A canonical example of this is one order and its many line items. “0..1:*” means “‘Zero or One’ to Many.” An example of this is a relationship between shippers and orders. One shipper may ship many orders, but only one shipper can be related to an order. However, it’s possible that the shipper was not assigned to the order at first; therefore, it can be zero or one on the shipper end of the relationship.
Notice that the entity has both a scalar property for the ContactID as well as a navigation property to the Contact entity referenced by the ContactID. If you have been working with the previous version of Entity Framework, the presence of the foreign key (ContactID) is new. It is optional, but it is there by default. You’ll read more about this later in the chapter.
When working in the Entity Designer, you can see more information about the container, each entity, and each entity property in the Visual Studio IDE’s Properties window.
Inspecting the EDM in the Designer Window | 25
Download from Library of Wow! eBook
Entity Container Properties The logical group of entities in a model is called an entity container. Figure 2-4 shows the Properties window for the entity container with some of the sections collapsed. Here you can modify the names of the container and its namespace, define the model’s pluralization rules, and more.
Figure 2-4. The Properties window for the entity container
Entity Properties Each entity and each association of an EDM, as well as the model itself, has properties. Let’s look at some properties of the Contact entity in the model that you’ve created. Select the Contact entity to view its Properties window (see Figure 2-5).
26 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
Figure 2-5. Viewing the Properties window for the Contact entity
In the Properties window, you can see that the entity not only has the name “Contact,” which it derived from the table name in the database, but also has an Entity Set Name property. If the table name in the database had been plural, e.g., Contacts, the wizard would have still named the entity Contact because an entity name should be singular. An entity set is a container for a collection of entities of a single type. Therefore, the entity set named “Contacts” will contain a collection of Contact entities. By default, the wizard pluralized the entity name when creating the entity set name. You can change this behavior by unchecking the “Pluralize or singularize generated object names” checkbox in the Entity Data Model Wizard.
Entity Property Properties Figure 2-6 displays the properties of the Contact’s FirstName property. You can see, for example, that FirstName is a string (Type is String) that is not nullable (Nullable is False).
Inspecting the EDM in the Designer Window | 27
Download from Library of Wow! eBook
Figure 2-6. The properties of the FirstName property
Properties that describe the schema of an entity property, such as Fixed Length, are also known as attributes. Because it can be confusing to discuss “the properties of the properties,” I will frequently refer to them as attributes.
The Unicode, Max Length, and Fixed Length properties are ignored by the Entity Framework runtime. Do not expect the Entity Framework to automatically perform validation based on these properties. These attributes are used by other consumers of the EDM, such as ASP.NET MVC 2.0 and ASP.NET Dynamic Data Controls, and, as you will learn in Chapter 25, for generating database scripts along with the StoreGeneratedPat tern property. You can use them yourself when working at a lower level with Entity Framework; e.g., with MetadataWorkspace, which you will learn about in Chapter 21. Although you can do much more with the Designer, it is time to open the model in its raw format. You will find additional discussion of the raw XML in Appendix C. Be sure to save all of your work before moving on.
28 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
Figure 2-7. The components of the Entity Framework’s model metadata When building a model from scratch in the Designer in Chapter 25, you will learn more about various features of working with entities, their properties, associations, and more.
The Model’s Supporting Metadata So far in the Designer you have seen only the conceptual portion of the model, but there are more critical sections of the EDMX: StorageModels and Mappings. There are, in fact, four sections in the EDMX file, but one of those four contains instructions to the Designer for object placement. I’ll be ignoring that section in this discussion.
These additional parts of the metadata enable the Entity Framework APIs to translate between the conceptual model and the actual data store. The StorageModels represent the schema of the database objects that you selected for inclusion, and the Mappings describe how to get from the entities and properties of the conceptual model to the tables and columns described in the storage model (see Figure 2-7). Why use the storage layer to represent the data store when you have the actual data store to work with? There are a number of reasons to use this piece of the model. The most important reason is that this provides loose coupling to the database; not every object in the database needs to be in the model, and as you will learn in Chapter 16, it is possible to customize even the storage layer to suit the needs of the model. Although the entire model is contained in a single file at design time, when the project is compiled it will create three separate files—one for each of these sections. The conceptual layer is saved to a file with a .csdl extension, which stands for Conceptual Schema Definition Language. The storage layer is saved to a file with an .ssdl extension (which stands for Store Schema Definition Language) and the mapping layer is saved to a file with an .msl extension (which stands for Mapping Specification Language). The Model’s Supporting Metadata | 29
Download from Library of Wow! eBook
These files are used at runtime, which is why they are contained in a section called edmx:Runtime in the model. By default, you will never see these physical files because they are embedded into the project assembly when the project is compiled. This is convenient for a lot of scenarios, though it is possible to change the model’s Metadata Artifact Processing property to read “Copy to Output Directory.”
A Schema by Any Other Name: Nicknames The three parts of the model have a variety of descriptions that you will see used in documentation, articles, training, and even this book. Here is a list of the various “nicknames”: Conceptual Schema Definition Language (CSDL)
Store Schema Definition Language (SSDL)
Mapping Specification Language (MSL)
•
Conceptual layer
•
Conceptual schema
•
Conceptual model
•
C-side
•
Store/storage layer
•
Store/storage schema
•
Store/storage model
•
Store/storage metadata
•
Store/storage metadata schema
•
S-side
•
Mapping layer
•
Mapping specification
•
C-S mapping (referring to “conceptual to store”)
Each section is controlled by its own XML Schema Definition (XSD) file that lives deep within the .NET Framework files. One schema file defines what the structure of the CSDL should be, another defines the MSL, and yet another defines the SSDL. Visual Studio’s IntelliSense uses these schema files to help you as you’re working directly with the XML, pointing out errors and presenting you with options. Compiler errors will also be displayed if the files don’t fall in line with their schema rules.
30 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
Schemas for the Schemas If you’re among the very curious you can take a look at the schema files that drive the rules for the CSDL, SSDL, and MSL sections in the model. The schema files for Visual Studio 2010 are located in C:\Program Files\Microsoft Visual Studio 10.0\xml\Schemas. If you are running a 64-bit O/S, check in the Program Files (x86) path instead. The three files to look for are: • System.Data.Resources.CSDLSchema_2.xsd • System.Data.Resources.CSMSL_2.xsd • System.Data.Resources.SSDLSchema_2.xsd If you open these in Visual Studio, you will see that they are formatted as XML files and are easy to navigate.
Viewing the Model in the Model Browser The Entity Data Model Designer also provides another view of the metadata with the Model Browser. You can access the Model Browser from the context menu of the model’s design surface. Figure 2-8 shows the Model Browser with a number of items expanded. The Model Browser lets you see an overview of the CSDL and SSDL items. From here you can access the property window of various entities and properties. You can also navigate to a particular entity or property by right-clicking and selecting Show in the Designer. As your model gets more complex, the Model Browser is a convenient way to see an organized view of the metadata.
Viewing the Model’s Raw XML Now it’s time to get down and dirty with the EDM. Only a portion of the model is visible in the Designer, which means you can learn a lot more by looking at it in its raw format. In places where the model has a counterpart in the Designer, you’ll see both views. By default, the file will open in the Designer; therefore, you need to use a different method to open it in its raw format. In the Solution Explorer, right-click the Model1.edmx file. From the context menu that opens, select Open With, and then choose XML Editor and click OK. Visual Studio cannot display the model in Design view and in XML at the same time, so you will see a message asking whether it’s OK to close the Design view of the model. Click Yes.
Viewing the Model’s Raw XML | 31
Download from Library of Wow! eBook
Figure 2-8. The Model Browser For those who have the common Fear-of-XML syndrome, this may look at little daunting at first. Have no fear and just follow along. We won’t go too deep, but if you are interested in the real plumbing, you’ll find more details about the raw metadata in Appendix C.
Later in this book, you will be working directly in the XML to make some model customizations that are not supported by the Designer. Additionally, you will write some code that interacts directly with the raw model. When performing these tasks, you will benefit from having had the interaction with the XML in the following pages and continued in Appendix C. The EDMX file is composed of two main sections: the runtime information and the Designer information. The runtime section comprises three additional sections: one each for storage models, conceptual models, and mappings. The Designer section specifies where the various model elements should be placed visually in the Designer. 32 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
Collapse all of the main sections of the model. You can do this quickly by right-clicking in the XML and choosing Outlining, then Toggle All Outlining. Now you will see only the main node—edmx:Edmx. You can expand that until your view matches Figure 2-9.
Figure 2-9. The main sections of the model
Now you can see the main sections of the model. The Designer element is metadata that tells the Designer how to position the entities. Feel free to explore that at a later time. The critical sections of the model are the runtime ConceptualModels, StorageModels, and Mappings.
CSDL: The Conceptual Schema Let’s begin by taking a closer look at the CSDL, the conceptual schema for the EDM. In the XML, use the + icons to expand the ConceptualModels section until you have exposed the Schema and the EntityContainer, as shown in Figure 2-10. Sometimes the XML formatting is affected and a particular section might lose all of its hard returns, resulting in one very long line of code that is hard to decipher. To fix this, highlight the line and then, from the Visual Studio menu, select Edit→Advanced→Format Selection. This will make the XML formatting much more palatable.
CSDL: The Conceptual Schema | 33
Download from Library of Wow! eBook
Figure 2-10. Expanding the conceptual model, its schema, and the EntityContainer inside the schema
Here you can see how the EntityContainer, EntitySets, and various EntityTypes that we looked at previously in the Designer are defined in the metadata and contain the various EntitySets. Now we will take advantage of the structured XML to learn more about the different elements of an Entity Data Model.
EntityContainer Within the schema is an EntityContainer named SampleEntities (by default). Like the namespace, this is the pattern for the default EntityContainer name using the database name plus the word Entities. You can view and change this name in the model’s Properties window when you have the model open in the Designer. The EntityContainer is a wrapper for EntitySets and AssociationSets. You may recognize the Contacts EntitySet from the Properties window in Figure 2-5. Association Sets reference the associations between the entities. We’ll come back to Association Sets after we’ve discussed the Association elements. As shown in Figure 2-11, the EntityContainer is the critical entry point for querying the model. It exposes the EntitySets, and it is the EntitySets against which you will write your queries. The EntitySets, in turn, give you access to their entities.
34 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
Figure 2-11. The relationship of the EntityContainer to its EntitySets and Entity objects
EntityContainer has an attribute: annotation:LazyLoadingEnabled="true". Annotations exist only in the EDMX file, and are directions for the generation of code based on the model and have nothing to do with the model itself. This setting is also available in the model’s Properties window.
EntitySet An EntitySet is a container for a type of entity. Its two attributes are Name and EntityType. EntityType defines which entity the set contains using its strongly typed name. The entity’s strongly typed name includes the model’s namespace, as shown in the following code snippet:
It is through the EntitySet that you have access to the individual entities when querying against the model. When you begin to query in the next chapter, you will see that you use code that translates to “find some entities in the Addresses EntitySet.” The model instructs your query to return Address entity types. As you will learn later in the book, the Entity Data Model allows for inherited types. Therefore, your model may have a Contact entity and a Customer entity, where the customer is a type of Contact. In this case, the Contacts EntitySet will serve as a wrapper for both the Contact entities and the Customer entities.
CSDL: The Conceptual Schema | 35
Download from Library of Wow! eBook
EntityType An EntityType is a data type in the model. You have already seen a Contact entity type and an Address entity type. In the XML schema, expand the Address entity type, which will look like Example 2-1, to get a closer look at it. It contains a Key element and a list of Property elements. Example 2-1. The Address entity’s XML
The Key element The Key element defines which properties comprise the identity key for the entity. In the Designer and at runtime you will see this represented as an EntityKey.The entity’s key plays a critical role in the life cycle of an entity, enabling your application to keep track of an entity, perform database updates and refreshes, and more. You will learn more about this in Chapter 10. In the Designer, you can specify the key in the Properties window of the entity. The key for the Address entity uses only a single property, addressID. It is possible to have keys composed of multiple properties. These are called composite entity keys and are similar to composite keys in databases. You’ll learn more about composite keys in Chapter 10.
36 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
Figure 2-12. The Address entity with its Street1 property selected and the Street1 details shown in the Properties window
The Property elements Not only do the Property elements have names, but they are additionally defined by their data type and a variety of “facets” that further describe them. The data types that define these properties are called simple types. These are primitive types in the Entity Framework object model that closely line up with the data types in the .NET Framework. The Entity Framework’s primitive types, however, are used only to define the entity property. They do not have their own properties. They are truly simple. You can view and edit most of this information in the Properties window, as shown in Figure 2-12. You can also see properties in the Properties window that are not shown in the XML. You cannot have both the XML and the Designer open at the same time. To return to the Designer, close the XML view of the model and then double-click on the EDMX file.
CSDL: The Conceptual Schema | 37
Download from Library of Wow! eBook
Properties that are set to their default values are not explicitly written out in the XML. This is the case for a number of the properties of Address.Street1, including ConcurrencyMode, Default Value, Getter, and Setter. The EntityKey property is not a facet of Street1 but is used to create the EntityKey element described earlier. If you look at the properties of addressID, you’ll see that its EntityKey property is True. The Getter and Setter properties define the accessibility of each property in the class that is generated from the model’s entity. By default, all of the properties are public, allowing anyone to read or write to them. Changing the values of Getter and Setter will impact the property declarations. Chapter 23 digs further into concurrency, and there you will learn about the ConcurrencyMode property.
The navigation properties The navigation properties of the entities are tightly bound to the associations that are represented by the lines between the entities, as you saw earlier in Figure 2-3. We’ll dig further into the subject of navigation properties after we discuss associations.
Associations Associations define the relationships between entity types. The association doesn’t define the relationship completely, however. It defines the endpoints (i.e., the entities that are involved in the relationship) and their multiplicity. In the example model, there is only one association, which is between the Contact entity type and the Address entity type, telling us that there is a relationship between the two. The name of this association was taken from the predefined relationship in the database when the wizard first created the model. Like any other element in the model, you can edit the name of the association if you prefer more readable names, or if you have naming conventions that you need to follow. Let’s first look at the association’s properties in the Design view. If you are following along, close the XML and open the model in the Designer and click on the association. Figure 2-13 shows the Properties window for the association between Contact and Address.
Download from Library of Wow! eBook 38 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
Figure 2-13. Association properties
The association lists both ends. End1 is the Contact entity type. It is assigned a role called “Contact,” which acts as the name of End1 so that elements elsewhere in the model can point to it. Additional End1 properties tell us more about this end of the association. Multiplicity specifies that there will be only one contact in a relationship between Contact and Address. Navigation Property shows that the Addresses property in the Contact type leads us to the other end of the association. OnDelete, whose options are Cascade and None, lets us know if any related Address entities in memory will be deleted when the Contact entity is deleted. The second end will be an Address entity type, and there can be many addresses in this relationship. After “reading” the association, you can see that there is a one-to-many relationship between Contact and Address. A single contact might have a home address, a work address, and even other addresses. However, an address will only ever be associated with a single contact. In the real world, it is possible for an address to be associated with multiple people—for example, family members or roommates or employees at a single organization. That would involve a many-to-many relationship, which we will explore in Chapter 8.
CSDL: The Conceptual Schema | 39
Download from Library of Wow! eBook
As with entities, an association defines the name of the AssociationSet that contains this type of association. By default, it matches the name of the association. You could also make this name plural, but doing so is not as critical as having plural EntitySet names because you won’t be interacting with the AssociationSets in code. Learn more about AssociationSet in Appendix C.
Finally, make note of the Referential Constraint property. In a model that contains foreign keys in the entities, such as the ContactID property of Address shown in Figure 2-14, the Referential Constraint is critical. It defines any dependency between the related entities.
Figure 2-14. Referential Constraint details
Every Address entity must point to a Contact. Referential constraints are checked when you attempt to store data in the database. For backward compatibility with version 1 models, it is still possible to define the constraints with association mappings (more on these shortly) when you have a relationship between two primary keys.
Including foreign key properties is the default mode for creating an Entity Data Model. You can build models following the version 1 approach whereby entities do not include 40 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
the foreign key. In this case, the Referential Constraint would not be used and the dependency between the Contact and Address would be defined in the association’s mappings. You will learn more about this alternative use in Chapter 19. The associations in this type of model are referred to as independent associations, whereas those in a model using foreign keys are called foreign key associations. We will discuss associations in more detail later in this chapter, and in even greater detail in Chapter 19, which focuses on relationships and associations.
Navigation Property Finally, we can look at the navigation properties in the Address and Contact entity types. Now that I’ve explained associations, navigation properties should be much easier to comprehend. Although it is easily possible for entities to have more than one navigation property, in this particular model we have only one in each entity type. Figure 2-15 shows the Properties window for the Contact navigation property of the Address entity.
Figure 2-15. The Contact navigation property of the Address entity
When you’re working with the Address in code, the Contact navigation property will appear as just another property. Although the other properties are referred to as scalar properties, meaning that they are values, the navigation property describes how to navigate to a related entity.
CSDL: The Conceptual Schema | 41
Download from Library of Wow! eBook
A critical property of the navigation is its association. This tells the navigation property which association in the model contains information regarding how to navigate to the related entity (or entities in the case of Contact.Addresses). As I explained earlier, that association defines the relationship between the Address and Contact entity types. The FromRole and ToRole attributes tell the Entity Framework that when it looks at this association, it needs to navigate from the endpoint named Address to the endpoint called Contact. This, in turn, will allow you to navigate from the Address entity type to its associated Contact entity type in your code. As with entities, the Designer shows some properties that are used for code generation: Getter, Setter, and Documentation. Additionally, Multiplicity is linked to the multiplicity of that same end in the association. You can change it in the Navigation Property properties or in the Association properties. The Contact’s multiplicity is “1 (One),” telling you that when you navigate to Address.Contact, you will get an instance of a contact (a single contact object). A navigation property that returns a single object is referred to as a navigation reference. Return Type is a read-only property that is located in the Designer’s Properties window to help you to better understand the navigation. When looking at this same navigation property in the XML you will not see these last two properties, and because the Setter, Getter, and Documentation properties are using the defaults, they are not listed either, as shown in the following code snippet:
Navigation Properties That Return Collections The Contact property of an Address entity returns a single instance of a contact. What about the Addresses property of a Contact entity? Figure 2-16 shows the Addresses property. When navigating from Contact to Addresses, the Addresses endpoint defined in the FK_Address_Contact association has a multiplicity of * (Many). Therefore, the Entity Framework expects a collection inside the Addresses property. In the Designer, the Return Type property of Addresses is a collection of Address types. This type of navigation property is called a navigation collection. In code, the Contact.Addresses property will return a collection of Address entities, even if there is only one address in the collection. If there are no addresses for a particular person, the collection will be empty. The collection that is exposed in the Addresses navigation property is not a collection from the System.Collections namespace, but rather an EntityCollection. The EntityCollection is a completely unique class in the Entity Framework. So, although it is simpler to say “a collection of addresses,” it is important to pay attention when 42 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
Figure 2-16. The Addresses navigation property
you are working with an EntityCollection versus a type that implements System.Col lections.ICollection, as they are not interchangeable. It is important to note that in this simple model, the conceptual layer has not been customized. It mirrors the schema of the database, which is a very good place to begin to learn about the EDM. Later in the book, you will learn about customized models and begin to leverage the real power of the EDM.
SSDL: The Store Schema Continuing with our simple model, it’s time to look at another piece, the SSDL, which you will need to understand before we discuss the MSL. The StorageModels section of an EDMX file is a schematic representation of its associated data store. The elements of this file are similar to those of the CSDL file. Figure 2-17 shows the complete SSDL from the EDMX file, although not every section is expanded. The EDM design tools include a feature that allows you to update the model from the database. It is available in the context menu that you get when you right-click in the EDM Designer. You’ll work with this feature in Chapter 7 to bring a database’s stored procedures into the model.
SSDL: The Store Schema | 43
Download from Library of Wow! eBook
Figure 2-17. Expanded StorageModels section to explore the store layer of the model
For consistency, the tables and columns are called EntityType and Property. Frequently, you will see these referred to in documentation as tables and columns, and even as such in the visual tools. Note the following ways in which the SSDL differs from the CSDL: • Schema element: — The namespace has “.Store” appended to it so that it’s clear that this schema is for the data store, not the conceptual layer of the model. — There is a ProviderManifestToken attribute. The value in the example represents the simple expression of the version of SQL Server—for example, 2008—which is the database being used for this model. The true version number of SQL Server 2008 is 10.0.1600.22. The Entity Framework relies on this bit of information, so it is required. The values are determined by the provider that you are using (in this case, SqlClient) and what values it exposes for the token. 44 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
— The xmlns namespace indicates the namespace used for this section of the XML file. Again, this particular parameter is static. • Entity container: — The name of the EntityContainer is “SampleModelStoreContainer,” which was derived from the database name. • Entity type: — The entity type names are the actual names of the tables in the database. — The property types are the data store data types—in this case, SQL Server data types. — The identity columns are attributed with StoreGeneratedPattern="Identity", meaning that the value will be generated (e.g., by the database) when the row is inserted and will not otherwise change. The other options are "Computed", which specifies that the value will be generated on inserts and updates, and "None", which is the default. In Chapter 16, you will have an opportunity to work directly with the SSDL metadata. You can find additional details about the SSDL metadata in Appendix C. Pay attention to the database version specified in the ProviderMani festToken. If you are moving from one version of SQL Server to another (e.g., your development machine uses SQL Server 2008 but a client that you work with uses SQL Server 2005), you will need to modify that value manually in the XML.
MSL: The Mappings The last section of the EDMX file to look at is the Mappings section. In the Entity Framework metadata, the mapping layer sits between the conceptual and store layers and not only provides the map from the entity properties back to the tables and columns in the data store, but also enables further customization of the model. You can view the mappings in the Designer’s Mapping Details window. To follow along, close the XML view of the model and open the model in the Designer by doubleclicking the model’s EDMX file in the Solution Explorer. To see the Mapping Details window, right-click the Contact entity and select Table Mapping from the menu. The Contact’s mapping information will be displayed in the Mapping Details window, as shown in Figure 2-18.
MSL: The Mappings | 45
Download from Library of Wow! eBook
Figure 2-18. The Contact’s mapping information as displayed in the Mapping Details window
Figure 2-18 shows visually how the Contact entity maps to the Contact table in the store layer. This is defined by “Maps to Contact,” which refers to the table name in the SSDL. In other words, the Contact entity maps to the Contact table. Because the Contact entity has not been customized, the mapping is straightforward—there is a one-to-one mapping between the conceptual layer and the store layer. Beneath the table selection for the Contact table, you can see that the columns from the table (on the left) are mapped to the entity’s properties on the right. When you are creating the mappings yourself rather than relying on the wizard, the Designer, by default, will match identical names to each other, which is a great help. You can also see that the columns include the provider type (int, nchar, and datetime from SQL Server), whereas the properties include the Entity Framework’s primitive types (Int32, String, and DateTime). You can use the “Add a Condition” and “Add a Table or View” placeholders to further customize the model. We will cover this subject in Chapter 14. Appendix C explores the XML representation of this entity mapping. There’s more to come on associations and mappings in Chapter 19.
Database Views in the EDM Something we haven’t yet explored in the EDM is the database view from the sample database. The wizard pulled one view into the model and this resulted in the vOfficeAddress entity. Database views are handled by the Entity Framework in essentially the same way it handles tables. 46 | Chapter 2: Exploring the Entity Data Model
Download from Library of Wow! eBook
If you were to dig through the model, you would find that something in the SSDL, called a DefiningQuery, contains the T-SQL from the database that defines the view. When you originally built the model with the EDM Wizard, you may have seen some warnings in the Error List window about this view. The wizard will discover that there is no primary key for the view and will infer an entity key from any non-nullable properties in the entity. The warning message informs you of this and the same message is embedded into the EDMX file. The wizard cannot create an entity with no EntityKey. Therefore, if there are no non-nullable values in the view, or in a table, for that matter, it will completely skip entity creation for that object.
Chapter 16 will dig deeper into DefiningQuery. For now, remember that the view comes into the model as an entity and if the view is not updatabale, you can update it by tying it to stored procedures, a feature called function mapping. You will learn about function mapping in Chapter 7. Keep in mind that any changes to the database tables, views, or other objects will not automatically be updated in the model. You’ll learn about updating the model in Chapter 7.
Summary This chapter introduced you to the Entity Data Model and to a bit of the functionality of the design tools. You created your first EDM and looked under the covers to gain an understanding of its most common components. You explored the mappings in the Designer and in the raw XML. As explained previously, the EDM shines when you can begin to take advantage of the fact that it is highly customizable. Now that you have a solid understanding of the EDM, you are prepared to learn about advanced mappings and customization, which we will explore in Chapters 14 through 16. But for now, this model provides enough to get started with querying, which you will begin in the very next chapter.
Summary | 47
Download from Library of Wow! eBook
Download from Library of Wow! eBook
CHAPTER 3
Querying Entity Data Models
You can query Entity Data Models in a variety of ways. Some ways you will choose for personal preference and others you will choose so that you can leverage particular benefits. You have likely heard of LINQ to Entities and Entity SQL. You can also use special methods (some based on LINQ and others based on the Entity Framework’s ObjectQuery class) to express queries. Each of these query styles will result in materialized objects. There is a lesser-known means of querying using Entity Framework’s EntityClient API, which allows you to stream raw data back to your applications. In this chapter, you will get a chance to try out all of these different styles of querying. You will repeat a few simple queries using the various mechanisms and look at the results so you can see how the different query methods relate to one other. By the end of this chapter, you will have gained a high-level understanding of all of the query options and their basic uses. In further chapters, you will write more complex queries; the foundation you will receive from this chapter will make that task much easier. In addition, at the end of this chapter you’ll find a critical lesson on query execution. Although the query examples in this chapter are presented within a console application, you can use LINQPad to test the queries and see the results. Some of the guidance in this chapter will also inspect the debugger, which you will not be able to do in LINQPad. See the sidebar “LINQPad” on page 56 for more information about this tool.
Query the Model, Not the Database In this chapter, you will learn how to construct queries against the EDM that you created in Chapter 2, and you will learn to let the Entity Framework take it from there. Here is where you will experience the difference between writing queries against a data model and writing queries against the database. The Entity Framework will process your queries and will leverage the ADO.NET provider (in this case, System.Data.SqlClient) to turn the EDM query into a query the target database will
49
Download from Library of Wow! eBook
comprehend. After the database has executed the query, the results will be turned into objects that are based on the entities in the model. These returned objects are an important piece of the querying process, but surely you want to start querying, so first we’ll query and then we’ll take a peek under the covers.
Your First EDM Query In Chapter 2, you created an EDM inside a console application. Here you’ll create your first queries against that EDM. You can use that same project, so if you’ve closed it, open it and let’s get started. The code in this section will execute the simplest form of a query, which will return every Contact entity from the database, and then display the results in a console window. 1. Open the Program.cs file. 2. Add the method in Example 3-1 beneath the Main method. IntelliSense will assist you as you type. After you’ve written a few basic queries, you’ll make the code a little more efficient. Example 3-1. Querying Contacts and writing out their names private static void QueryContacts() { using (var context = new SampleEntities()) { var contacts = context.Contacts; foreach (var contact in contacts) { Console.WriteLine("{0} {1}", contact.FirstName.Trim(), contact.LastName); } } Console.Write("Press Enter..."); Console.ReadLine(); }
3. Add the following code into the Main method: QueryContacts();
4. Press F5 to run this bit of code. When the code hits the ReadLine() method, all of the names are listed in the console window. You have just executed your first query against an EDM and seen the objects that result. 5. Press the Enter key to finish running the app. Now you’ll run the query again, but this time you’ll look at some of what’s going on: 1. Set a breakpoint at the end of the foreach block, which is at the closing brace (}).
50 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
2. Press F5 to run the code again. 3. When the debugger reaches the breakpoint, hover your mouse pointer over the contact variable in the foreach statement and you will see that it is a Contact entity type (see Figure 3-1).
Figure 3-1. The query results returning Contact entities at runtime
4. Next, hover your mouse pointer over the contacts variable in that same statement and you’ll see that its type is a System.Data.Objects.ObjectSet of Contact types. System.Data.Objects is the Entity Framework’s API for creating and managing entity objects. The ObjectSet is what the Entity Framework returns when you make a call to an EntitySet (e.g., Contacts). It derives from another important class called ObjectQuery, which is used to construct and execute queries that will return objects. Once the ObjectQuery has been executed, it contains results, which were all of the contacts you saw listed in the console. The context took the data that was returned and used it to create these Contact objects on your behalf. Because you asked only for the Contacts and did not request any filtering, all of the contacts were retrieved from the database when the query was executed. Although this doesn’t really look like a query, it is a query—albeit a very simple one. You’ll take a closer look at this after the next query. 5. You can continue the application or stop it by pressing Shift-F5. Now that you know this query returns an ObjectSet you can rewrite the code that uses implicit typing with the var keyword to explicitly declare the type. This way, you can specify the type when the code (e.g., context.Contacts) does not make it obvious what will be returned, which will make it easier for you or others to understand your code at a later time. ObjectSet contacts = context.Contacts;
ObjectSet is in the System.Data.Objects namespace. Either specify that
in the code line or add the namespace to the beginning of the code file (using System.Data.Objects; or for VB, Imports System.Data.Objects).
Where Did the Context and Classes Come From? Since you just dove right into the code, you might have a few questions. For instance, where did the Contact type come from? How did you go from an XML file (the EDMX file) to strongly typed .NET objects? Why is context.Contacts a query unto itself, and what is that context anyway? Your First EDM Query | 51
Download from Library of Wow! eBook
One of the features of the EDM design tools is that the Designer automatically performs code generation based on the model. The model’s Designer.cs file is attached to the model in the Solution Explorer, as shown in Figure 3-2.
Figure 3-2. The automatically generated code file attached to the model in Solution Explorer
Expand the .edmx file in the Solution Explorer to see the generated code file. Open the file to see what’s in there. Because the file is generated automatically, you don’t want to edit it directly. You’ll learn how to customize the classes in this file in Chapter 11.
The generator reads the conceptual layer of the model and creates from it an ObjectContext class based on the EntityContainer, and then one entity class for each entity in the model (see Figure 3-3).
Figure 3-3. Generated ObjectContext and Entity classes based on the conceptual model
The generated code file contains four classes. Figure 3-4 shows these classes in Visual Studio’s Class Designer view. You can open a class in the Class Designer by rightclicking on the class in the Solution Explorer and then choosing View Class Diagram. The first class (which I’ve expanded from its default view by clicking on the arrows in the upper-right corner) is SampleEntities. This class has taken the model’s EntityContainer name. The others are for each entity—Address, Contact, and vOfficeAddresses. 52 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
Figure 3-4. The four classes in Visual Studio’s Class Designer view
The ObjectContext class, SampleEntities When you looked at the XML view of the model in Chapter 2, you saw an EntityContainer that contained the EntitySets and AssociationSets. The SampleEntities class represents that EntityContainer and inherits from an Entity Framework type called ObjectContext. This is why context is used for the variable in the example. SampleEntities has three properties—Addresses, Contacts, and vOfficeAddresses—which are the EntitySets defined in the model. The three AddTo methods were created by the code generator to provide a means of adding new object instances to the context, which will then be able to insert those into the database. These AddTo methods exist for backward compatibility with the .NET 3.5 version of Entity Framework. In .NET 4, you should take advantage of the Add method provided by ObjectSet, which you will learn about in later chapters. My convention when coding Entity Framework is to always use “context” as the variable name for ObjectContext instances.
Looking more closely at the Contacts property, you can see that it returns an ObjectSet of Contact types: public ObjectSet Contacts
Your First EDM Query | 53
Download from Library of Wow! eBook
For VB developers: if you are unfamiliar with the syntax for generics, C# expresses the type in angle brackets, whereas VB uses parentheses plus the keyword Of. The preceding code in VB would be as follows: Public Property Contacts As ObjectSet(Of Contact)
An ObjectSet is the basis for our queries, whether you want the entire set of Contact entities, as you requested in Example 3-1, or you request a subset, which you will do in Example 3-2. You will write entity queries against the ObjectSet in much the same way that you would write a database query against a database table.
The entity classes The three entities defined in the model are the source for the three entity classes. Each class inherits from the Entity Framework’s EntityObject class and has properties based on the properties defined in the model, including the Contact.Addresses and Address.Contact navigation properties where necessary (see Figure 3-5).
Figure 3-5. The entity classes in the Class Designer
But there’s something new in the Address class: ContactReference, which is another way to access the Contact property. You’ll learn about EntityReference properties in detail in Chapter 19. These classes have more members, but as they are not relevant to the querying you’ll do in this chapter, we will dissect them later in the book. 54 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
Dig deeper: don’t be afraid to poke around in the generated code file, but remember that any changes you make will be overwritten anytime the model is modified and saved.
Querying with LINQ to Entities The LINQ to Entities query syntax is easier to learn and to use than Entity SQL, and it’s possibly already familiar to you if you have been using LINQ elsewhere. LINQ to Entities will very likely cover a large portion of your query needs. LINQ is a language enhancement that was added to Visual Basic and C# in .NET 3.5. LINQ stands for Language INtegrated Query, and LINQ to Entities is one of its implementations. Although F# does not natively support LINQ, the F# Power Pack (http: //fsharppowerpack.codeplex.com), created by the F# team, provides LINQ querying.
LINQ was originally written to provide an independent query language that could be used across all CLR objects. There are now many implementations of it. You just used an implementation created to work with entity objects. Visual Studio and the .NET runtime also include LINQ to SQL, an implementation that queries directly against SQL Server databases. Many third parties are also writing LINQ providers. It is possible to get very creative with LINQ queries, and you will easily find a number of books devoted entirely to LINQ. When you’re starting out it’s helpful to understand the basic structure.
Writing Your First LINQ to Entities Query The preceding query used a shortcut that produced a query for you. But it didn’t really feel like a query. Now you’ll write a LINQ to Entities query using LINQ operators. Remove the breakpoint that you set in the previous steps. In the line of code that created the contacts memory variable, replace context.Contacts with the query in Example 3-2, which retrieves a subset of the contacts. Example 3-2. A LINQ to Entities query in VB and C# VB
Dim contacts=From c In context.Contacts Where c.FirstName = "Robert"
C#
var contacts = from c in context.Contacts where c.FirstName == "Robert" select c;
Querying with LINQ to Entities | 55
Download from Library of Wow! eBook
You’ll find many differences between VB and C# syntax when writing LINQ queries. Besides the casing, notice that VB does not require that you explicitly use the Select operator, whereas C# does.
Run the application again and you will see that only a small number of contacts are listed and they all have Robert as their first name. The most obvious sign of integration in LINQ queries is that as you typed your query, you had the benefit of IntelliSense assisting you—for example, providing FirstName as an option for the c variable. That was because when you identified Contacts at the beginning of the query, the compiler was able to determine that the items in that collection are Contact items. When you typed c later in the query in the SELECT and WHERE clauses, IntelliSense was able to present a list of Contact properties in the IntelliSense suggestions.
Why Does LINQ Start with FROM? LINQ queries begin with the FROM clause, rather than the SELECT clause that most of us are familiar with in other query languages. When LINQ was being created, query statements did begin with SELECT. However, the developers at Microsoft quickly realized that identifying the type that is being used up front enabled IntelliSense to provide meaningful suggestions as the rest of the query was constructed. According to Microsoft’s Y. Alan Griver, who was very involved with the LINQ project during its early stages, the Microsoft developers jokingly referred to this syntax as “Yoda speak” when they altered the syntax for the sake of IntelliSense.
In the query, c is just an arbitrary variable name that lets you reference the thing you are working with further on in the query. It’s referred to as a control variable. The control variable provides another means by which IntelliSense and the compiler are able to make LINQ more productive for developers.
LINQPad LINQPad is a wonderful tool written by fellow O’Reilly author, Joseph Albahari (LINQ Pocket Reference [http://oreilly.com/catalog/9780596519254/], C# 4.0 in a Nutshell [http://oreilly.com/catalog/9780596800963/], and more). It was originally written to be used with LINQ to Objects, but over time, Joseph added support for LINQ to SQL and Entity Framework (Entity SQL as well as LINQ to Entities). It is a great way of testing your queries outside your application. You can download LINQPad for free at http://www.linqpad.net. There’s an inexpensive (and well worth the nominal fee) upgrade to enable IntelliSense in the tool. On the LINQPad website and in the download, you will find lots of great tutorial instruction on how to use LINQPad and how to use it with the Entity Framework. 56 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
Many of the examples in this chapter focus on only the query. These are great queries to test in LINQPad. Other examples involve additional tasks beyond the queries and you may want to perform these in a console application as instructed.
Querying with Object Services and Entity SQL Another way to create a query, instead of LINQ to Entities, is by using the Entity Framework’s Object Services (in the System.Data.Objects namespace) directly. You can create an ObjectQuery directly combined with the Entity Framework’s T-SQL-like query language, called Entity SQL, to build the query expression. To see how this works, modify your example with the following steps: 1. Replace (or comment out) the line of code containing the LINQ to Entities query with the code in Example 3-3. Example 3-3. Querying with Entity SQL var queryString = "SELECT VALUE c " + "FROM SampleEntities.Contacts AS c " + "WHERE c.FirstName='Robert'"; ObjectQuery contacts = context.CreateQuery(queryString);
2. Run the app again, and the results will be the same as before. In the first line of code, you created an Entity SQL expression. In the second, you created an ObjectQuery, passing in the expression that the query should use. The existing code in your example then executes the query and returns results. If you have constructed SQL queries before, the Entity SQL syntax you used in Example 3-3 looks familiar but not quite right. The return type of this query at design time is an ObjectQuery , which implements IQueryable. But as you will learn later in this book, it is possible to cast the LINQ to Entities IQueryable to an ObjectQuery and then access those properties and methods. This means that even if you choose to use LINQ to Entities, you will still get to benefit from these properties and methods.
Why Another Way to Query? Why would you need another means of querying the EDM in addition to LINQ to Entities? Microsoft did not plan in advance to confuse you with these two options. In fact, Entity SQL was being created before LINQ existed, but now each serves its own purpose. LINQ is obviously much easier to use because it is a query language you can use throughout .NET, not just in Entity Framework, and its strong typing makes it fairly easy to construct the queries. However, LINQ can’t be used for every scenario. It is part of C# and Visual Basic but is not built into the other .NET languages. Additionally, you’ll learn later about streaming query results in DataReaders when you don’t Querying with Object Services and Entity SQL | 57
Download from Library of Wow! eBook
need to materialize objects. This can be done only with Entity SQL expressions. As you will see in Chapter 5 and later chapters, there are also some scenarios where being able to build Entity SQL strings is advantageous. Therefore, although you will most likely do the bulk of your querying with LINQ to Entities, when you do encounter these less common cases, you’ll be prepared.
Entity SQL Entity SQL (ESQL) was actually the first syntax devised for querying entities. LINQ was being developed as a language extension by the VB and C# language teams, and eventually it became obvious that LINQ would be a fabulous addition to the Entity Framework, which is how LINQ to Entities came to be. Entity SQL has its roots in SQL because it makes sense to start with something that is well known. However, because entities are different from relational data, Entity SQL deviates from SQL to provide the necessary capabilities for querying the EDM.
How Is Entity SQL Different from T-SQL? The Entity Framework documentation has a topic called “How Entity SQL Differs from Transact-SQL.” It provides a list of differences with an extended explanation for each difference. For example, Entity SQL supports the inheritance and relationships found in an EDM, whereas in T-SQL you must use joins to work with relationships. Relational databases do not even have the concept of inheritance; therefore, T-SQL doesn’t support that either.
Looking more closely at the Entity SQL query string you built earlier, you’ll notice that, like LINQ to Entities, it defines a variable for use in the query: c. In LINQ this is referred to as a control variable, but in Entity SQL it is just called a variable. Figure 3-6 deconstructs the query string without the WHERE clause. The variable is defined using the AS keyword and is referenced in the SELECT clause. The VALUE keyword specifies that you want to return a collection of single items; in this case, it will be Contact entities.
Figure 3-6. Deconstructing a simple Entity SQL query
58 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
The VALUE clause is needed if you are selecting a single type, which can be an entity, a single property, or even an entity collection, and that you want to return strongly typed objects. This is shown in the following code snippet: SELECT VALUE c FROM SampleEntities.Contacts ... SELECT VALUE c.FirstName FROM SampleEntities.Contacts ... SELECT VALUE c.Addresses FROM SampleEntities.Contacts ...
If you are selecting multiple items, you cannot use VALUE, as shown here: SELECT c, c.Addresses FROM SampleEntities.Contacts .... SELECT c.LastName,c.Title FROM SampleEntities.Contacts ...
If you forget to use VALUE, the strongly typed objects will be inside a wrapper, which we will discuss in more detail momentarily. You will need to explicitly cast the results back to the desired type or you could encounter an InvalidOperationException at runtime. If you include VALUE with multiple items, an EntitySqlException will be thrown that specifically tells you the following: "SELECT VALUE can have only one expression in the projection list."
It will even tell you the line number and column number of the problem. But unfortunately, because the Entity SQL string is not compiled until runtime, you won’t be aware of this problem until then. Chapter 22 delves deeper into Entity Framework exceptions.
Without the VALUE clause, the results will be wrapped in tabular rows and you will have to dig into the rows and columns to get at the data. Similar to the LINQ query, you are selecting FROM a collection. In this query, that collection is the entity set, Contacts, but it is necessary in Entity SQL to specify the EntityContainer as well. Again, c is a random variable name I used in the query used to represent contact items within the Contacts entity set. The WHERE clause in Entity SQL uses SQL-like syntax, as in the following: WHERE c.FirstName='Robert'
Entity SQL canonical functions The Entity SQL language is very robust and offers a lot of functionality. Although it would be impossible to cover all of the operators and functions the language supports, you will see many of them used throughout this book and you can get the full list by looking at the Entity SQL documentation in the MSDN Library.
Querying with Object Services and Entity SQL | 59
Download from Library of Wow! eBook
Entity SQL supports a large set of canonical functions, which are functions that all data providers are required to support. It also enables data providers to include their own specific functions. The .NET Framework provider for SQL Server, written by Microsoft, offers approximately 75 specific functions that you can use in Entity SQL queries when the target database is SQL Server; some of these overlap with the canonical functions. The provider additionally offers the provider-specific primitive types and their facets as well as the internal logic for mapping between the EDM and SQL Server. Other providers that are written for the EDM will have their own lists of additional functions and features that are supported. Remember that one of the great benefits of querying in Entity Framework is that it is database-agnostic. Therefore, you should be considerate before adopting provider-specific elements in your Entity SQL queries.
If you are familiar with T-SQL, you’ll be happy to know that one of the canonical functions is Trim(), which means you won’t have to use the silly LTRIM(RTRIM()) combo anymore.
The Parameterized ObjectQuery ObjectQuery allows you to create parameterized queries. Similar to some other query languages, you use an @ placeholder in the string, and then define its value in a
parameter. To use a parameterized query, you can add parameters to an ObjectQuery created with the CreateQuery method of the ObjectContext or to one that you have instantiated explicitly, as shown in Example 3-4. You also need to pass the ObjectContext as a parameter when you instantiate an ObjectQuery. You then add parameters to the ObjectQuery prior to execution. To see how this works, you can rewrite the query you’ve been working with to enable dynamic changes to the query, as in Example 3-4. Example 3-4. Adding an ObjectParameter to an ObjectQuery qStr = "SELECT VALUE c FROM SampleEntities.Contacts AS c " + "WHERE c.firstname=@firstName"; ObjectQuery contacts = new ObjectQuery(qStr, context); contacts.Parameters.Add(new ObjectParameter("firstName", "Robert"));
60 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
The namespaces in many of the examples are not spelled out along with the classes. Be sure to reference the appropriate namespaces at the top of your code files with Include for Visual Basic and using for C#. For example, for the ObjectQuery class you’ll need the System.Data.Objects namespace.
Although it may seem tempting, you cannot use parameters to replace property names in the query string. In other words, if you tried to create the Entity SQL string SELECT @myproperty FROM SampleEntities.Contacts AS c and you created a parameter that set @myproperty to c.LastName, the ESQL that results would look like this: "SELECT 'c.LastName' FROM SampleEntities.Contacts AS c"
This is invalid ESQL and will throw an error. You would need to use string concatenation with variables to build the ESQL: "SELECT " + _propName + " FROM SampleEntities.Contacts AS c"
Because of security concerns, you should be extremely careful about where the property names come from. You should not concatenate from user input. Imagine someone enabling a query such as “Select Login, Password from Contacts”.
Querying with Methods So far, the LINQ to Entities and Object Services queries you have seen have been written as standard query expressions. Both LINQ to Entities and Object Services provide a way to write queries as methods, rather than as operators and functions (as in LINQ) or as a string (as in Entity SQL). Both query languages have a method syntax that you can use, but each exists for opposite reasons. The C# and Visual Basic implementations of LINQ sit on top of query methods. Your LINQ expressions are translated into these query methods, but you can use them directly if you like. The Entity Framework processes Entity SQL directly; however, a method-based syntax is available that will construct Entity SQL expressions for you.
Querying with LINQ Methods Although Visual Basic and C# understand LINQ syntax, the CLR does not. One of the first things to happen when the compiler compiles LINQ queries is that it translates the query into a set of method calls on the collection being queried. All of the standard query operators (WHERE, SELECT, JOIN, etc.) have associated methods in .NET.
Querying with Methods | 61
Download from Library of Wow! eBook
You can write your queries using the method syntax directly, if you prefer. Many developers do happen to prefer this, although many others would rather use the query expression syntax. The MSDN documentation says, “In general, we recommend query syntax because it is usually simpler and more readable; however, there is no semantic difference between method syntax and query syntax.”* Therefore, using one over the other is a matter of style and personal choice. MSDN provides a list of LINQ methods and whether they are supported by LINQ to Entities. The topic title is “Supported and Unsupported LINQ Methods (LINQ to Entities)” and its URL is http://msdn.microsoft .com/en-us/library/bb738550.aspx.
To write method-based queries, you will need to leverage a feature introduced in .NET 3.5, called lambdas. Lambdas are inline methods with a very specific syntax. If you are new to LINQ and lambdas and have never used anonymous delegates, this will make more sense after you’ve seen some examples. Let’s use the Where clause to explore working with a method rather than an operator. A standard Where clause is written as where LastName=="Hesse". The Where() method requires the condition LastName=='Hesse' as a parameter. You will write this lambda very differently in C# and Visual Basic.
Wrapping Your Head Around Lambdas There’s no question that lambda expressions are a little confusing at first; but once you get the hang of them, they make perfect sense and can help you write some very efficient code. Admittedly, my Visual Basic background prepared me a little less for lambdas than if I had been programming in C++ or more frequently in C#. Some great articles are available that can help you learn more about lambda expressions. For C# developers, the excellent MSDN Magazine article by Anson Horton, “The Evolution of LINQ and Its Impact on the Design of C#” (http://msdn.microsoft.com/en-us/magazine/ cc163400.aspx), has a great explanation of lambdas. For VB developers, the great MSDN Magazine article by Timothy Ng, “Basic Instincts: Lambda Expressions” (http: //msdn.microsoft.com/en-us/magazine/cc163362.aspx), puts lambdas into perspective.
Here we’ll take a look at the query you used in the previous examples, now written using method-based queries. In Visual Basic, the expression begins with Function, to indicate that you are performing a function on a control variable; then it states the condition. The control variable, c in this example, is named on the fly: Dim contacts = context.Contacts _ .Where(Function(c) c.FirstName="Robert")
* LINQ Query Syntax versus Method Syntax (C#): http://msdn.microsoft.com/en-us/library/bb397947.aspx.
62 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
The C# LINQ to Entities query using the method-based syntax looks very different: var contacts = context.Contacts .Where(c => c.FirstName=="Robert");
C# lambda expressions begin by identifying the control variable, followed by => (the lambda) and then the expression, [controlVariable].FirstName=="Robert". When using LINQ methods in C#, you are not required to use a Select command as you are with LINQ query operators.
In the Where clauses, the expression that returns a Boolean is called a predicate. The query will return all of the contacts for which the expression evaluates to True. Try it out: 1. Replace your existing query with one of the method queries. You will see that IntelliSense is helpful when writing the lambdas. 2. Press F5 to run the application. The results will be the same as before.
Chaining methods You can combine LINQ query methods to build more useful expressions. This is referred to as chaining. To try this, add an OrderBy method to the previous query. Notice that the lambda expression for OrderBy does not need to evaluate a condition to see whether it is true or false, as does the Where method. It only needs to return a property, as in Example 3-5. Example 3-5. Chaining LINQ methods var contacts = context.Contacts .Where((c) => c.FirstName == "Robert") .OrderBy((foo) => foo.LastName);
When a method’s signature requests a predicate, as is the case with the Where method, it is asking for an expression that returns a Boolean. Otherwise, the lambda only needs to be a function, as in the OrderBy method. You’ll see that in Visual Basic, the signatures of all methods refer to this as a function. The C# methods specifically refer to predicates in the methods that require an expression that returns a Boolean. You can view the signatures of the various LINQ to Entities methods in the MSDN documentation topic, “Supported and Unsupported Methods (LINQ to Entities).”
Querying with Methods | 63
Download from Library of Wow! eBook
Although you can easily use the same variable name throughout compound methods, the variables don’t represent the same instance. In the preceding LINQ query, I named the variables differently to highlight how the compiler evaluates the query. LINQ actually evaluates the query one method at a time. First it evaluates context.Con tacts. Then it applies the Where method to those results. Finally, it applies the OrderBy method to the results of the Where method. The c in the Where method refers to the items returned by context.Contacts. The foo in the OrderBy method refers to the IQueryable that is returned by context.Contacts.Where(....). Evaluating one method at a time does not mean executing one query at a time. LINQ to Entities will evaluate this query one method at a time and then will create a SQL query based on the complete method, unless you are also using methods that must be performed on the client side. It does not execute each method separately. Here is the T-SQL that results from the preceding query: SELECT [Extent1].[ContactID] AS [ContactID], [Extent1].[FirstName] AS [FirstName], [Extent1].[LastName] AS [LastName], [Extent1].[Title] AS [Title], [Extent1].[AddDate] AS [AddDate], [Extent1].[ModifiedDate] AS [ModifiedDate] FROM [dbo].[Contact] AS [Extent1] WHERE N'Robert' = [Extent1].[FirstName] ORDER BY [Extent1].[LastName] ASC
Querying with Query Builder Methods and Entity SQL It’s possible to use Entity SQL with method syntax as well, although a limited number of methods are available: 13, in fact, including Where and Select. These methods are called query builder methods. Query builder methods will do as their name suggests: build an ObjectQuery with the correct Entity SQL expression for you. Although the query builder methods may look like some of the LINQ methods, they are definitely different. The compiler can tell when you are using a query builder method based on the parameter expression, which will contain either a lambda expression for LINQ queries or an Entity SQL expression. Since you have explored only WHERE and SELECT so far while learning about the different ways to query, we’ll hold off on listing methods and operators until the following chapter, which has many queries.
Example 3-6 shows the latest query using Entity SQL as the method parameters.
64 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
Example 3-6. Entity SQL query builder method var contacts = context.Contacts .Where("it.FirstName = 'Robert'") .OrderBy("it.LastName");
The most common question regarding these expressions is “Where did it come from?” it is the default alias for the control variable. There is no opportunity to define the control variable as you have had to do with all of the other queries we have looked at so far, though it is possible to define your own for nested queries, as you’ll see in Example 3-8. When debugging, you can inspect the CommandText property of the contacts Object Query to see that the query builder did indeed build the Entity SQL for you as shown in Example 3-7. It’s a little more complex than what you might have written yourself. This is a result of the query builder’s need to be flexible. Additionally, it does not specify the EntityContainer name in the expression, something that you can’t get away with when building the Entity SQL yourself. Example 3-7. The Entity SQL built by the query builder methods SELECT VALUE it FROM (SELECT VALUE it FROM ([Contacts]) AS it WHERE it.FirstName = 'Robert') AS it ORDER BY it.LastName
An interesting difference between query builder methods with Entity SQL and LINQ methods with lambdas is that the Entity SQL expressions remove the need to worry about any syntax differences between Visual Basic and C#. Whether you use LINQ predicates or Entity SQL predicates, at compile time the Entity Framework will be able to determine which query compilation path to choose by looking at the predicate.
Specifying the control variable As you can see in Example 3-8, you also can combine query builder methods. The Entity SQL control variable is always it by default for all new ObjectQuery instances. Once you have an ObjectQuery instance, however, you can change the control variable name by setting the name property. From there you could continue composing the query as shown in Example 3-8. Example 3-8. Naming a control variable var contactsQuery = context.Contacts; contactsQuery.Name = "con"; var contacts = contactsQuery.Where("con.FirstName = 'Robert'") .OrderBy("con.lastname");
Querying with Methods | 65
Download from Library of Wow! eBook
The preceding example demonstrated an additional feature, called composable queries. A query was defined (contactsQuery) and then another query was written against it. The first query is not executed separately. It is compiled into the second query, contacts. When the contacts query is finally executed, the composed query is compiled by Entity Framework and sent to the database. LINQ to Entities queries are composable as well.
The Shortest Query Remember the first query in this chapter? ObjectSet contacts = context.Contacts;
In this case, context.Contacts refers to the Contacts property of the SampleEntities. If you look back at the code generated from the model, you can see that context.Contacts returns the following query: _Contacts = base.CreateObjectSet("Contacts");
This is an ObjectSet of Contact types. When you pass in only the name of the EntitySet, the Entity Framework will do the rest of the work. You can use this shortcut yourself as well, but it is no different from calling context.Contacts; it’s just longer.
Combining LINQ Methods and Query Builder Methods Because their methods are evaluated incrementally, it is possible to combine LINQ query methods and the query builder methods. Then you can get the variety of methods, strong typing, and IntelliSense provided by LINQ, plus the ability to build dynamic expressions and use provider functions, among other benefits of Entity SQL. However, there’s a catch. You can add LINQ methods to an ObjectQuery or to query builder methods, but the only query builder method that you can add to a LINQ expression is Include.
ObjectQuery, ObjectSet, and LINQ to Entities Simply requesting and then executing context.Contacts without building a query is enough to allow the context to construct and execute a query to return the Contact entities. You saw that effect in Example 3-1. That is possible because ObjectSet is a type of ObjectQuery. ObjectQuery is the class that provides all of the information the context needs to execute the query. In addition to being a type of ObjectQuery, Object Set also implements the IObjectSet interface, which provides collection-like functionality. This allows us to manipulate an ObjectSet (e.g., adding and removing entities). Example 3-9 shows the class declaration for ObjectSet with the base class and interfaces that give ObjectSet its core functionality. 66 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
Example 3-9. ObjectSet declaration in VB and C# VB
Public Class ObjectSet(Of TEntity As Class) Inherits ObjectQuery(Of TEntity) Implements IObjectSet(Of TEntity), IQueryable(Of TEntity), IEnumerable(Of TEntity), IQueryable, IEnumerable
C#
public class ObjectSet : ObjectQuery, IObjectSet, IQueryable, IEnumerable, IQueryable, IEnumerable where TEntity : class
It’s important to recognize that an ObjectSet is not a LINQ to Entities query. LINQ to Entities comes into play when you write LINQ queries against this ObjectSet. However, as shown in Figure 3-7, both IObjectSet and ObjectQuery implement IQueryable (as well as other interfaces), which is a LINQ query type. IQueryable contains metadata about the query, such as the query expression and the provider being used. ObjectQuery is an IQueryable with additional query details that are specific to Entity Framework queries. By inheriting from ObjectQuery, ObjectSet gains the Entity Framework-specific attributes as well.
Figure 3-7. ObjectSet deriving much of its functionality from ObjectQuery, IObjectSet, and various IQueryable and IEnumerable interfaces
IQueryable is a common link between ObjectSet/ObjectQuery and LINQ to Entities queries because a LINQ to Entities query is also an IQueryable.
Once a query has been executed, IQueryable exposes its query metadata as well as the new query results. The query results inside IQueryable are described as an “enumerable type,” based on .NET’s IEnumerable interface. An IEnumerable allows you to enumerate or iterate ObjectQuery, ObjectSet, and LINQ to Entities | 67
Download from Library of Wow! eBook
through each item in the collection as you did in the preceding code sample (i.e., in foreach). An ICollection is an enhanced IEnumerable. Whereas an IEnumerable is readonly, the more familiar Collection class allows you to perform additional actions, such as adding or removing items from the group.
Terminology: IQueryable and IEnumerable It is important to be familiar with the terms IQueryable and IEnumerable because they are used frequently when discussing LINQ (not just LINQ to Entities) and Entity Framework queries. Although the phrase “this query returns a collection” is easier for developers to understand, the phrase “this query returns an IQueryable/IEnumerable” is more technically correct.
Querying with EntityClient to Return Streamed Data There is still one additional way to query the EDM: via EntityClient. EntityClient differs from LINQ to Entities and Object Services because it does not materialize objects. Instead, it streams data back to the requesting application as rows and columns in an EntityDataReader, which implements DbDataReader. If you have experience with ADO.NET, EntityClient is comparable to SqlClient, OracleClient, and other client providers; these clients return SqlDataReader, OracleDataReader, and so forth, which also inherit from DbDataReader. A data reader represents data in rows and columns. With the familiar DataReaders, each “cell” contains a scalar value—in other words, a primitive type such as a string or an integer. For example: Column 1
Column 2
Column 3
Row 1
1
John
Doe
Row 2
2
Jessica
Rabbit
Row 3
3
Cecil
De Mille
EntityDataReaders are designed to represent the entities and relationships that exist in an EDM; therefore, scalar data is not enough. An EntityDataReader has the ability to return data as shaped results. In an EntityDataReader, the cells in the preceding example could contain not only scalar values, but also an entire DbDataReader, a DbDataRecord (a single row from a DbDataReader), or even an EntityKey object. You saw EntityKey as a property of an entity in the EDM you built in Chapter 2; the EntityKey class is a full
class implementation based on that property, which you will learn more about in Chapter 10.
68 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
EntityClient uses Entity SQL for its query syntax and contains methods and properties
that will be familiar if you have worked with ADO.NET previously, including connections, commands, parameters, and transactions. The next example will give you a chance to work with EntityClient. Following the example is an explanation of the code. 1. Add the following namespace declarations to the beginning of the code file: using System.Data.EntityClient;
2. Add the method in Example 3-10 to your existing code to perform the same query you wrote earlier with LINQ to Entities and Object Services. This time you will be using the EntityClient provider. Example 3-10. Querying with EntityClient static void EntityClientQueryContacts() { using (EntityConnection conn = new EntityConnection("name=SampleEntities")) { conn.Open(); var queryString = "SELECT VALUE c " + "FROM SampleEntities.Contacts AS c " + "WHERE c.FirstName='Robert'";
}
EntityCommand cmd = conn.CreateCommand(); cmd.CommandText = queryString; using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess | CommandBehavior.CloseConnection)) { while (rdr.Read()) { var firstname = rdr.GetString(1); var lastname = rdr.GetString(2); var title = rdr.GetString(3); Console.WriteLine("{0} {1} {2}", title.Trim(), firstname.Trim(), lastname); } } conn.Close(); Console.Write("Press Enter..."); Console.ReadLine();
}
3. Call this new method from the Main method. You may want to comment out the call to QueryContacts so that only the new method is run.
Querying with EntityClient to Return Streamed Data | 69
Download from Library of Wow! eBook
4. Press F5 to test the new method. The result will be similar to the previous two queries. There is a bit to explain regarding the code for calling the EntityCommand.
EntityConnection and the Connection String With other client providers, the connection connects directly to the data store. However, the EntityConnection provides a connection to the EDM. When you created the model with the ADO.NET Entity Data Model Wizard, you may remember seeing the odd connection string in the wizard’s page where you selected the connection. An EntityConnection string consists of pointers to the EDM XML metadata files as well as a database connection string. The wizard wrote the EntityConnection string into the app.config file. You can open this file from the Solution Explorer and see that the ConnectionString named SampleEntities is composed of three parts: the metadata, provider, and provider connection string. The metadata contains file path pointers to the three files that are created from the model when the project is built. The data provider refers to the SqlClient provider that is being used to connect to the SQL Server database in this example. And finally, the provider connection string is a standard database connection string: metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl; provider=System.Data.SqlClient; provider connection string= "Data Source=MyServer; Initial Catalog=ProgrammingEFDB1; Integrated Security=True; MultipleActiveResultSets=True"
The res://* in the metadata indicates that the files are embedded into the assembly file of the project that contains the model and its classes. This is the default, although you can specify that the files be saved to the filesystem. You’ll learn more about this in Chapter 8.
EntityConnection provides an easy way to reference the connection string in the app.config file, which is to set a name property to the same name of the connection string: for example, "name=SampleEntities". As you saw in Example 3-10, the quotes are
required.
70 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
EntityCommand Creating the EntityCommand is no different from creating any other provider command and setting its CommandText. The CommandText here is the Entity SQL expression defined in the variable, queryString.
ExecuteReader With EntityClient, the SequentialAccess CommandBehavior is required for the ExecuteReader method. With other DbDataReaders, rows must be accessed sequentially, but the columns within the rows need not be. This rule exists to control memory consumption. You can combine the SequentialAccess behavior with CommandBehav ior.CloseConnection. CloseConnection is a commonly used (and highly recommended) behavior to use with ADO.NET dbCommand as another assurance that an unused connection does not inadvertently remain in memory.
Forward-Only Access to the Fields DbDataReaders are streams of data and are, by definition, forward-only. This also means
that the columns must be read in this way, which made the code in Example 3-10 a little cumbersome. In the string concatenation, you want to combine the fields to read Title FirstName LastName. But this is not the order of the fields returned in the DataReader. Title is the fourth column in the row, whereas FirstName is the second column and LastName is the third; therefore, you cannot read the Title data first, and instead must read the fields in the order in which they are streaming. That is why this method creates the variables prior to building the string—so the data can be extracted in sequential order. Once the variables exist, you can build the string. This is an important lesson to remember, regardless of how you plan to use the streamed data returned by the EntityClient.
Translating Entity Queries to Database Queries Although we will explore query processing in detail later in the book, you may already be wondering what kind of query the Entity Framework is sending to your database. The Entity Framework will break down the LINQ or Entity SQL query into a command tree and, with the help of the EDM and the database provider, will create another command tree that is specific to the database.
Translating Entity Queries to Database Queries | 71
Download from Library of Wow! eBook
Command trees will be familiar to hardcore database geeks and computer science majors. If you don’t fit into either group, MSDN defines it’s DbCommandTree class as “an abstract class that is used to represent queries, Data Manipulation Language (DML) operations and function/ procedure invocations.”
You can imagine how flexible the API needs to be to pull this off no matter what query you write. Although the examples so far have been simplistic, it is possible to write very complex LINQ to Entities or Entity SQL queries. The Entity Framework needs to be able to deal with anything you throw at it. Therefore, the resulting store queries may not look exactly the same as you might write them directly in your database’s query syntax, because they are being constructed in a somewhat formulaic manner. Sometimes the queries may look more complex but have no negative impact whatsoever on performance. But don’t expect this to always be the case. Here is the T-SQL rendered from the LINQ to Entities and Entity SQL queries that returned Contacts named Robert: SELECT [Extent1].[ContactID] AS [ContactID], [Extent1].[FirstName] AS [FirstName], [Extent1].[LastName] AS [LastName], [Extent1].[Title] AS [Title], [Extent1].[AddDate] AS [AddDate], [Extent1].[ModifiedDate] AS [ModifiedDate] FROM [dbo].[Contact] AS [Extent1] WHERE [Extent1].[FirstName] = 'Robert'
Both queries result in the same T-SQL because they are fairly simple queries.
Pay Attention to the .NET Method’s Impact on Generated SQL In the end it is the actual ADO.NET provider—for example, System.Data.SqlClient or perhaps a third-party provider such as FirebirdSql.Data.FirebirdClient—that builds the actual query string to be executed by the database. The Entity Framework team put a great deal of effort into improving the SQL generated from the .NET 4 version of this product. If you are using the SqlClient provider that is part of .NET 4, it has been enhanced to produce more efficient T-SQL. A key improvement to look for in the T-SQL generated by System.Data.SqlClient is smarter translation of queries that use StartsWith or Contains. In .NET 3.5, Contains was not even supported. However, if you used StartsWith in a query—for example, Contacts.Where(c=>c.LastName.StartsWith("T"))—the database query that resulted performed poorly in the database. Now StartsWith and its newly supported siblings, EndsWith and Contains, all result in queries that leverage T-SQL’s LIKE operator, which takes advantage of indexing in SQL Server.
72 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
The previous version of Entity Framework generated queries that forced the database to perform a full table scan, which brought pain to the hearts of many database developers. The use of the LIKE operator in .NET 4 will be a relief to many database professionals.
Entity Framework also has many opportunities to tune the performance of a query as it moves along the query pipeline to the ADO.NET provider. Improvements in the query pipeline benefit all of the database providers that support Entity Framework. The August 5, 2009, ADO.NET Team blog titled “Improvements to the Generated SQL in .NET 4.0 Beta1” lists the Beta 1 improvements and discusses changes that appeared in the Beta 2 version of Visual Studio 2010. All of these changes are in the final release. The URL for this post is http://blogs.msdn.com/adonet/archive/2009/08/05/improve ments-to-the-generated-sql-in-net-4-0-beta1.aspx. The MSDN blogs were revamped in 2010. The original URLs should automatically resolve to the new URLs. However, if you do have a problem getting to this or any other MSDN blog posts pointed to throughout this book, you can specify the new locations by adding a “b/” to the path between msdn.com/ and the specific blog. For example, if the pointer is to http://blogs.msdn.com/adonet, you would change that to http://blogs.msdn.com/b/adonet.
It’s wonderful that the improvements have been made, but this doesn’t mean you are off the hook. You should always pay attention to what’s happening in your applications and in your database regardless of what tool or framework you are using for data access. You have a number of options for watching the queries hit your database. An Entity Framework method called ToTraceString allows you to look at some queries at runtime. With ToTraceString, you can inspect some, but not all, queries and you cannot see updates. You will learn more about ToTraceString in Chapter 10. If you are using SQL Server Developer and later, you can watch SQL Profiler. Visual Studio 2010’s new IntelliTrace feature will expose the queries and updates to the database. However, IntelliTrace will not pick up queries generated by a feature called lazy loading. (Lazy loading is introduced later in this chapter.) Another option is a fantastic third-party tool called EFProf from Hibernating Rhinos.
Translating Entity Queries to Database Queries | 73
Download from Library of Wow! eBook
A Bit of Entity Framework Profiler History I was elated that Oren Eini (a.k.a. Ayende Rahien), the author of NHProf and LINQ to SQL Prof, wanted to create a version for Entity Framework, and I spent a bit of time helping him out—but not in an office. The collaboration began at an after-conference party at the Øredev Conference in Malmö, Sweden, then continued later that evening in the back corner of a local bar. For the curious, some evidence of that is captured on Steve Bohlen’s blog at http://unhandled-exceptions.com/blog/index.php/2009/11/10/trav elogue-oredev-2009-wrap-up/. I merely played the muse (and guide through the Entity Framework APIs) to Oren’s genius as he hammered out his solution.
What About SQL Injection Attacks? SQL injection attacks can be used to insert commands into your queries that can display data or impact your database by leveraging your connection to the database and any permission your connection may have. This is a common threat to database applications, and you can find plenty of information in books and on the Web about how to avoid it. Anytime a variable is used in a LINQ to Entities query, the generated store query will be parameterized, thus avoiding SQL injection. In Entity SQL, most SQL injections won’t even evaluate as correct Entity SQL syntax, and therefore they cannot be executed. However, someone could attempt to inject Entity SQL. ObjectParameters can help avoid this problem. Chapter 20 addresses security in the Entity Framework, and you can read more about this topic there.
Avoiding Inadvertent Query Execution You may have noticed when debugging some of the queries in this chapter that next to the Results property it says “Expanding the Results View will enumerate the IEnumerable.” This is a very important behavior to be aware of and it impacts all LINQ queries (including in-memory queries and LINQ to SQL) as well as ObjectQuery queries. Whether you do it in debug mode or in code, every time you do anything to force the enumeration or execution of a query, the query will be executed on the database again. In the Entity Framework, this means that even if you have already done something to enumerate the query (e.g., bound it to a control, run it through a foreach iteration, called ToList() on the query, etc.), anytime you repeat one of these methods that forces execution it will go back to the database, run the query again, bring back the results again, and then merge the results into the cache that’s in memory.
74 | Chapter 3: Querying Entity Data Models
Download from Library of Wow! eBook
Once you have executed a query, you will most likely want to work with the results and no longer the actual query. When querying for sets of data, I recommend calling ToList() to force query execution and provide a variable to work with. That variable will be a System.Collections.Generic.List (List(Of T) in VB) of whatever type the query returns. You can also use ToArray() if that better suits your needs. When returning a single result, you should consider using the Single() or SingleOrDefault() method. First() and FirstOrDefault() can also be used, but will additionally work when the query might return multiple results but you wish for only the first one. We’ll look at the Single and First methods in a little more detail in Chapter 4. Another method to be aware of is ObjectQuery.Execute, which will also force execution. Execute returns a System.Data.Objects.ObjectResult. ObjectResult has some special functionality that makes it the right choice for data-binding scenarios; you’ll see ObjectResult in later chapters where you will be doing data binding in various applications. Execute takes a MergeOption parameter that specifies how the query results should be merged into existing entities; you’ll learn more about MergeOption in Chapter 10. But the ObjectResult from Execute is forward-only. You’ll learn more about the limitations this creates in Chapter 9. I use ToList and other methods throughout this book to avoid accidentally repeating query execution. This is my practice in production applications as well.
Summary In this chapter, you learned about the many different ways to query an EDM using LINQ to Entities, the ObjectQuery with Entity SQL, LINQ methods, query builder methods, and streaming data with EntityClient. Along the way, you learned about many of the fundamentals that will make it easier for you to construct intelligent queries. In Chapter 10, you will spend some time comparing how these queries are processed so that you can see the different paths the various query methods embark on as they are resolved. Chapter 20 will cover the performance differences between the various query methods and will demonstrate ways to affect performance directly. Although this chapter focused on a single simple query with a twist here and there, the next two chapters will delve more deeply into querying, demonstrating ways to retrieve more complex data using all of the methods you are now familiar with.
Summary | 75
Download from Library of Wow! eBook
Download from Library of Wow! eBook
CHAPTER 4
Exploring LINQ to Entities in Greater Depth
In Chapter 3, you wrote the same basic query over and over and over again. I hope you’ll agree that this was a great way to get exposure to the many different ways of writing queries against the Entity Data Model. There is a lot more to querying an EDM, however. You’ll need to learn about the flexibility you have for expressing complex queries, projecting data, combining and nesting queries, and writing parameterized queries. There are also nuances regarding what type of data is returned based on how you construct your queries. Sometimes you will get objects, as you saw in the examples in Chapter 3, but other times you will get unknown objects (anonymous types). It is also possible for Object Services queries to return rows and columns. You’ll need to know when to expect these varied forms of data to be returned. Covering all of this exhaustively would require hundreds of pages. Therefore, the goal of these next two chapters on LINQ to Entities and Entity SQL is to teach you the critical features and many of the possibilities, focusing on the most typically needed query features. You will learn how to project specific values (rather than entire objects) in queries, how to query across relationships, how to write nested queries and joins, and how to control when trips are made to the database. Along the way, I will introduce and explain additional new concepts to help you truly understand the workings of the Entity Framework. This chapter will focus on LINQ to Entities and introducing new concepts. The queries you build here will be demonstrated using Entity SQL in Chapter 5. Throughout the rest of the book, you will see variations on queries that take advantage of even more techniques as we use queries in real-world examples. A number of resources provide many specific examples of queries. Here you will learn some of the more common query tasks so that you will know enough to write queries without constantly having to search online for the perfect example of what you are 77
Download from Library of Wow! eBook
trying to accomplish. It is also useful to check out resources such as the 101 LINQ Examples on MSDN (for VB and for C#), the number of great books dedicated to LINQ, and the Entity Framework Samples, which provide a great variety of query examples with helpful commentary. Due to some syntax differences between VB and C# when creating LINQ expressions, you will see a number of Visual Basic examples in this chapter along with the C# versions when the difference is significant.
Getting Ready with Some New Lingo Here is a list of terms used in this chapter (and throughout the book) that may be new to you: Projection Selecting specific properties or expressions in a query, rather than the entity being queried. For example: from c in context.contacts select c.firstname + c.last name, c.contactID. Eager loading Requesting that related data be returned along with query results from the database. For example: when querying contacts, eager-load their addresses. The contacts and their addresses will be retrieved in a single query. Deferred loading Delaying the loading of related data until you specifically request it. For example: when working with query results for a particular contact, you can make a request to retrieve that contact’s addresses from the database. When deferred loading happens automatically (implicitly), it is called lazy loading. Navigating Moving from an entity to its related data. For example: navigate from a contact to its addresses using the contact.Addresses property.
Projections in Queries So far, the queries you have seen return an entire object, comparable to writing a SELECT SQL query requesting every column in a table. By returning an entire object in your query, you will get all of the benefits associated with the entity classes—the most important of which is the ability to keep track of changes to an entity class for database updates. Often in SQL, you will select particular columns to return from a table (SELECT Firstname, Lastname FROM Contact) or from a set of joined tables. This is referred to as projection. With LINQ or Entity SQL queries you can shape your results by picking particular properties or expressions rather than entities. You can also select properties from related data. 78 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
In the Chapter 3 queries, you returned an entire object but used only the Title, FirstName, and LastName properties. You can rewrite those queries to return only these three properties. As long as you won’t need to modify and update these results, a projection will suffice.
Projections in LINQ to Entities To see how projections work, you can continue modifying the QueryContacts method that you worked on in Chapter 3. Replace the latest version of the query with the query in Example 4-1. The difference from earlier LINQ queries is that rather than ending with select c to select the entire contact, you are selecting only a few properties. Example 4-1. Simple LINQ to Entities query with projection in VB and C# VB
Dim contacts = From c In context.Contacts Where c.FirstName= "Robert" _ Select New With {c.Title, c.LastName, c.FirstName}
C#
var contacts = from c in context.Contacts where c.FirstName=="Robert" select new { c.Title, c.FirstName, c.LastName };
Why are we back to using Dim and var again? You’ll see the reason shortly in the section titled “Implicitly typed local variables” on page 81.
VB and C# Syntax Differences You may have noticed the syntax differences between VB and C# projections. This is not particular to LINQ to Entities, but it is common for all implementations of LINQ. C# requires that you use select new {...} when projecting. Visual Basic is more lenient. The most explicit syntax for VB is Select New With {...} as in Example 4-1, though you could write the Visual Basic query in this simpler format: From c In context.Contacts _ Where c.FirstName= "Robert" _ Select c.Title, c.LastName, c.FirstName
There are plenty of other nuances to LINQ projections in both languages. For example, you can project into predefined types. In addition, C# projections always create immutable (read-only) results, whereas VB allows the creation of immutable and mutable results. You can learn more about projecting with LINQ in the MSDN Library and from the many great resources that focus on LINQ.
Projections in LINQ to Entities | 79
Download from Library of Wow! eBook
LINQ Projections and Special Language Features A number of language and compiler features that were added to Visual Basic and C# (in the VB 9 and C# 3.0 versions that were released along with Visual Studio 2008 and .NET 3.5) have made it easier for developers to implement LINQ projections. We’ll examine several of these in this section, including anonymous types and implicitly typed local variables. If you hover your mouse pointer over the contacts variable, when the code is not running, the DataTip will show you what the query returns. It’s an IQueryable of an “anonymous type,” rather than an IQueryable of contact types. The anonymous type is a result of the projection in your query, which returned results that don’t match a defined type. The DataTips and debuggers in Visual Basic and C# often show different information. In this case, the difference is interesting, as you can see in Figures 4-1 and 4-2.
Figure 4-1. The DataTip in Visual Basic, which shows the new contacts variable to be an IQueryable(Of )
Figure 4-2. The DataTip in C#, which shows even more details regarding the anonymous type
Anonymous types What is this anonymous type that the LINQ to Entities projection is returning? The anonymous type is a language enhancement that was introduced in Visual Basic 9 and C# 3.0 that allows compilers to work with types that were not previously defined. Anonymous types are generally used for on-the-fly types that won’t be used elsewhere in the application. You cannot even pass them from one method to another. Anonymous types relieve you from having to define a class for every type, even if the type is to be used only briefly. Yet an anonymous type returned by the query is still strongly typed, which means you can easily interact with it in the code following its creation. The sidebar “Wrapping Your Head Around Lambdas” on page 62 includes a link to an article by Anders Hejlsberg. The article contains a great introduction to anonymous types. Anonymous types are a powerful feature that you can use throughout .NET, but 80 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
they have special importance for LINQ queries because of their ability to allow projections that can return anything a developer can dream up. So, the query in Example 4-1 returned an anonymous type that doesn’t have a name, but has the properties Title, FirstName, and LastName. If you are still modifying the earlier query method, you can see a bit of .NET magic by removing the Console.Write Line method and retyping it. The anonymous type is strongly typed and recognized by IntelliSense. Pretty cool!
Anonymous Types and Updates Although later chapters will introduce the concepts of tracking changes to entities and performing updates to the database, it is important to keep in mind that this takes place only with entities defined in your model. Anonymous types do not participate in change tracking or updates. With any projections, it’s important to know whether the operation returns entities or anonymous types, because the result determines how the object can be used.
Implicitly typed local variables Another new compiler trick that you have been taking advantage of in some of the code samples so far is the use of implicitly typed local variables. In C# you use them with a new keyword, var, and in VB you use them with the existing Dim keyword. It is possible to declare variables without identifying their types. They will infer the type based on the value that is being set. Hasn’t it always seemed redundant to say something like Dim str as String="this is some text" or int MyInt=123? With implicitly typed local variables, Dim str="this is some text" and var MyInt=123 are enough. In the case of replacing int with var the benefit is not very obvious. Had that type been MyCustomType, suddenly var would look pretty convenient. This shortcut is not always a good thing, as it removes some of the explicitness of your code. I wrote a blog post on DevSource.com titled “How Visual Studio 2008 made me even lazier” (http://blogs.devsource .com/devlife/content/net_general/how_visual_studio_2008_made_me _even_lazier.html). There is an interesting discussion in the comments about the pros and cons of implicit typing. Throughout the book, I will attempt to declare types explicitly for the sake of clarity. However, in cases where the type name is quite long, you may find a var in its place.
Where implicitly typed local variables really shine, however, is with LINQ query projections, because there’s no way to say “Dim contacts as a thing with a Title, a FirstName, and a LastName.” Instead, you can write “Dim contacts (and just look at the Projections in LINQ to Entities | 81
Download from Library of Wow! eBook
other side of the equals sign to figure out what this is).” In this context, Dim in VB and var in C# essentially translate to “thing,” or for some readers, “whatever.” Run the application and you’ll see that, once again, the results are the same as they were previously. You can modify the Console.WriteLine command to include the Title property that is in the newest query. In Chapter 10, you will learn more about Object Services and all of the functionality it provides to objects returned by queries against the EDM. This will help you better understand the significance of returning anonymous types rather than entire entity objects defined by the EDM.
Implicit and explicit anonymous type creation You can project into anonymous types in a number of ways. For instance, it is possible to give a name to the returned variable, such as ContactName in Example 4-2. Example 4-2. Naming a projected anonymous type in LINQ in VB and C# VB
From c In context.Contacts _ Where c.FirstName = "Robert" _ Select ContactName = New With {c.Title, c.LastName, c.FirstName}
C#
from c in context.Contacts where c.FirstName == "Robert" let ContactName = new {c.Title, c.LastName, c.FirstName} select ContactName
C# does not allow naming in the SELECT statement; it has another operator, LET, that can be used for this purpose. There are so many ways to do projection and use anonymous types in LINQ queries. Here you are seeing just a small slice of what you can achieve, so be sure to look to the dedicated LINQ resources to expand your understanding.
Naming the anonymous type is more useful if this new type is a property of the projected results. In Example 4-3, a projection is used to project much more than some strings. It creates a new type with another anonymous type as the first property and the addresses of the contact as the second. I’m projecting the Addresses property here to highlight the projection. You’ll learn more about working with related data later in this chapter.
82 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
When you name the anonymous type, the property that results will have the name specified in the query. Notice that the property name is used later in the query for the Order By operator and when working with the results. Example 4-3. Anonymous types as properties var contacts = from c in context.Contacts where c.FirstName == "Robert" let foo= new { ContactName = new {c.Title, c.LastName, c.FirstName}, c.Addresses } orderby foo.ContactName.LastName select foo; foreach (var contact in contacts) { var name = contact.ContactName; Console.WriteLine("{0} {1} {2}: # Addresses {3}", name.Title.Trim(), name.FirstName.Trim(), name.LastName.Trim(),contact.Addresses.Count()); }
Figure 4-3 shows the shape of the new range variable, foo. The first property is the ContactName anonymous type.
Figure 4-3. A named anonymous type with a named anonymous type property
Unlike the ContactName anonymous type in this query, the Address entities that this query returns will participate in the change tracking and database updates.
Projections in LINQ to Entities | 83
Download from Library of Wow! eBook
Projections with LINQ Query Methods To project using LINQ’s method-based query syntax, you would use the Select method and then identify the properties you want in its parameter. The method-based query syntax requires the syntax for creating an anonymous type in the lambda (see Example 4-4). Example 4-4. Projecting using LINQ’s method-based syntax context.Contacts .Where(c => c.FirstName == "Robert") .Select(c => new {c.Title, c.LastName, c.FirstName})
Using Navigations in Queries One of the big benefits that the EDM lends to querying is that the relationships are built into the model and you won’t have to construct joins very often to access related data. Additionally, when using LINQ for the queries, the related data is presented via IntelliSense, which makes it very discoverable. Using the model, let’s take a look at some more queries, this time digging into associations. The model has only one association, that which lies between Contact and Address. The association provides two navigations—one from Contact to all of its related addresses and one from Address to its related contact. You can easily do projection, drilling into related entities, although drilling into a collection is different from drilling into a reference entity. For example, you can’t request Contact.Addresses.Street in a query. Contact to Addresses is a one-to-many relationship and Addresses is a collection, not a single entity. Street is not a property of the Addresses EntityCollection. However, you could select Address.Contact.LastName, because you would be navigating to a single entity. There is only one contact per address; therefore, there is no question regarding from which entity the query should retrieve the LastName.
Navigating to an EntityReference Recall that navigating to the “one” end of a one-to-one or many-to-one relationship is referred to as a navigation reference. The entity you are pointing to is referred to as an EntityReference, sometimes called an EntityRef. Chapter
19 will drill further into EntityReferences and EntityCollections, and how they are surfaced as navigation properties.
84 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
The LINQ query in Example 4-5 returns an anonymous type containing an address and its related contact. Example 4-5. Projecting into an EntityRef with LINQ to Entities var addresses = from a in context.Addresses where a.CountryRegion == "UK" select new { a, a.Contact };
Figure 4-4 displays the anonymous type that results in the debugger, where you can see that one property is the address record and the other is the contact.
Figure 4-4. The query results, which contain a new type with the address and its contact
When working with the results, you’ll have to drill into the new type’s properties (the Address and the Contact) and from there you’ll have to drill into their properties, as shown in Example 4-6. Example 4-6. Accessing the properties of an anonymous type foreach (var address in addresses) { Console.WriteLine("{0} {1} {2}", address.Contact.LastName, address.a.Street1, address.a.City); }
The first property is named a because it is using the variable name given in the query. If you want to be sure the property is called Address you can use that instead of the simpler a, or use LINQ syntax to rename the property: VB
Select New With {.Address = a, a.Contact}
C#
select new {Address= a, a.Contact };
Then you can work with address.Address in the data which results.
Although this may suit many scenarios in your applications, you may prefer to project individual properties from the reference navigation. Example 4-7 shows such a query using LINQ to Entities. This projection returns a new type with three properties. The first is an Address entity; the second and third are strings. Again, the property names are based on the query defaults—a, FirstName, and LastName.
Using Navigations in Queries | 85
Download from Library of Wow! eBook
Example 4-7. Combining properties from related entities var addresses = from a in context.Addresses where a.CountryRegion == "UK" select new { a, a.Contact.FirstName, a.Contact.LastName }; foreach (var address in addresses) { Console.WriteLine("{0} {1} {2} {3}", address.FirstName, address.LastName, address.a.Street1, address.a.City); }
Filtering and Sorting with an EntityReference You can filter and sort based on a property of an EntityReference whether or not you are selecting the related data. For example, you can select all addresses for contacts with a particular last name. The LINQ to Entities query in Example 4-8 sorts by Contact.LastName and filters on the Contact.AddDate field even though AddDate is not part of the results. Example 4-8. Filtering and sorting on reference properties from a in context.Addresses where a.Contact.AddDate > new System.DateTime(2009, 1, 1) orderby a.Contact.LastName select new {a, a.Contact.LastName};
Navigating to Entity Collections Querying with related data is straightforward when the related data is a single entity, but what about when the navigation property is an EntityCollection such as Contact.Addresses? Let’s start with a simple scenario that you have seen a few times already in this chapter: returning a contact and its collection of addresses. To highlight the difference between the original properties and the results, the EntityCollection in the new type is given a random name, as shown in Example 4-9. Example 4-9. Projecting an EntityCollection with LINQ var contacts =
from c in context.Contacts select new {c, Foos = c.Addresses};
This query creates a new anonymous type with two properties. The first is the Contact and the second is Foos, which is the EntityCollection of Addresses related to that Contact. You can enumerate through the results, and then, for each result, enumerate through the collection of the Foos property, as shown in Example 4-10. 86 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
Example 4-10. Enumerating over shaped data that includes an EntityCollection foreach (var contact in contacts) { Console.WriteLine("{0}: Address Count {1} ", contact.c.LastName.Trim(), contact.Foos.Count); foreach (var foo in contact.Foos) { Console.WriteLine(" City= {0}", foo.City); } }
Projecting Properties from EntityCollection Entities If you wanted to select particular properties such as Street and City from each Address of each Contact, the method you should use to build the query depends on what shape you want the results to be.
Shaped results You could shape the data similar to the previous example, but instead of a set of complete address entities as the Foos property, you can project some of the address properties. This would result in a set of anonymous types, named StreetsCities instead of Foos, in the second property. You can achieve this with a nested query, a feature we’ll look at more closely later in the chapter. For now, you can see in the query in Example 4-11 that the third property, StreetsCities, contains the results of querying the Contact’s Addresses. Example 4-11. Projecting values from an EntityCollection from c in context.Contacts select new {c.FirstName, c.LastName, StreetsCities = from a in c.Addresses select new { a.Street1, a.City } }
The anonymous type that is returned has the properties FirstName and LastName, along with a collection of anonymous types with Street and City properties. The debugger screenshot in Figure 4-5 displays the new type.
Figure 4-5. The newly shaped anonymous type
Using Navigations in Queries | 87
Download from Library of Wow! eBook
Flattened results Another way to project into the addresses is to merely turn the query around. That is, query the addresses and their contact data to flatten the results, as shown in Example 4-12, so that the data is no longer shaped. Example 4-12. Flattening the related data var contacts = from a in context.Addresses orderby a.Contact.LastName select new {a.Contact.LastName, a.Contact.FirstName, a.Street1, a.City};
This will result in a single type with four properties, but contacts with multiple addresses will appear multiple times, as you can see in this section of the results. For instance, Katherine Harding and Keith Harris each have two results: Hanson, John: 825 W 500 S, Bountiful Harding, Katherine: 52560 Free Street, Toronto Harding, Katherine: 25 Flatiron Blvd., Vancouver Harrington, Lucy: 482505 Warm Springs Blvd., Fremont Harris, Keith: 3207 S Grady Way, Renton Harris, Keith: 7943 Walnut Ave., Renton Harui, Roger: 9927 N. Main St., Tooele Hass, Ann: Medford Outlet Center, Medford
Filtering and Sorting with EntityCollections Although you can easily use related data in projections or for filtering, sorting, and other operations, it is important to keep in mind that when the related data is in a collection, you need to leverage operations that can be performed on a set of data. For example, if you want to find contacts with addresses in the United Kingdom (represented as UK in the database), you can use the ANY method in LINQ to Entities (see Example 4-13) or the EXISTS operator in Entity SQL (which you’ll see in the next chapter) to search the contact’s addresses. The LINQ query uses a predicate to provide the condition for ANY. Example 4-13. Filter condition provided by an EntityCollection with LINQ from c in context.Contacts where c.Addresses.Any(a => a.CountryRegion == "UK") select c;
Aggregates with EntityCollections Aggregates perform calculations on a series of data. Aggregate methods include Count, Sum, Average, Min, and Max. You may not want the entire collection of addresses, but rather some aggregated information about that collection.
88 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
Aggregates in LINQ to Entities Aggregating data with LINQ is easyusing one of the aggregate methods such as Count; simply append the method to the collection name. The Count method will return the count of the items in the collection (see Example 4-14). Example 4-14. Using the Count aggregate method in LINQ to Entities from c in context.Contacts select new {c.LastName, c.Addresses.Count};
Other types of aggregates, such as Max, require a specific value to aggregate. You can supply that value using a lambda expression, as shown in Example 4-15. Example 4-15. Using an aggregate method with a lambda in LINQ from c in context.Contacts select new { c.LastName, MaxPC = c.Addresses.Max(a => a.PostalCode)};
It’s important to name the property returned by the aggregate function, because LINQ is unable to derive one based on the method. If you forget to do this, both VB and C# will give a compiler error explaining the problem. Visual Basic has an Aggregate operator for LINQ that you can use in place of FROM in your LINQ queries. Check the MSDN Library topic “Aggregate Clause (Visual Basic)” for more information.
Aggregates in LINQ Methods The LINQ aggregates are methods, not query operators. Therefore, they work very naturally with the LINQ query methods. Example 4-16 uses the Max aggregate as one of two projected values to be returned. Example 4-16. A LINQ method syntax query using an aggregate context.Contacts .Select((c) => new { c.LastName, MaxCode = c.Addresses.Max(a => a.PostalCode) });
This query does two interesting things with the lambdas. First it uses a lambda expression to specify what values should be projected: LastName and MaxCode. Once the variable, c, has been declared, the function projects an anonymous type consisting of LastName as the first property and MaxCode as the second. MaxCode is defined by using the Max aggregate on the Addresses collection of the contact.
Using Navigations in Queries | 89
Download from Library of Wow! eBook
Joins and Nested Queries Although associations in the EDM minimize the need for joins in queries, sometimes a relationship may exist but there is no association to represent the relationship. In these and other cases, you can use nested queries or joins to bring the data together.
From the Source: Should You Even Use Joins? Zlatko Michailov, former Entity SQL program manager at Microsoft, writes in his blog: “A well defined query against a well defined entity data model does not need JOIN. Navigation properties in combination with nesting sub-queries should be used instead. These latter constructs represent task requirements much more closely than JOIN does.”* You may not always have the opportunity to define a model the way you’d like, because of limitations in the database or your domain. What you should take away from Zlatko’s quote is that using JOINs should not be your first stab at expressing a query. I’ve seen clients using JOINs in queries simply because they don’t understand yet how to take advantage of navigation properties in their queries.
LINQ to Entities provides a JOIN operator as well as GROUPJOIN. Entity SQL provides a variety of options in the JOIN FROM clause, including inner joins, as well as left, right, and full outer joins. It also enables joining multiple collections separated by commas.
Joins The vOfficeAddresses entity in the current model has all of the contact properties except for the contact’s Title. Because there is no association between vOfficeAddresses and Contact, you will need to use JOIN to combine the vOfficeAddresses entity properties with the Title property. You could, of course, add the association to the model in this case, but then there would be no lesson here, would there?
Example 4-17 shows the syntax of a LINQ JOIN. Example 4-17. JOIN syntax for LINQ FROM [variableA] IN collectionA JOIN [variableB] IN collection ON variableA.commonproperty EQUALS variableB.commonProperty SELECT .....
* http://blogs.msdn.com/esql/ (November 1, 2007).
90 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
Example 4-18 shows how to combine data from Contact entities and vOfficeAddresses entities using the JOIN. Example 4-18. A LINQ to Entities query using a JOIN from c in context.Contacts join oa in context.vOfficeAddresses on c.ContactID equals oa.ContactID select new { oa.FirstName, oa.LastName, c.Title, oa.Street1, oa.City, oa.StateProvince };
This provides an inner join where only entities with matching ContactIDs are returned. Any contacts with no match in the vOfficeAddresses will not be returned. vOfficeAddresses with no match in Contacts will not be returned either.
Nested Queries Both LINQ and Entity SQL provide the ability to nest queries, and you have already seen some examples of this. When you write a query, anywhere a value is expected you can use another query in its place, as long as that query returns an acceptable type. You can use a nested query in place of an expression or a collection, as you will see in the following examples. The goal of the previous JOIN queries was to return properties from a Contact entity combined with properties from the vOfficeAddresses entities where the ContactID matches.
Using a nested LINQ query as a projection Example 4-19 shows how to express the previous query in LINQ using a nested query instead of a JOIN. The query uses a nested query (highlighted) combined with the FirstOrDefault method in place of a projected value to return results from vOfficeAddresses. Example 4-19. Nested query in place of a SELECT expression in LINQ from oa in context.vOfficeAddresses select new { oa.FirstName, a.LastName, Title = (from c in context.Contacts where c.ContactID == oa.ContactID select c.Title).FirstOrDefault(), oa.Street1, oa.City, oa.StateProvince; }
There are a few notable twists to this query. The first should be familiar: an anonymous type is not able to automatically name the return from the nested query. Therefore, it is given the name “Title”. The second twist is that the subquery returns an IQueryable of String, not just a string, which is why the FirstOrDefault method is appended to the query.
Joins and Nested Queries | 91
Download from Library of Wow! eBook
Using a nested LINQ query as the collection to be queried You can also use the nested query in place of the collection being queried. The nested query merely returns another collection to be queried. Let’s start with a basic example. Rather than querying all vOfficeAddresses, you could create a subquery that returns only vOfficeAddresses in Ontario and then query against that. Example 4-20 is simplistic and could easily be expressed without the nested query. The technique can be useful when you are attempting to express queries that are much more complex. Example 4-20. Nested query in place of a target collection in LINQ var contacts = from add in (from oa in context.vOfficeAddresses where oa.StateProvince == "Ontario" select oa) select ...
You can benefit from using nested queries to help with complicated queries by separating the nested query from the main query. On its own, this particular example doesn’t seem very useful, but imagine being able to use subqueries to redefine the universe of vOfficeAddresses from which to query, and then passing that into different methods which will perform additional queries on that subset. Example 4-21 ties a subquery to a variable and then uses that variable in another query. The second query is complex enough, using another nested query to join vOfficeAddresses back to Contact. Breaking up the query makes the code much more readable. When the query is executed, the Entity Framework will create a single query from the combined expressions. Don’t forget the importance of knowing what is going on at the database level by using some type of profiler, as suggested in Chapter 3.
Example 4-21. Breaking a nested query out of the main query in LINQ var universe = from oa in context.vOfficeAddresses where oa.StateProvince == "Ontario" select oa; var query = from oa in universe select new { oa, contact = (from c in context.Contacts where c.ContactID == oa.ContactID select c)
92 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
}; var AddressesWithContacts = query.ToList();
You can’t separate out a nested query that’s inside a projection, as in Example 4-21, because its filter condition is dependent on the main query. An Order operator in a subquery will be ignored. The main query controls ordering.
Grouping Both LINQ and Entity SQL provide operations for grouping data. You can use grouping in connection with aggregates or to shape data. LINQ to Entities has a Group operator (literally Group By in Visual Basic and Group in C#) and a GroupBy method (with eight overloads). Entity SQL provides a GROUP BY operator and a GroupBy query builder method. The results of the grouping can use automatic naming, and in other cases can be explicitly named. In addition, an INTO GROUP clause is required in Visual Basic. C# has an optional INTO clause. The constructs for VB and C# are quite different and it’s easiest to explain them with examples. Example 4-22 shows the simplest form of grouping in LINQ for both Visual Basic and C#. Example 4-22. Simple grouping in LINQ to Entities in VB and C# VB
From c In context.Contacts Group By c.Title Into Group
C#
from c in context.Contacts group c by c.Title into mygroup select mygroup
The result of this query is an IQueryable of an Entity Framework class called Grouping; more specifically, System.Data.Objects.ELinq.InitializerMetadata.Group ing. In our example, it’s a Grouping. This is something like a key/ value pair where the key is K (the string in our example) and the value is an IEnumera ble of T (e.g., the group of Contact types). The results, therefore, are a set of these key/value pairs. If we select one of the groupings, as you can see in Figure 4-6, VB automatically names the property containing the title as “Title”.
Grouping | 93
Download from Library of Wow! eBook
Figure 4-6. The VB result, which contains a Title property and a Group property that contains three contacts
By default, C# uses the word Key as the name for the key of the grouping and doesn’t name the property that contains the grouped records, as you can see in Figure 4-7.
Figure 4-7. Default C# grouping
VB allows you to specify the property name rather than use the default. In Visual Basic, to change the Title property of the preceding query to MyTitle, you would use the syntax Group By MyTitle=c.Title. In VB, the Group property is available to access the group. You can rename this as well. For example, Into MyGroup = Group renames the property to MyGroup.
Naming Properties When Grouping The optional INTO clause in C# allows you to specify a group name, but this is not exposed as a property. You specify the name with INTO so that you can perform further functions on the group. Note that in C#, using the INTO clause requires that you also use the SELECT clause. The Key property is then accessible as a property of the group. With the group specified, it is now possible to explicitly name the properties in C#. LINQ queries in Visual Basic will imply a SELECT statement if it is not used. In this case, the query will still return Title and MyGroup by default without specifying SELECT. Of course, you can shape the data further by specifying your own output with an explicit SELECT operator.
94 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
Example 4-23 demonstrates these changes to the previous queries. Example 4-23. LINQ Group By with explicitly named groups and targets in VB and C# VB
From c In context.Contacts _ Group By c.Title Into MyGroup = Group
C#
from c in context.Contacts group c by c.Title into MyGroup orderby MyGroup.Key select new {MyTitle = MyGroup.Key, MyGroup};
Chaining Aggregates Visual Basic provides a simple way to use aggregates in grouping queries, by specifying one or more aggregates in the INTO clause separated by commas. In Example 4-24, your result will contain the properties Max and Count. Example 4-24. Chained aggregates in VB LINQ VB
From c In context.Contacts _ Group By c.Title Into MyGroup = Group, _ Max(c.AddDate), Count()
In C#, you need to explicitly project these properties in the Select clause using methods and predicates, as shown in Example 4-25. Example 4-25. Combining aggregates in C# LINQ C#
from c in context.Contacts group c by c.Title into MyGroup orderby MyGroup.Key select new {MyTitle = MyGroup.Key, MyGroup, Max = MyGroup.Max(c => c.AddDate), Count = MyGroup.Count()}
Filtering on Group Conditions There is so much more that you can do with grouping in LINQ. For now, we’ll take a look at one more variation: filtering on the grouping condition. The Title fields in the sample data contain Mr., Mrs., Ms., Sr., and a few other titles. Also, some contacts have no title. Perhaps you would like to group on title, but exclude empty titles. To filter what is being grouped, such as “only group contacts with something in the Title field,” you can apply the filter to the control variable, Title, to make sure it contains a value. You may, however, want to filter on a property of the Group. With LINQ you can continue to use the WHERE operator, as shown in Example 4-26.
Grouping | 95
Download from Library of Wow! eBook
Example 4-26. Filtering on a Group property with LINQ VB
From c In context.Contacts _ Group By c.Title Into MyGroup = Group, Count() _ Where (MyGroup.Count() > 150)
C#
from c in context.Contacts group c by c.Title into MyGroup where MyGroup.Count() > 150 select new { MyTitle = MyGroup.Key, MyGroup, Count = MyGroup.Count()};
In LINQ, you will also need to be aware of variables going out of scope, as in the Visual Basic query shown in Example 4-27, which won’t compile. The a in Group by a.CountryRegion is out of scope because by this point in the query, you are working with the anonymous type returned by the Select statement. And the Select does need to go before the Group By. Example 4-27. An out-of-scope variable preventing this query from compiling VB
From a In context.Addresses _ Select a.Contact.FirstName, a.Contact.LastName, a.CountryRegion _ Group By a.CountryRegion Into MyGroup = Group, Count() _ Where (MyGroup.Count() > 150)
You can avoid this problem by naming the anonymous type, and then grouping by a field within the name, as shown in Example 4-28. Example 4-28. Naming variables to keep them from going out of scope VB
From a In context.Addresses _ Select c = New With {add.Contact.FirstName, a.Contact.LastName, _ a.CountryRegion} _ Group By c.CountryRegion Into MyGroup = Group
C#
from a in context.Addresses let c= new {a.Contact.FirstName, a.Contact.LastName, a.CountryRegion} group c by c.CountryRegion into MyGroup where (MyGroup.Count() > 150) select MyGroup;
Both the Visual Studio documentation and the ADO.NET Entity Framework documentation and samples can provide you with an astounding array of data shaping that you can perform with Group By/groupby in LINQ, and even then there are still many more. See “Finding More Query Samples” on page 109 for links to these resources.
96 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
Like everything else this chapter has covered so far, we have only skimmed the surface of GROUP BY in Entity Framework queries. You will see more uses throughout this book and can find more details (and plenty of rules) in the documentation. The rest of this chapter will explain some important concepts that have been exposed by the queries you’ve seen so far.
LINQ Compiled Queries and Entity SQL Cached Queries One of the expensive processes of executing queries is in the query compilation. This is when the query is transformed into the proper query to be sent along to the database. LINQ to Entities has a feature called precompilation whereby you can compile a query in advance and access that compiled version as needed. Even if some of the query parameters change, such as searching for LastName="Smith" and then searching for LastName="Holbert", the precompiled query will be used. This has a huge impact on performance, and Microsoft recommends that you use precompilation for any queries that might be called repeatedly. Entity SQL has the ability to cache its queries, and does this by default. The performance benefit is similar to that of using precompiled LINQ queries. Chapter 20 explores both of these features.
Shaping Data Returned by Queries Whether you write a query that returns entities, anonymous types, DbDataRecords, or DbDataReaders, you can return shaped data. You’ve seen this in several of the previous queries, with a variety of shaped results. How you use this data depends on how the data is shaped. Let’s take a further look at the results of some of the earlier queries. The LINQ and Object Services queries that returned entities defined in the model are not shaped. They are purely a collection of individual entities. For instance, Example 4-13 returned an IQueryable of Contact objects. Example 4-9, however, returned an anonymous type with two properties. The first property was a Contact entity and the second was a collection of Address entities related to that Contact. The code in Example 4-10 enumerated over that data, albeit in a somewhat boring way, to demonstrate what the data looked like. It showed the contacts and the addresses but did not truly demonstrate the relationship between the two. Example 4-29 executes the same query and then enumerates through the anonymous types that result. This time, however, the code accesses the Addresses as a navigation property of the Contact. LazyLoadingEnabled is set to false to ensure that the Count method does not impact the
results.
Shaping Data Returned by Queries | 97
Download from Library of Wow! eBook
Example 4-29. LINQ query creating shaped results context.ContextOptions.LazyLoadingEnabled=false; var addressGraphs = from a in context.Addresses where a.CountryRegion == "Canada" select new { a, a.Contact }; foreach (var ag in addressGraphs) { Console.WriteLine("LastName: {0} # Addresses: {1} ", ag.Contact.LastName.Trim(), ag.Contact.Addresses.Count()); foreach (Address address in ag.Contact.Addresses) { Console.WriteLine(".....{0}", address.City); } }
Console.WriteLine();
There’s a simpler way to express this particular query with the Include method, which you will see next. But what differentiates this from Include is that with it you can take the projection in Example 4-29 a step further in a direction that you won’t be able to do with Include. I’ll discuss this after we look at the results of this example.
Let’s turn the query around a bit to see how this can work. Imagine you are querying contacts and want to also return their addresses. The WriteLine method doesn’t access the a property of the anonymous type, but instead navigates to the addresses through the Contact property of the anonymous type. As the Contact and Address entities are materialized, the Entity Framework recognizes that they are related to each other and wires them up so that you can navigate between them. The Address objects have a Contact object in their Contact property and the Contact objects have Address objects in their Addresses property. This is a very highlevel explanation of an important function of the Entity Framework’s Object Services API, which you will learn plenty about throughout the book. There is an interesting thing to be aware of with respect to how the Entity Framework connects the related entities in the scenario laid out in Example 4-29. If you look at the following sample of the output, you can see that two addresses belong to the contact “Harding.” One is in Toronto and the other is in Vancouver. But the first instance says that Harding has only one address. Not until the code has reached the second address is the contact aware that two addresses exist in its Addresses navigation collection. LastName: Garza # Addresses: 1 ....Burnaby LastName: Harding # Addresses: 1 ....Toronto
98 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
LastName: Harding # Addresses: 2 ....Toronto ....Vancouver LastName: Caprio # Addresses: 1 ....Toronto LastName: Blackwell # Addresses: 1 ....Toronto LastName: Hamilton # Addresses: 1 ....Chalk Riber
The second address isn’t recognized initially because it hasn’t been materialized as an object yet. As the code enumerates through the query results for the first time, the objects are created from the query results as each contact or address is reached. Once the second address is encountered and turned into an object, its relationship to the contact is identified. I had you disable lazy loading in order to see this because when you requested Addresses.Count, lazy loading would have kicked in and gone to the database to retrieve the contact’s complete Addresses collection. For the sake of the demo, I did not want this behavior. You’ll learn more about lazy loading further on in this chapter, and later in the book as well.
We will explore the object life cycle more deeply in a later chapter, but this should give you some idea for now about what’s going on in this example.
Limiting Which Related Data Is Returned At the end of the previous example, I mentioned that projections will allow something that the upcoming Include method won’t allow. That is the ability to filter which related data is returned. If you were querying for contacts with their addresses, a projection would look like this: var contactGraphs = from c in context.Contacts select new { c, c.Addresses };
You can modify the query to load all of the contacts, but only a subset of their addresses, as in Example 4-30. Example 4-30. Filtering related data in a query using projections var contactGraphs = from c in context.Contacts select new { c, c.Addresses.Where(a=>a.CountryRegion="UK")};
I’ll refer back to this example as we look at other means of loading related data.
Shaping Data Returned by Queries | 99
Download from Library of Wow! eBook
Loading Related Data So far, all of the queries that involved returning related data have explicitly asked for that data in the query itself. The Entity Framework will only return data that you explicitly ask for. If your query asks only for contacts, the Entity Framework will not make an assumption that just because contacts have addresses, it should return the addresses anytime you query for contacts. Consider a typical model for sales information. A contact is related to a customer; a customer has sales orders; each sales order has line items; each line item relates to a product; each product comes from a vendor and is also related to a category. Can you imagine if you queried for contacts, and without expecting it, the entire contents of the database were returned—because it was all related? It is possible to get related data after the fact. For example, if you queried for a selection of contacts, as you work with those contacts in code you can request the contacts’ addresses without performing another complete query. For developers coming from the first version of Entity Framework, there is a big change to be aware of here. The implicit, automatic loading of related data, controlled by the ObjectContext.ContextOptions.LazyLoa dingEnabled property, is a new option in the Entity Framework. It is enabled (i.e., set to true) by default, for newly created models. The property will be false on existing models pulled into .NET 4 to prevent breaking changes in your existing code.
This is referred to as deferred loading or implicit deferred loading, and is most commonly known as lazy loading. As of .NET 4, Entity Framework performs lazy loading by default. The LINQ to Entities query in Example 4-31 returns an ObjectSet of Contact entities. As the code enumerates through the results, it also asks for information about the related Addresses. But the Addresses were not returned with the original query. Example 4-31. Implicitly loading related data after the fact var contacts= from c in context.Contacts select c; foreach (var contact in contacts) { Console.WriteLine("{0} #Addresses: {1}", contact.LastName,contact.Addresses.Count()); }
Unlike the filtered projection in Example 4-30, lazy loading has no means of filtering the data being loaded. However, each time the code hits a request for the address count of the current contact, a new query will be executed on the server to retrieve the addresses for the current contact. You should understand that this means that if there were 10 contacts in the 100 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
original result, there will be 10 additional trips to the database as you iterate through the 10 contacts.
Controlling Lazy Loading Lazy loading is surely convenient, but if you are not paying attention, you could be abusing your server resources by unknowingly or even unnecessarily causing repeated trips to the database. You can disable (and reenable) lazy loading as needed in code or modify the default behavior for the context. There are other ways to load related data when you need it even if you are not depending on lazy loading.
Disabling and enabling lazy loading programmatically Lazy loading can be controlled through the ObjectContext’s ContextOptions.LazyLoa dingEnabled property: var context = new SampleEntities(); context.ContextOptions.LazyLoadingEnabled = false;
Once it is disabled, you can still explicitly load related data on demand if needed, or even load the data along with the initial query. These two methods are covered in the next few pages.
Changing the default behavior for lazy loading In the default generated classes, the constructors for the ObjectContext (e.g., SampleEntities) set LazyLoadingEnabled based on an annotation in the EDMX. The XML annotation was pointed out in the CSDL EntityContainer section of Chapter 2. Models that are created in Visual Studio 2010 have this annotation with the value set to true. Models that were created in Visual Studio 2008 SP1 do not have the annotation, and therefore, if you are using an older model, by default, lazy loading will not be enabled. The Lazy Loading Enabled setting is exposed in the model’s Properties window in the Designer, in the Code Generation section, where you can change the default behavior for a particular model.
Explicitly Loading Entity Collections and Entity References Let’s return to the query in Example 4-31: var contacts= from c in context.Contacts select c;
When lazy loading is disabled, because the query does not explicitly request the addresses, the Addresses.Count for every single contact will be zero. But you can explicitly tell the Entity Framework to get the addresses for the current contact, as shown in the Example 4-32.
Loading Related Data | 101
Download from Library of Wow! eBook
Example 4-32. Explicitly loading related data with the Load method foreach (var contact in contacts) { contact.Addresses.Load(); Console.WriteLine(contact.Addresses.Count); }
When Load is called, Object Services will execute a query to retrieve all of the addresses for that contact. In the preceding example, after Load is called, the value of Count will be correct and all of the Address entities for that contact will be available. Using Load is another case where you cannot filter the related data being loaded as you can with the projection in Example 4-30. In .NET 4, a new method was introduced to load from the context, not from the navigation property. The method is ObjectContext.LoadProperty and it was created as part of the support for classes that do not inherit from EntityObject. You’ll learn about LoadProperty in Chapter 11.
Loading the EntityReference You can also perform deferred loading for EntityReference navigation properties—for example, Address.Contact. However, rather than load from the Contact property, you must load from the additional property that was created by the code generation: Con tactReference. The Entity Framework sees Address.Contact as merely a Contact entity, and the Contact class does not have the Load method. It is the ContactReference property that has the knowledge of how to load the related information. Each EntityReference navigation property will have a related property with the word Reference appended to its name. Example 4-33 shows how to load Contact data for particular addresses after the addresses have already been queried. Example 4-33. Loading the Contact using ContactReference.Load var addresses = from a in context.Addresses select a ; foreach (var address in addresses) { if (address.CountryRegion != null) { if (address.CountryRegion.Trim() == "UK") { address.ContactReference.Load(); } } }
102 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
Performance considerations with deferred loading There is a big performance consideration here. Whether you are lazy-loading or explicitly loading the related data for each contact, the code is forcing an extra round trip to the database, something many developers won’t realize unless they are profiling the database activity. This can be extremely inefficient and might also get you into big trouble with the IT pros in your company. With lazy loading disabled, you can have some control over when the extra trip is made. Load is a great choice in cases where you want to inspect the contacts and then load
addresses for only particular contacts. Perhaps you want to list all contacts, but for contacts that were added after a particular date you need to see how many addresses are in the database. The code in Example 4-34 demonstrates this scenario, where you may determine it is more efficient to make a small number of database trips rather than preloading addresses for every contact. Example 4-34. Loading addresses for some of the contacts foreach (Contact contact in contacts) { Console.WriteLine(contact.LastName); if (contact.AddDate > System.Convert.ToDateTime("1/1/2008")) { contact.Addresses.Load(); } }
With lazy loading enabled, this kind of granular control is a bit more difficult to achieve. The benefit of having lazy loading enabled is that you won’t have to worry about reporting that there are no addresses for a contact when in fact there are a number of them in the database because you forgot to, or didn’t know that you needed to, explicitly load those related addresses.
Using the Include Method to Eager-Load In cases where you know you will need all of the addresses up front, it may be more efficient to retrieve them as part of the original query. Although you have seen how to do this with projection by including the addresses in the SELECT clause, the Include method is another way to achieve this and may be preferable for a variety of reasons. The most notable reason is that the resultant objects will be your entities, rather than anonymous types with entities as their properties. However, Include does not allow you to filter the related data as you can with a projection. Include is a query builder method and you can apply it to an ObjectQuery or Object Set (which, as you may recall, derives from ObjectQuery). Because context.Contacts is an ObjectSet, you can use Include even within a LINQ query, as shown in Exam-
ple 4-35.
Loading Related Data | 103
Download from Library of Wow! eBook
Example 4-35. The Include method in a LINQ to Entities query from c in context.Contacts.Include("Addresses") where c.LastName.StartsWith("J") select c
The argument for Include is a string that is the name (or names) of the navigation properties to bring back along with the contacts. This is referred to as eager loading or eager fetching. You can use Include only when returning an ObjectQuery or ObjectSet of a single entity type. You cannot use it with projections, and if you do project, Include will be ignored. In the sample model, there is only one navigation property for contact, which is Addresses. Imagine a sales model with a number of entities and a variety of navigations. You could query customers and eager-load the orders and all of the orders’ details by querying Customers.Include("Orders.OrderDetails"). The string is called a query path because it defines the path that the query should navigate through the model. This will bring in both the Orders and OrderDetails. Additionally, you could eager-load the orders and the customers’ addresses by chaining the Include methods like this: Customers.Include("Orders.OrderDetails").Include("Addresses")
How is the data shaped with Include? Data shaping is one of the interesting benefits of Include. The previous Contacts.Include("Addresses") query returns a set of Contact entities. This does not have the same effect as projection, which would have to return DbDataRecords. Figure 4-8 shows the query results in the debugger’s QuickWatch window. You can see that the results are strictly a set of Contact entities. Where are the addresses? Figure 4-9 drills into one of the contacts, and you can see that both of this contact’s addresses are there. The Include brings in the related data, and unlike the issue you saw in the results of Example 4-29 (not all addresses were being attached to Ms. Harding from Toronto until the addresses had been enumerated), all of these addresses are present as soon as you get to the contact. Lazy loading will still be active when you are inspecting data in debug windows such as the QuickWatch window in Figure 4-9. I disabled lazy loading for the context prior to opening the QuickWatch window. You can also watch a database profiler to ensure that the Addresses count you are looking at is truly a result of eager loading and is not being provided by way of lazy loading and an extra hit to the database.
104 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
Figure 4-8. The result of the Include with no projections, which returns only the primary entity of the query
Figure 4-9. The result of the Include with projections, with lazy loading disabled, which returns the contact’s related Addresses in the query
Loading Related Data | 105
Download from Library of Wow! eBook
Accessing properties from an Include in the query You can use the properties of the Include entities in many of the same ways you can use properties of any related data when querying. Example 4-36 uses the CountryRegion field of Address to limit which contacts are retrieved. But be sure you are clear on the results. This will return contacts that happen to have any of their addresses in the United Kingdom. If a contact has multiple addresses and only one of them is in the United Kingdom, you will still get all of those addresses. Example 4-36. Limiting which contacts are retrieved from c in context.Contacts.Include("Addresses") where c.Addresses.Any((a) => a.CountryRegion == "UK") select c
Although you can use the properties of the included data in your query, you cannot filter or sort the included data. There’s no way to say “when you return the addresses along with the contacts, please sort the addresses by city.” Additionally, as mentioned before, you can’t filter the included data either.
Pros and Cons of Load and Include You have some things to consider when choosing between the Load and Include methods. Although the Load method may require additional round trips to the server, the Include method may result in a large amount of data being streamed back to the client application and then processed as the data is materialized into objects. This would be especially problematic if you are doing all of this work to retrieve related data that may never be used. As is true with many choices in programming, this is a balancing act that you need to work out based on your particular scenario. The documentation also warns that using query paths with Include could result in very complex queries at the data store because of the possible need to use numerous joins. As the model becomes more complex, the potential for trouble increases. You could certainly balance the pros and cons by combining the two methods. For example, you can load the customers and orders with Include and then pull in the order details on an as-needed basis with Load. The correct choice, or combination, will most likely change on a case-by-case basis.
106 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
Retrieving a Single Entity All of the queries so far have returned sets of data. What if you wanted to retrieve a single entity or a single result? The queries return IQueryables or ObjectQuerys and you need to dig into those to get at the actual data, which might be entities, anonymous types, or DbDataRecords. This is reasonable if you are returning multiple items, but what about cases where you query for one particular item—for example, the contact whose ContactID is 63—and you don’t want to have an IQueryable returned, but just the item? LINQ to Entities has a pair of methods, First and FirstOrDefault, which will return the first item in the result set. Additionally, Single and SingleOrDefault are useful when you are expecting only one item in the result set—for example, if you are querying for a single contact. These methods are not specific to LINQ to Entities, but come from LINQ and may be familiar to you already. Example 4-37 shows two techniques for using these methods. In the first technique, a query is defined and then the Single method is called. This will cause the query to be executed and the contact entity to be returned. The second technique appends the Single method directly to the query. Even though Single is a LINQ method, you can combine it with the query operator syntax by wrapping the query in parentheses. In this case, the query is executed immediately and the contact is returned. Example 4-37. Querying with the Single method IQueryable contacts = from c in context.Contacts where c.ContactID == 1 select c; Contact contact = contacts.Single(); Console.WriteLine(contact.LastName); Contact singleContact = (from c in context.Contacts where c.ContactID == 2 select c).Single(); Console.WriteLine(singleContact.LastName);
There’s a potential problem here. If there are no items, First and Single will throw an InvalidOperationException with the message “Sequence contains no elements.” FirstOrDefault and SingleOrDefault protect you from the exception by returning the default, which is generally a null (Nothing in VB). Additionally, if you use Single or SingleOrDefault but the result set contains more than one item, an exception will be thrown. In that case, you should be using First or FirstOrDefault.
Retrieving a Single Entity | 107
Download from Library of Wow! eBook
In Example 4-38, SingleOrDefault is used to avoid an exception being thrown. Contact in this case will be Nothing/null after the query is executed. Example 4-38. Using SingleOrDefault to avoid an exception var contact = (from c in context.Contacts where c.ContactID == 7654321 select c).SingleOrDefault();
Another way to use these methods is to pass the predicate directly to them, rather than using a where operator. For example: var contact = context.Contacts.Single(c => c.ContactID == 1);
Retrieving a Single Entity with GetObjectByKey The ObjectContext.GetObjectByKey method and its counterpart, TryGetObjectByKey, provide a way to query for an object without having to construct and execute a query. However, this has a notable twist. The runtime will first look in the existing instantiated objects to see whether the object has already been retrieved. If it is found, this is what will be returned. If not, the query to the data store will be executed automatically and the object will be returned. GetObjectByKey takes an EntityKey type that defines what object to retrieve based on its EntitySet, its key property name, and the value of that property. For example, EntityKey("SampleEntities.Contacts","ContactID",5) defines an object in the Contacts EntitySet with a ContactID value of 5. Once the EntityKey has been created, GetObjectByKey(myEntityKey) will return the object either from memory or from the
database. TryGetObjectByKey uses the .NET Try pattern to avoid returning an exception if the
object is not found in memory or in the database. You will see both of these used many times in later chapters, and you will learn all about the EntityKey class in Chapter 10. There is also a method for retrieving an entity by only looking in memory and not checking the database, called GetObjectStateEntry. You'll learn about this method in Chapter 10.
108 | Chapter 4: Exploring LINQ to Entities in Greater Depth
Download from Library of Wow! eBook
Finding More Query Samples This chapter is filled with many queries, but there are so many possibilities for querying with LINQ or Entity SQL that you will certainly benefit from checking these other great resources: MSDN’s 101 C# LINQ Samples http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx MSDN’s 101 Visual Basic LINQ Samples http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx MSDN’s Entity Framework Query Samples http://code.msdn.microsoft.com/EFQuerySamples There are also a number of excellent books that are focused on LINQ or that contain LINQ content. Some that I recommend are LINQ Pocket Reference by Joseph Albahari and Ben Albahari (O’Reilly), LINQ in Action by Fabrice Marguerie et al. (Manning Press), and Essential LINQ by Charlie Calvert and Dinesh Kulkarni (Addison-Wesley).
Summary In this chapter, you have learned a variety of ways to use LINQ to Entities to express more complicated queries. You have used projections, queried across navigations, and learned how to group. You have also learned about various ways to load related data, whether through returning shaped results with the Include method, retrieving related data after the fact with lazy loading or explicitly calling a Load method. With LINQ to Entities, Entity SQL, Object Services, and EntityClient, the Entity Framework provides myriad possibilities for querying data and shaping results. In the next chapter you will see how many of the queries written in this chapter can be written with Entity SQL. Although it would take a few hundred more pages to ensure that you have seen an example of almost any type of query you may want to write, these past two chapters should leave you very prepared to venture forth. In Chapter 6, you will learn about updating the data you have queried and taking advantage of stored procedures. Then, beginning with Chapter 9, you will start to write some small applications and be able to leverage many of these types of queries.
Summary | 109
Download from Library of Wow! eBook
Download from Library of Wow! eBook
CHAPTER 5
Exploring Entity SQL in Greater Depth
Chapter 4 introduced you to a number of new querying concepts and how to express those queries with LINQ to Entities. LINQ to Entities will most likely be the more common form of querying in your applications. But there are still many scenarios where you may find that Entity SQL gives you an advantage, such as with complex dynamic query building. Outside of the MSDN documentation, you will find that the resources for learning Entity SQL are few and far between. Therefore, in this chapter, we will run through the same types of queries explored in Chapter 4 and I will demonstrate how to express them using Entity SQL. But we’ll begin with a look at some nuances for expressing Entity SQL that don’t exist with LINQ to Entities.
Literals in Entity SQL When writing queries in LINQ to Entities, you don’t have to be too concerned about the data types that you are using for projections or filtering, but in Entity SQL there are rules about including many of the types you may want in your query. As an example, you must use special syntax with date types. Many SQL syntaxes require special handling for date types. T-SQL is very forgiving, as it simply requires that you express the date as a string. Entity SQL, however, has specialized formatting for a number of literals. There is an MSDN topic called “Literals (Entity SQL)” (http://msdn.microsoft.com/en-us/library/ bb399176.aspx) that drills into these, but unfortunately it is very easy to miss the critical information in the document. I’ve done it myself, and so have many people who have emailed me with questions about Entity SQL. Therefore, I will highlight a few of these literals here. I won’t cover every literal type, but once you have the hang of it, you can refer back to the MSDN topic for the other types. Without the specific syntax, in some cases you will get an error message, in others it won’t pose a problem, but in others still you will simply get inaccurate results.
111
Download from Library of Wow! eBook
Because you’ll be writing a lot of Entity SQL expressions in this chapter, I am using a shorter container name, PEF (an abbreviation of Programming Entity Framework), rather than SampleEntities. See the sidebar “Simplifying the Container Name for Our Examples” on page 113 for steps to do this yourself.
Expressing a DateTime Literal To express a DateTime in Entity SQL, the value must be formatted minimally as DATETIME'YYYY-MM-DD HH:MM', as shown here: SELECT c FROM PEF.Contacts as c WHERE c.ModifiedDate>DATETIME'2009-01-01 00:00'
Even if you are using a SQL Server 2008 Date type, you need the DATETIME keyword. You must also include the hours and minutes, but you can go further with seconds and beyond if you like. Incorrect syntax with dates will generally cause an exception to be thrown.
Expressing a Decimal Literal Decimals are trickier. The following expression queries a model based on Microsoft’s AdventureWorksLT sample database: SELECT p FROM AdventureWorksEntities.Products as p WHERE p.ListPrice=133
The ListPrice column in the database table is defined as a Decimal data type. The expression uses an Integer (133) as a filtering value against this column, and the query will return the expected results. However, if you wanted to express a Decimal value and simply used WHERE p.listprice=133.34, you would get an EntitySQLException stating “The argument types ‘Edm.Decimal’ and ‘Edm.Double’ are incompatible for this operation.” The documentation tells you to follow the value with an uppercase M. Here is the correct syntax for this query: select p from AdventureWorksEntities.Products as p WHERE p.listprice=133.34M
Using Additional Literal Types There are a number of different value modifiers depending on the type. Single types must be followed by a lowercase f; an Int64 (bigint) is followed by an uppercase L. Examples of other types that use literal keywords in Entity SQL are Time, GUID, BINARY, and DATETIMEOFFSET. Pay attention to these syntax requirements when constructing Entity SQL. 112 | Chapter 5: Exploring Entity SQL in Greater Depth
Download from Library of Wow! eBook
Simplifying the Container Name for Our Examples Since you will be writing a lot more Entity SQL expressions in this chapter, you may want to modify the EntityContainer name so that you have something even simpler to type. The Designer makes this easy to do. Open the model in the Designer and click the background of the model. This will cause the model’s properties to show up in the Properties window. Change the Entity Container Name to PEF, the acronym of this book’s title. This change will have an impact in three places in your application: • PEF will be the new EntityContainer name for the model. • PEF will be the new name of the EntityContainer class that you will instantiate to perform queries. • PEF will be the new name of the connection string in the app.config file. You should double-check that the change was made in all three locations. You will also need to change any existing code references that use this model from the longer name to the new short name. If you are using Find & Replace, I recommend using the Current Document option. This new name is not a recommended naming convention, but a convenience for the sake of writing numerous Entity SQL queries in this chapter.
Projecting in Entity SQL You can use projections with Entity SQL queries in both Object Services and EntityClient queries. Only LINQ queries can return anonymous types as you saw in Chapter 4. This is not a concern with EntityClient queries as EntityClient does not attempt to materialize objects from the results. When projecting with Entity SQL and Object Services, the query will return data records. These are the same System.Data.Common.DbDataRecords returned by Entity Client queries, which you saw in Chapter 3. First look at the code in Example 5-1 and then at the query results. I’ve added Sys tem.Data.Common to the using statements at the beginning of the class file. Example 5-1. Projection with Entity SQL String query = "SELECT c.FirstName,c.LastName, c.Title " + "FROM PEF.Contacts AS c " + "WHERE c.FirstName='Robert'"; ObjectQuery contacts = context.CreateQuery(query);
Projecting in Entity SQL | 113
Download from Library of Wow! eBook
Notice that in the Entity SQL string, the keyword VALUE is gone. That’s because the projection is selecting multiple values. Also, note that the type being passed into the CreateQuery method is now a DbDataRecord. In the introduction to EntityClient in Chapter 3, you learned that a DbDataRecord represents a single item in a DbDataReader. Therefore, you will need to interact with these results in the same way you did when using the EntityClient example. There is one very nice difference, however. The results are not being streamed; they have been materialized into the DbDataRecord. Therefore, you can access the column data in any order you want. To highlight this, the query string selected FirstName, LastName, and then Title. When you build the code to display the results, shown in Example 5-2, you’ll see that it’s OK to use Title first. Example 5-2. Enumerating through the DbDataRecord returned by an Entity SQL projection foreach (DbDataRecord record in contacts) { Console.WriteLine("{0} {1} {2}", record["Title"].ToString().Trim(), record["FirstName"].ToString().Trim(), record["LastName"].ToString().Trim()); }
In Example 5-2, I used an alternative way of pulling data from a DbDataRecord. Item takes a string parameter (the column name) or an integer (the column position), whereas the GetString, GetInt, and other related methods take only an integer as a parameter. I’ve used the string here for clarity; however, be aware that there is a slight performance penalty for using the string instead of the integer.
DbDataRecords and Nonscalar Properties Most of these examples project strings, though you saw one example with LINQ for Entities where an anonymous type and an EntityCollection of Address types were projected. How would you interact with a DbDataRecord that contains an entity or another object in its columns? The Entity SQL expression in Example 5-3 selects the entire Contact entity as the first property of the results and the contact’s addresses as the second property. Example 5-3. Projecting objects with Entity SQL String query = "SELECT c, c.Addresses " + "FROM PEF.Contacts AS c " + "WHERE c.FirstName='Robert'"; ObjectQuery contacts = context.CreateQuery(query); foreach (DbDataRecord c in contacts) { var contact = c[0] as Contact;
114 | Chapter 5: Exploring Entity SQL in Greater Depth
Download from Library of Wow! eBook
}
Console.WriteLine("{0} {1} {2}", contact.Title.Trim(), contact.FirstName.Trim(), contact.LastName); foreach(var a in contact.Addresses) { Console.WriteLine(" {0}, {1}", a.Street1.Trim(), a.City); }
Remember, DbDataRecord is in the System.Data.Common namespace. You’ll need that in a using/Imports statement in your code file.
In Example 5-2, you had to explicitly cast the items of the results to String types. In this case, because you know the first item will contain a Contact type, you can cast the column to Contact and then work directly with that strongly typed object. You can do the same with the collection of Address types in the second column.
Projecting with Query Builder Methods Example 5-4 shows an example of using a query builder method to do projection. In the projection, you use the it alias to access the properties. Example 5-4. Using query builder methods to project data ObjectQuery contacts = context.Contacts .Where("it.FirstName='Robert'") .Select("it.Title, it.FirstName, it.LastName");
Projection with query builder methods also returns DbDataRecords. You’ll need to access the results through the data record’s items, as with Example 4-6 in Chapter 4.
Using Navigation in Entity SQL Queries In Chapter 4, you saw LINQ to Entities queries that leveraged navigation properties whether for projecting, filtering, or performing other query tasks. Here we will look at how to use navigations in Entity SQL.
Navigating to an EntityReference Recall that navigating to the “one” end of a one-to-one or many-to-one relationship is referred to as a navigation reference. The entity you are pointing to is an EntityReference.
Using Navigation in Entity SQL Queries | 115
Download from Library of Wow! eBook
Chapter 19 will drill further into EntityReferences and EntityCollec tions, and how they are surfaced as navigation properties.
Example 5-5 demonstrates how to query for a type (Address) along with an EntityReference navigation property (Address.Contact) using Entity SQL. Example 5-5. Projecting into an EntityRef with Entity SQL SELECT a,a.Contact FROM PEF.Addresses AS a WHERE a.CountryRegion='UK'
This will return DbDataRecord objects. When working with these results you can cast the data in the first position to an Address and the data in the second position to a Contact, as you did in Example 4-7 in Chapter 4.
Filtering and Sorting with an EntityReference You can filter and sort based on a property of an EntityReference even if you are not selecting the related data. The Entity SQL query in Example 5-6 sorts by Contact.LastName and filters on the Contact.AddDate field even though AddDate is not part of the results. Example 5-6. Filtering and sorting on reference properties SELECT a,a.Contact.LastName FROM PEF.Addresses AS a WHERE a.Contact.AddDate>DATETIME'2009-01-1 00:00' ORDER BY a.Contact.LastName
Filtering and Sorting with EntityCollections In Chapter 4, we used the LINQ Any method to filter based on an object’s EntityCollection navigation property. The relevant Entity SQL EXISTS operator is not as facile as the ANY method. You’ll need to pass a subquery into EXISTS so that it knows what to search. Look closely at the subquery in Example 5-7. It is querying c.Addresses, which is the collection of addresses that belongs to the value being returned in the main query. The subquery is able to take advantage of the navigation from a contact to its addresses. Example 5-7. Filtering across a navigation with Entity SQL Select VALUE c FROM PEF.Contacts as c WHERE EXISTS(SELECT a from c.Addresses as a WHERE a.CountryRegion='UK')
116 | Chapter 5: Exploring Entity SQL in Greater Depth
Download from Library of Wow! eBook
Aggregating with EntityCollections Working with aggregates in Entity SQL is not as simple as it is in LINQ to Entities. For example, LINQ is able to count the elements in a collection and doesn’t care whether the collection contains values or objects. But Entity SQL can perform aggregates on only a set of values, and even then on only certain types of values. This behavior mirrors how SQL Server uses aggregates. Therefore, with Entity SQL you can’t write Count(c.Addresses), but rather you need to pass a value, such as AddressID, in to the Count function. To do this, you can use a subquery against c.Addresses that returns a collection of AddressIDs. You can then COUNT the results of that query, as shown in Example 5-8. Example 5-8. Using the Count aggregate function in Entity SQL Select c, COUNT(Select VALUE a.AddressID FROM c.Addresses as a) FROM PEF.Contacts as c
The other aggregates work in the same way. Example 5-9 shows the MAX query written with Entity SQL. Example 5-9. Using the MAX aggregate function in Entity SQL SELECT c.LastName, MAX(SELECT VALUE a.PostalCode FROM c.Addresses AS a) FROM PEF.Contacts AS c
You can even use an aggregate in a subquery, as in Example 5-10. Example 5-10. An aggregate in a subquery SELECT c.LastName, (SELECT VALUE MAX(a.PostalCode) FROM c.Addresses as a) FROM PEF.Contacts AS c
In this example, the second column of the query results does not contain the string value of the PostalCode. It contains the results of a query, and therefore it is a collection of string values. If you want to read the PostalCodes, you can iterate through the collection or use a SET operator.
Using Entity SQL SET Operators Like aggregates, SET operators work with a set of values. The ANYELEMENT operator is a SET operator that will randomly pick an element from a collection. As shown in Example 5-11, you can even use this with collections that contain only one element, such as the MAX PostalCode column.
Using Navigation in Entity SQL Queries | 117
Download from Library of Wow! eBook
Example 5-11. Using the ANYELEMENT operator against a set of data SELECT c.LastName, ANYELEMENT(SELECT VALUE max(a.PostalCode) FROM c.Addresses AS a) AS MaxPostal FROM PEF.Contacts AS c
The results of this query will now contain a string in the second position, not a collection. The SET operators in Entity SQL are ANYELEMENT, EXCEPT, FLATTEN, INTERSECT, EXISTS and NOT EXISTS, IN and NOT IN, OVERLAPS, SET, and UNION. There is also an ELEMENT operator that has not yet been implemented but is reserved. If you attempt to use it in the first version of the Entity Framework, you will get an exception that explains that ELEMENT cannot be used yet. Take some time to explore these operators in the documentation and in code to get a feel for where and when you might want to use them.
Aggregating with Query Builder Methods The Entity SQL query builder methods do not provide aggregate methods. However, you can use an Entity SQL query as the argument of the SELECT query builder method to perform the aggregate. Remember that the collection being queried in the subquery is based on the main query’s control variable, referred to with the it alias by default. Example 5-12 uses the MAX aggregate as one of two projected values to be returned. Example 5-12. An Entity SQL query builder method using an aggregate context.Contacts .Select("it.LastName, " + "( MAX(SELECT VALUE a.PostalCode FROM it.Addresses AS a))");
Using Joins Example 5-13 shows the syntax of an Entity SQL JOIN. Example 5-13. JOIN syntax for Entity SQL SELECT variableA, variableB FROM collection as variableA JOIN Collection as variableB ON Property = Property
118 | Chapter 5: Exploring Entity SQL in Greater Depth
Download from Library of Wow! eBook
Entity SQL has the ability to do cross joins. You can express them explicitly; however, a JOIN without an ON clause will implicitly become a cross join, pairing every entity in the first collection with every entity in the second collection. So, watch out!
Example 5-14 demonstrates a JOIN query expressed in Entity SQL. Example 5-14. An Entity SQL query using JOIN SELECT c.Title,oa.FirstName, oa.LastName, oa.Street1, oa.City, oa.StateProvince FROM PEF.Contacts as c JOIN PEF.vOfficeAddresses as oa ON c.ContactID = oa.ContactID
Nesting Queries Both LINQ and Entity SQL provide the ability to nest queries, and you have already seen some examples of this. When you write a query, anywhere a value is expected you can use another query in its place, as long as that query returns an acceptable type. You can use a nested query in place of an expression or a collection, as you will see in the following examples. The goal of the previous JOIN queries was to return properties from a Contact entity combined with properties from the vOfficeAddresses entities where the ContactID matches. With Entity SQL, the nested query works in the same manner as with LINQ to Entities, using the query in place of an actual value, though there’s no need to name the property it represents (see Example 5-15). Here you will also see the Entity SQL TRIM function in effect. Example 5-15. Nested query in place of a SELECT expression in Entity SQL SELECT TRIM(oa.FirstName), oa.LastName, ANYELEMENT(SELECT VALUE c.Title FROM PEF.Contacts as c WHERE c.ContactID=oa.ContactID), oa.Street1, oa.City, oa.StateProvince FROM PEF.vOfficeAddresses as oa
The query in Example 5-16 demonstrates replacing the queried collection with a nested query.
Nesting Queries | 119
Download from Library of Wow! eBook
Example 5-16. Nested query in place of a FROM expression in Entity SQL SELECT TRIM(oa.FirstName), oa.LastName FROM (SELECT VALUE oa FROM PEF.vOfficeAddresses AS oa WHERE oa.StateProvince='Ontario') AS oa
You can easily break this up for readability, because you are merely building strings, and you can concatenate the queries, as shown in Example 5-17. Example 5-17. Breaking up a nested query in Entity SQL string subQuery = "SELECT VALUE oa " + "FROM PEF.vOfficeAddresses AS oa " + "WHERE oa.StateProvince='Ontario'"; String queryString = _ "SELECT add.FirstName, add.LastName FROM (" + subQuery + ") as add";
Remember that an Order operator in a subquery will be ignored. The main query controls ordering.
Grouping in Entity SQL LINQ will spoil you with its grouping capabilities. Like SQL, Entity SQL comes with a lot of rules so that you can convert queries into a command tree and then into the provider’s query syntax. For example, in SQL the most commonly encountered rule is that every expression in the SELECT must either be accounted for in the GROUP BY clause or be wrapped in an aggregate. The same is true in Entity SQL, which prevents you from being able to select entire objects in the SELECT clause. However, it is still possible to return entire objects and shape data in Entity SQL by putting the GROUP BY operator into a nested query. First take a look at Example 5-18, which shows some simple grouping in Entity SQL. Example 5-18. A simple GROUP BY example in Entity SQL SELECT c.Title, COUNT(c.Title) FROM PEF.Contacts as c GROUP BY c.Title
The two projected expressions in the SELECT are covered by either the GROUP BY or an aggregate (COUNT). The query returns the following: [blank] 6 Mr. 255 Ms. 177 Sr. 3 Sra. 2
120 | Chapter 5: Exploring Entity SQL in Greater Depth
Download from Library of Wow! eBook
To group on an expression that is evaluated, such as "Title" + c.Title, the grouping must be explicitly named and that name needs to be used as a projected expression. Example 5-19 shows the Entity SQL syntax for creating an expression and grouping on it in the same query. The expression, EvalTitle, is built in the GROUP BY clause and is used by name in the SELECT. Example 5-19. Grouping by a calculated expression SELECT evalTitle,count(c.Title) FROM PEF.Contacts as c GROUP BY "Title: " +c.Title as EvalTitle
Returning Entities from an Entity SQL GROUP BY Query Now, let’s take a look at how you can return full objects from Entity SQL when using GROUP BY. The trick is in using nested queries. To reproduce the LINQ query that grouped by Title and returned each title with its collection of contacts, you can use a nested query as an expression in the SELECT statement, as shown in Example 5-20. It seems as though the query does not have to follow the rule of being part of the GROUP BY clause or the target of an aggregate. Example 5-20. An Entity SQL GROUP BY query that returns entities SELECT groupCon.Title, (SELECT c FROM PEF.Contacts as c WHERE c.Title= groupCon.Title) FROM PEF.Contacts as groupCon GROUP BY groupCon.title
The nested query returns a collection of contacts whose Title property equals the current title being returned by the group. Although this looks like it might do some scary things on the server with respect to the generated SQL, the SQL is similar to the SQL created as a result of the first LINQ query in this section on grouping.
Filtering Based on Group Properties You saw that LINQ uses the WHERE clause to filter within a group. In Entity SQL, you can use the HAVING clause for this purpose, as shown in Example 5-21. Example 5-21. Entity SQL’s HAVING clause, which helps with filtering SELECT groupCon.Title,count(groupCon.ContactID) FROM PEF.Contacts as groupCon GROUP BY groupCon.title HAVING count(groupCon.ContactID)>150
Grouping in Entity SQL | 121
Download from Library of Wow! eBook
This returns only the title groups that contain more than 150 contacts. The results will be as follows: Mr. Ms.
255 177
Shaping Data with Entity SQL As you’ve seen already, projections in Object Services result in DbDataRecords, as opposed to the anonymous types that LINQ returns. However, even in these DbDataRecords, you can still find complete entities and navigate through their associations. The query shown in Example 5-22 results in an ObjectQuery of DbDataRecords that are structured as rows and columns. Each row in this result has two columns (also called fields). An Address entity is contained in the first field and a Contact entity is contained in the second field. Example 5-22. Entity SQL resulting in addresses with their contacts SELECT a,a.Contact FROM PEF.Addresses AS a WHERE a.CountryRegion='Canada'
Figure 5-1 shows the first column of one of the DbDataRecords in the results. The item is an Address entity. The second column contains a Contact entity. So, even though it is a DbDataRecord, it still can con