List of Figures
Chapter 1. Data access reloaded: Entity Framework
Figure 1.1. The Order table has a related OrderDetail table that contains its details.
Figure 1.2. The Order class contains data from the Order table.
Figure 1.3. The Order2 table contains columns for the new data and is related to the Order table. In the object model, there’s
no new class—just a new property on the Order class.
Figure 1.4. The relationship between Order and OrderDetail in the object model is expressed with properties.
Figure 1.5. In the database, the Product and Supplier tables are related via the ProductSupplier table. In the model, the
Product and Supplier classes are directly related using properties.
Figure 1.6. An excerpt of the new Order table with the new billing address field
Figure 1.7. The Supplier and Customer classes inherit from Company.
Figure 1.8. Shirt and Shoes inherit from Product, which is referenced by OrderDetail.
Figure 1.9. The overall architecture of Entity Framework: The query languages lie on top of Object Services, which relies
on Entity Client to interact with the database. Entity Client uses the standard ADO.NET providers to physically communicate
with the database. The EDM is a layer that’s referenced by all the others and is used by them to obtain metadata about classes.
Figure 1.10. How data is received by the Object Services layer
Chapter 2. Getting started with Entity Framework
Figure 2.1. OrderIT use cases
Figure 2.2. On the left is the Company table; on the right is the model with its inheritance hierarchy.
Figure 2.3. Mapping between the product tables and entities. Each class has its own mapped table (TPT).
Figure 2.4. The relationship between suppliers and products is held in a link table in the database; there’s a direct relationship
in the model.
Figure 2.5. The Order and OrderDetail classes have their corresponding tables.
Figure 2.6. The design of OrderIT
Figure 2.7. The wizard for importing the database
Figure 2.8. The Add Entity wizard for adding the Company entity
Figure 2.9. The AddressInfo complex type in the Model Browser window
Figure 2.10. Mapping the Company entity
Figure 2.11. Mapping the Customer entity
Figure 2.12. The dialog box for creating inheritance between existing entities
Figure 2.13. Creating the association between Customer and Order
Figure 2.14. Mapping the association between Customer and Order
Figure 2.15. Mapping the many-to-many association between Product and Supplier
Figure 2.16. The Extension Manager window shows downloadable POCO templates as well as those that are already installed on
the machine.
Chapter 3. Querying the object model: the basics
Figure 3.1. Queries written against classes are transformed into SQL.
Figure 3.2. How a LINQ to Entities query passes through different layers and becomes a set of objects
Figure 3.3. The proxy class (bottom) inherits from the object model class (top) and overrides properties.
Chapter 4. Querying with LINQ to Entities
Figure 4.1. The chained methods filter data and count how many products are sold.
Figure 4.2. The structure returned by a grouping query
Figure 4.3. Seven products: five of type Shirt and two of type Shoe
Figure 4.4. ExecuteStoreQuery performs a mapping based on the column name.
Figure 4.5. How the proxy overrides the code of a property that navigates to another entity
Chapter 5. Domain model mapping
Figure 5.1. The context menu displayed when you right-click the EDMX file
Figure 5.2. The Open With dialog box lets you open the EDMX file as an XML file instead of showing the designer.
Figure 5.3. A collapsed view of the EDMX file. The Runtime section contains the EDM, and the Designer section contains the
designer-related information.
Figure 5.4. The structure of the CSDL file. Schema is the main element, and EntityContainer, ComplexType, and EntityType are
its children.
Figure 5.5. The main structure of the mapping file
Chapter 6. Understanding the entity lifecycle
Figure 6.1. The different entity states and the context methods that make them change
Figure 6.2. The code injected by the proxy notifies the state manager about changes in properties.
Figure 6.3. The state manager entries after an order with independent associations is attached (shown in the QuickWatch window).
There are six entities (one for the order, one for the customer, two for the order details, and two for their related products)
and five entries for the relationships between the entities (one for the relationship between the order and the customer,
two for the relationship between the order and its details, and two for the relationship between the order details and their
product).
Figure 6.4. The state manager after an order with foreign-key associations is attached. There are three entities (one for
the order and two for the details). The relationship entries don’t exist because foreign keys are used.
Chapter 7. Persisting objects into the database
Figure 7.1. The steps performed by the SaveChanges method to persist entity state
Figure 7.2. The billing address and city are empty in the persisted entity, so the database’s original values are lost after
persistence.
Figure 7.3. An order and details with matching keys are updated. Details from the input order with a key of 0 have been added.
Other details from the database order are deleted.
Chapter 8. Handling concurrency and transactions
Figure 8.1. A typical concurrency scenario. Both users read the same data, employee 1 saves the records first, and employee
2 overrides them later. New detail 3 and the modifications to detail 2 are lost.
Figure 8.2. In pessimistic concurrency, employee 1 reads the data, and it can’t be read by employee 2 until it’s saved by
employee 1.
Figure 8.3. With optimistic concurrency, employee 1 and employee 2 read order version 1. Employee 1 saves the order, which
is updated to version 2. Employee 2 saves order version 1 and gets an exception.
Figure 8.4. Enabling concurrency checking for a property in the designer
Figure 8.5. The tree view showing original and current values of the conflicting Customer entry. Employee 2 is changing the
name from “Giulio Mostarda” to “Stefano Mostarda”. In the meantime, employee 1 has changed the password from “PWD1” to “password1”.
Figure 8.6. The first INSERT is executed under a database transaction. Later, the transaction is promoted, and then the second
INSERT is issued.
Chapter 9. An alternative way of querying: Entity SQL
Figure 9.1. The ShippingAddress column in the DbDataRecord is a DbDataRecord representing the complex property.
Figure 9.2. The result of projecting both orders and details
Figure 9.3. The result of a grouping query. The first column contains the grouping field—City, in this case. The second column
contains the orders shipped to the city.
Figure 9.4. The structure of an EntityDataReader record returned by listing 9.8
Chapter 10. Working with stored procedures
Figure 10.1. The GetOrderDetails stored procedure in the wizard
Figure 10.2. The GetOrderDetails stored procedure in the Model Browser window
Figure 10.3. The wizard makes the stored procedure available on the conceptual side and maps its result to the OrderDetail
class.
Figure 10.4. Once imported, the GetOrderDetails stored procedure is visible in the Model.
Figure 10.5. The mapping between the function result and the class is based on the match between column and property names.
Figure 10.6. The result of the GetTopOrders stored procedure
Figure 10.7. The class that matches the results of the GetTopOrders stored procedure
Figure 10.8. The structure of the stored procedure’s results
Figure 10.9. Mapping a column to a property with a different name
Figure 10.10. Mapping function result to a class with a complex property is impossible due to the names mismatch.
Figure 10.11. The wizard imports into the conceptual schema a stored procedure that returns a single decimal value.
Figure 10.12. Mapping between the function result and the objects. Values in the first Color column are used to set both Shirt
and Shoe entity properties. The second Color column is ignored.
Figure 10.13. Mapping between the function result and the objects. The Color, Size, and Gender columns are used to fill properties
of both classes.
Figure 10.14. The function-execution process: Entity Framework gets the SQL from the SSDL and then executes it against the
database.
Figure 10.15. Mapping a stored procedure to persist a new order. The same process applies for modifications and deletions.
Figure 10.16. Mapping the stored procedure’s parameters to the entity’s properties
Figure 10.17. The order is inserted using a stored procedure; details are stored using Entity Framework–generated code.
Figure 10.18. The original value of the Version field is passed to the stored procedure. The other parameters take the property’s
current value.
Chapter 12. Exploring EDM metadata
Figure 12.1. On the left side is a snapshot of items returned by GetItems in the CSpace. On the right side are items returned
by GetItems in the SSpace.
Figure 12.2. The tree view where metadata is shown. The conceptual side is divided into entities, complex types, functions,
and containers. The same is done for the storage side.
Figure 12.3. The regular expression of the IBAN is reachable through the property metadata.
Chapter 13. Customizing code and the designer
Figure 13.1. A template file shown in the Solution Explorer
Figure 13.2. The designer properties that manage database script generation
Figure 13.3. The workflows and templates installed by the Database Generation Power Pack
Figure 13.4. The custom annotation value is shown in the Property Editor when the entity property is selected.
Figure 13.5. The Property window asks the designer for additional properties to display. The designer delegates the factory,
which instantiates the property class that is then displayed by the Properties window.
Figure 13.6. Configuring the extension project to debug the extension
Chapter 14. Designing the application around Entity Framework
Figure 14.1. A diagram of the three-layer architecture
Figure 14.2. An interface for an order-placing web page
Figure 14.3. A sequence UML diagram of the order-placing process
Figure 14.4. Application layering according to DDD
Figure 14.5. An Order and Customer UML diagram
Figure 14.6. UML diagram with the Address value object
Figure 14.7. An updated UML diagram with Order and Customer aggregates
Figure 14.8. The refined model of the discount policy
Chapter 15. Entity Framework and ASP.NET
Figure 15.1. You can insert an EntityDataSource control via the Visual Studio toolbar. It can be found under the Data category.
Figure 15.2. It’s possible to directly define a new EntityDataSource using the associated Visual Studio smart task. Choose
the New Data Source option to open a specific wizard.
Figure 15.3. By selecting Entity in the Data Source Configuration Wizard, you can later define all the details associated
with the to-be-created EntityDataSource.
Figure 15.4. Choose the entity from the EntitySetName drop-down list. If you don’t need to create a partial view of your entity,
Select All is the preferred option in the Select box.
Figure 15.5. The rows are materialized into entities and displayed in the web page using the EntityDataSource control’s capabilities.
This is performed without writing code.
Figure 15.6. Dynamic Data Entities Web Application is the template project you need to select to start using Dynamic Data
controls with Entity Framework. The other templates won’t work for this purpose.
Figure 15.7. Dynamic Data controls are capable of displaying any mapped entity. As shown here, the display can be customized.
Figure 15.8. The data-annotation attributes modify the way the properties are displayed and how their values are handled.
By using them, you can influence what Dynamic Data does.
Figure 15.9. By using the IoC container, both the ObjectContext and repositories are resolved at runtime. This helps to avoid
coupling and lets you manage the ObjectContext’s lifecycle correctly.
Figure 15.10. The customers in the database are displayed using the new design.
Chapter 16. Entity Framework and n-tier development
Figure 16.1. The path of an update in a n-tier application. The context that’s used to read a customer is different from the
one used to update it. Furthermore, client change-tracking isn’t available.
Figure 16.2. WCF knows Customer but not CustomerProxy, so it throws an exception when serializing CustomerProxy.
Figure 16.3. WCF knows Customer, and when it receives CustomerProxy, it uses the resolver to map CustomerProxy to Customer.
Figure 16.4. The flow when the context is disposed of and a proxied entity is later serialized. Accessing the navigation properties
(OrderDetail) causes the navigation properties to be lazy loaded and a serialization exception to be thrown because the context
is disposed of.
Figure 16.5. When lazy loading is active, serialization can generate lots of unwanted queries to the database, which may lead
to an entire graph being loaded.
Figure 16.6. The client reads the STE, modifies it, and then sends it back. During the process, the STE keeps track of the
changes.
Figure 16.7. Adding STEs to the project
Chapter 17. Entity Framework and Windows applications
Figure 17.1. The Windows form shows orders and their data in the Orders box and the details related to the current order in
the Order Details box.
Figure 17.2. The second page of the wizard allows you to select data-source classes.
Figure 17.3. The form shows the orders and the current order data.
Figure 17.4. Configuring the combo box. The CompanyId and CustomerID properties are bound to enable the lookup.
Figure 17.5. Details related to the selected order are shown in the second grid.
Figure 17.6. The WPF form showing orders and details
Chapter 18. Testing Entity Framework
Figure 18.1. A typical test report
Figure 18.2. Creating a new test project in Visual Studio 2010
Figure 18.3. Order.Total test-results report
Figure 18.4. A mock is able to track unexpected invocations.
Figure 18.5. Repository and ObjectContextWrapper
Figure 18.6. Conceptual flow of a persistence test
Chapter 19. Keeping an eye on performance
Figure 19.1. The testing application. On the left are the buttons to start the tests and check boxes to enable various optimizations.
The right ListView shows the results of each test.
Appendix A. Understanding LINQ
Figure A.1. Searching data from different sources without LINQ requires a wide knowledge of retrieval methods.
Figure A.2. Data access using LINQ technology
Figure A.3. Visual Studio autocompletion without and with the using statement
Figure A.4. The structure of a LINQ query that uses a lambda expression
Figure A.5. The skeleton of the anonymous type generated by the compiler
Appendix B. Entity Framework tips and tricks
Figure B.1. The class that maps to the table. It exposes the Id as Int32 and SpatialData as an array of bytes.