Chapter 1. Data access reloaded: Entity Framework
Listing 1.1. Displaying a list of orders
Listing 1.2. Displaying data for a single order
Chapter 2. Getting started with Entity Framework
Listing 2.1. Displaying orders in a grid
Listing 2.2. Displaying orders placed in the current month
Listing 2.3. Retrieving a single order
Listing 2.4. Inserting a new order
Listing 2.5. Updating an existing order
Listing 2.6. Deleting an existing order in a disconnected way
Chapter 3. Querying the object model: the basics
Listing 3.1. The Orders property definition in the context class
Listing 3.2. A connection string example
Listing 3.3. Building a connection string
Listing 3.4. Modifying an existing connection string
Listing 3.5. Attaching a handler to the ObjectMaterialized event
Listing 3.6. Setting tracking options
Listing 3.7. Setting tracking via the Execute method
Listing 3.8. Iterating twice over query results, causing double query execution
Listing 3.9. Iterating twice over query results, causing single query execution
Chapter 4. Querying with LINQ to Entities
Listing 4.1. SQL generated by the previous query
Listing 4.2. Retrieving all orders that have sold more than one product
Listing 4.3. Retrieving an order given its ID using First
Listing 4.4. Retrieving an entity by its key using context methods
Listing 4.5. Applying filters dynamically
Listing 4.6. Grouping the shipping address information into a single property
Listing 4.7. Nesting anonymous types to group properties
Listing 4.8. Retrieving orders and customer information in a single object
Listing 4.9. Retrieving orders and customer information in a DTO
Listing 4.10. Retrieving projected orders and their projected details
Listing 4.11. Retrieving projected orders and their total
Listing 4.12. Iterating over the results of a grouping query
Listing 4.13. Changing the names of grouped data
Listing 4.14. Using multiple properties for grouping
Listing 4.15. Projecting the grouped data
Listing 4.16. Grouping orders only for cities that have more than two orders
Listing 4.17. Sorting with single and multiple properties
Listing 4.18. Sorting by an aggregated value of association
Listing 4.19. Retrieving projected orders and details ordered by quantity
Listing 4.20. Writing queries that use joins
Listing 4.21. Performing a group join to simulate SQL OUTER JOIN clause
Listing 4.22. Filtering products by type
Listing 4.23. Filtering products by type and data, and returning them as base types
Listing 4.24. Projecting the OrderDetail
Listing 4.25. Passing query parameters using a numbered list
Listing 4.26. Passing query parameters using a numbered list
Listing 4.27. Customer and details retrieved on demand
Listing 4.28. Manually retrieving the customer and details
Listing 4.29. Generic method that manually retrieves a property of an entity
Chapter 5. Domain model mapping
Listing 5.1. Creating the order scenario model
Listing 5.2. Two ways of instantiating complex properties
Listing 5.3. Implementing Equals and GetHashCode in the Order entity
Listing 5.4. Defining the entity sets in EntityContainer
Listing 5.5. Describing complex types and entities
Listing 5.6. Defining database tables and views
Listing 5.7. Describing the Order and OrderDetail database tables
Listing 5.8. Defining the containers in the mapping
Listing 5.9. Defining the entity set, the class, and the mapped table
Listing 5.10. Defining the mapping between properties and columns
Listing 5.11. Creating properties that reference the order from the order detail
Listing 5.12. Adding navigation and foreign-key properties to the entity in the CSDL
Listing 5.13. Declaring and defining associations
Listing 5.14. Defining a foreign-key association
Listing 5.15. Defining a database foreign key in the storage schema
Listing 5.16. Creating a property that references a collection of objects
Listing 5.17. Mapping an independent property
Listing 5.18. The Company, Customer, and Supplier classes
Listing 5.19. Defining inheritance in the conceptual schema
Listing 5.20. The Product, Shoe, and Shirt classes
Chapter 6. Understanding the entity lifecycle
Listing 6.1. Adding an entity to the context via the ObjectContext method
Listing 6.2. Adding an entity to the context with the ObjectSet<T> class method
Listing 6.3. Attaching an entity with the Attach method
Listing 6.4. Changing state with ApplyOriginalValues and ApplyCurrentValues
Listing 6.5. Deleting an entity
Listing 6.6. Changing object state using ChangeObjectState and ChangeState
Listing 6.7. Detaching an entity
Listing 6.8. Logging persisted objects
Listing 6.9. Retrieving an entry from the ObjectStateManager
Listing 6.10. Safely retrieving an entry from ObjectStateManager
Listing 6.11. Modifying a customer using an entity not wrapped inside a proxy
Chapter 7. Persisting objects into the database
Listing 7.1. Persisting a new customer
Listing 7.2. The SQL generated by the insert
Listing 7.3. Persisting a modified entity in the connected scenario
Listing 7.4. Updating an entity using ChangeObjectState
Listing 7.5. Updating an entity using ApplyCurrentValues
Listing 7.6. Deleting an entity in both connected and disconnected scenarios
Listing 7.7. Creating an order using foreign-key associations
Listing 7.8. Creating an order using independent associations
Listing 7.9. Correctly creating an order with independent associations
Listing 7.10. Updating an order in a connected scenario
Listing 7.11. Updating an order in the disconnected scenario
Listing 7.12. Changing the customer of an order using independent associations
Listing 7.13. Deleting an order with foreign-key associations, disconnected scenario
Listing 7.14. Deleting an order with independent associations
Chapter 8. Handling concurrency and transactions
Listing 8.1. Performing concurrency checks with ApplyCurrentValues
Listing 8.2. Performing concurrency checks with TPT hierarchies
Listing 8.3. Intercepting concurrency exception
Listing 8.4. Refreshing entities with values from the server
Listing 8.5. Showing entry values in a tree view display
Listing 8.6. Persisting only selected columns
Listing 8.7. Complete code for managing concurrency exceptions
Chapter 9. An alternative way of querying: Entity SQL
Listing 9.1. A simple Entity SQL query
Listing 9.2. Retrieving all orders shipped to New York
Listing 9.3. Using projected data
Listing 9.4. Returning projected data as typed objects
Listing 9.5. Projecting an order plus full details
Listing 9.6. Chaining query-builder methods
Listing 9.7. Chaining query-builder methods vs. chaining LINQ to Entities methods
Chapter 10. Working with stored procedures
Listing 10.1. The autogenerated method to invoke a stored procedure
Listing 10.2. Mapping resultset rows to types with a discriminator
Listing 10.3. Mapping resultset rows to types via a discriminator with TPT hierarchies
Listing 10.4. Stored procedure that returns a resultset plus an output parameter
Listing 10.5. Output parameters returned as ObjectParameter objects
Listing 10.6. Output parameters returned as simple values
Listing 10.7. A stored procedure that inserts an order
Listing 10.8. Mapping an entity to persistence stored procedures in the MSL
Listing 10.9. Declaring functions and letting them invoke stored procedures
Chapter 11. Working with functions and views
Listing 11.1. Code for querying a view, and the generated SQL
Listing 11.2. Stub method that maps a function in the storage to a CLR method
Listing 11.3. Using the stub function in LINQ to Entities
Listing 11.4. User-defined function that returns and accepts scalar values
Listing 11.5. User-defined function that accepts an object and returns a DbDataRecord
Listing 11.6. User-defined function that returns a list of DbDataRecord objects
Chapter 12. Exploring EDM metadata
Listing 12.1. Accessing the metadata using the MetadataWorkspace class
Listing 12.2. Forcing the loading of the storage and mapping schemas
Listing 12.3. Using GetItem<T> to retrieve metadata
Listing 12.4. Using TryGetItem<T> to retrieve metadata
Listing 12.5. Creating the entities node
Listing 12.6. Creating the base types nodes
Listing 12.7. Creating the derived type nodes
Listing 12.8. Creating the properties nodes
Listing 12.9. Creating the complex types nodes
Listing 12.10. Creating the functions nodes
Listing 12.11. Creating parameter nodes for each function
Listing 12.12. Creating the containers nodes
Listing 12.13. Creating the entity sets and function imports nodes
Listing 12.14. Creating the containers nodes
Listing 12.15. Adding or attaching an entity depending on the value in the EDM
Chapter 13. Customizing code and the designer
Listing 13.1. Template that creates an HTML file that lists a directory’s files
Listing 13.2. Creating methods in the template
Listing 13.3. Retrieving the storage schema
Listing 13.4. Creating the code for stub methods
Listing 13.5. Creating a data annotation containing a regular expression for a property
Listing 13.6. Creating a partial class
Listing 13.7. Workflow that generates a script to persist inheritance with TPT
Listing 13.8. Container and entity-set creation
Listing 13.9. Table, primary key, and discriminator definitions
Listing 13.10. Mapping entities to tables
Listing 13.11. Generating database tables
Listing 13.12. Creating the designer property class
Chapter 14. Designing the application around Entity Framework
Listing 14.1. Filling the productsList list box
Listing 14.2. Retrieving available products
Listing 14.3. Calculating the order total
Listing 14.4. Computing an order’s discount
Listing 14.5. Computing an order’s discount with DiscountPolicy
Listing 14.6. A generic IRepository<T> interface
Listing 14.7. Repository constructor
Listing 14.8. Query and GetSingle implementations
Listing 14.9. Add, Remove, and Update implementations
Chapter 15. Entity Framework and ASP.NET
Listing 15.1. EntityDataSource markup code that displays a list of products
Listing 15.2. The model and routes registration
Listing 15.3. POCO entities generated by a template with data annotations
Listing 15.4. Context wrapper creating an instance of the ObjectContext
Listing 15.5. HttpModule that instantiates the ObjectContext per request
Chapter 16. Entity Framework and n-tier development
Listing 16.1. The interface exposed by the service
Listing 16.2. The implementation of the Read method
Listing 16.3. The implementation of the Update method
Listing 16.4. Marking entities for WCF custom serialization
Listing 16.5. Class that plugs the contract resolver into WCF
Listing 16.6. The DTO class for the customer
Listing 16.7. The ReadDTO method using DTO
Listing 16.8. The UpdateDTO method using DTO
Listing 16.9. Detecting added, deleted, and modified details in an order
Chapter 17. Entity Framework and Windows applications
Listing 17.1. Implementing the INotifyPropertyChanged interface
Listing 17.2. Raising the data-changed event
Listing 17.3. Implementing the IEditableObject interface
Listing 17.4. Implementing the IDataErrorInfo interface
Listing 17.5. Adding errors to the dictionary
Listing 17.6. Binding controls in code
Listing 17.7. Detecting deleted details
Listing 17.8. Marking new orders as added before saving changes
Listing 17.9. A DataGrid that shows orders
Listing 17.10. Controls that show order information
Listing 17.11. Controls that show detail information
Listing 17.12. Intercepting an order’s removal and addition for context notification
Chapter 18. Testing Entity Framework
Listing 18.1. Calculating an order total
Listing 18.2. Console application that tests order’s Total calculated property
Listing 18.3. OrderTests class
Listing 18.4. Testing the raising of an exception
Listing 18.5. Test initialization method
Listing 18.6. Accessing a remote web service to check delivery status
Listing 18.7. A testable version of CheckDeliveryStatus
Listing 18.8. The ITrackingService interface and its fake implementation
Listing 18.9. Unit test for CheckDeliveryStatus
Listing 18.10. Testing with a Rhino Mocks stub
Listing 18.11. Testing with a mock
Listing 18.12. Repository’s Add method
Listing 18.13. Refactoring the RepositoryConstructor
Listing 18.14. Test code for the Add method
Listing 18.15. Query method of Repository<T>
Listing 18.16. The CustomerRepository.Add method
Listing 18.17. FakeObjectSet implementation (an excerpt)
Listing 18.18. Testing CustomerRepository.Add method with FakeObjectSet
Listing 18.19. Persistence test for the Customer entity
Listing 18.20. Using transactions in persistence tests
Listing 18.21. Persistence test of the Customer entity using EF Mapping Verifier
Chapter 19. Keeping an eye on performance
Listing 19.1. The custom timer
Listing 19.2. Inserting customers and suppliers using Entity Framework
Listing 19.3. Inserting customers and suppliers using ADO.NET
Listing 19.4. Retrieving customers using Object Services
Listing 19.5. Retrieving customers using Entity Client
Listing 19.6. Using EdmGen to generate views
Listing 19.7. The main part of the template that generates views
Appendix A. Understanding LINQ
Listing A.1. How type inference understands an object’s type
Listing A.2. Creating and using a helper method without extension methods
Listing A.3. Creating and using a helper method with extension methods
Listing A.4. A set of extension methods that add methods to different classes
Listing A.5. A method that searches orders by date
Listing A.6. Searching orders by date using anonymous methods
Listing A.7. Anatomy of an extension method
Listing A.8. Deferred and immediate execution of a LINQ query
Appendix B. Entity Framework tips and tricks
Listing B.1. Marking the entity as Modified or Deleted after attaching it
Listing B.2. Strongly typed extension method for marking a property as Modified
Listing B.3. Adding code to mark the selected properties
Listing B.4. The factory for the custom property of the entity in the designer
Listing B.5. Class that manages the auditable custom annotation for the entity
Listing B.6. The class that writes auditing data
Listing B.7. The new context class
Listing B.8. Template code for generating correct constructors
Listing B.9. The code that uses the new infrastructure
Listing B.10. The code that uses the new infrastructure
Listing B.11. The partial class that defines the TypedSpatialData property
18.221.179.220