Book description
Get a thorough introduction to ADO.NET Entity Framework 4 -- Microsoft's core framework for modeling and interacting with data in .NET applications. The second edition of this acclaimed guide provides a hands-on tour of the framework latest version in Visual Studio 2010 and .NET Framework 4. Not only will you learn how to use EF4 in a variety of applications, you'll also gain a deep understanding of its architecture and APIs.
Written by Julia Lerman, the leading independent authority on the framework, Programming Entity Framework covers it all -- from the Entity Data Model and Object Services to WCF Services, MVC Apps, and unit testing. This book highlights important changes for experienced developers familiar with the earlier version.
- Understand the core concepts you need to make the best use of the EF4 in your applications
- Learn to query your data, using either LINQ to Entities or Entity SQL
- Create Windows Forms, WPF, ASP.NET Web Forms, and ASP.NET MVC applications
- Build and consume WCF Services, WCF Data Services, and WCF RIA Services
- Use Object Services to work directly with your entity objects
- Create persistent ignorant entities, repositories, and write unit tests
- Delve into model customization, relationship management, change tracking, data concurrency, and more
- Get scores of reusable examples -- written in C# (with notes on Visual Basic syntax) -- that you can implement right away
Publisher resources
Table of contents
- Programming Entity Framework
- Foreword
- Preface
- Author Note for Third Printing, August 2011
-
1. Introducing the ADO.NET Entity Framework
- 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
- Entity Framework Features: APIs and Tools
- The Entity Framework and WCF Services
- What About ADO.NET DataSets and LINQ to SQL?
- Entity Framework Pain Points Are Fading Away
- Programming the Entity Framework
-
2. Exploring the Entity Data Model
- Why Use an Entity Data Model?
- The EDM Within the Entity Framework
- Walkthrough: Building Your First EDM
- Inspecting the EDM in the Designer Window
- The Model’s Supporting Metadata
- Viewing the Model in the Model Browser
- Viewing the Model’s Raw XML
- CSDL: The Conceptual Schema
- SSDL: The Store Schema
- MSL: The Mappings
- Database Views in the EDM
- Summary
-
3. Querying Entity Data Models
- Query the Model, Not the Database
- Your First EDM Query
- Querying with LINQ to Entities
- Querying with Object Services and Entity SQL
- Querying with Methods
- The Shortest Query
- ObjectQuery, ObjectSet, and LINQ to Entities
- Querying with EntityClient to Return Streamed Data
- Translating Entity Queries to Database Queries
- Avoiding Inadvertent Query Execution
- Summary
-
4. Exploring LINQ to Entities in Greater Depth
- Getting Ready with Some New Lingo
- Projections in Queries
- Projections in LINQ to Entities
- Using Navigations in Queries
- Joins and Nested Queries
- Grouping
- Shaping Data Returned by Queries
- Loading Related Data
- Retrieving a Single Entity
- Finding More Query Samples
- Summary
- 5. Exploring Entity SQL in Greater Depth
- 6. Modifying Entities and Saving Changes
-
7. Using Stored Procedures with the EDM
- Updating the Model from a Database
- Working with Functions
- Mapping Functions to Entities
- Using the EDM Designer Model Browser to Import Additional Functions into Your Model
- Mapping the First of the Read Stored Procedures: ContactsbyState
- Mapping a Function to a Scalar Type
- Mapping a Function to a Complex Type
- Summary
-
8. Implementing a More Real-World Model
- Introducing the BreakAway Geek Adventures Business Model and Legacy Database
- Creating a Separate Project for an EDM
- Inspecting and Cleaning Up a New EDM
- Setting Default Values
- Mapping Stored Procedures
- Working with Many-to-Many Relationships
- Inspecting the Completed BreakAway Model
- Building the BreakAway Model Assembly
- Summary
-
9. Data Binding with Windows Forms and WPF Applications
-
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
- 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
-
Data Binding with Windows Forms Applications
-
10. Working with Object Services
- 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
- Managing Relationships
- Taking Control of ObjectState
- Sending Changes Back to the Database
- Implementing Serialization, Data Binding, and More
- Summary
-
11. Customizing Entities
- Partial Classes
- Using Partial Methods
- Subscribing to Event Handlers
- Creating Your Own Partial Methods and Properties
- Overriding Default Code Generation
- Summary
-
12. Data Binding with RAD ASP.NET Applications
- Using the EntityDataSource Control to Access Flat Data
- Understanding How the EntityDataSource Retrieves and Updates Your Data
-
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
-
13. Creating and Using POCO Entities
- Creating POCO Classes
- Change Tracking with POCOs
- Loading Related Data with POCOs
- Exploring and Correcting POCOs’ Impact on Two-Way Relationships
- Using Proxies to Enable Change Notification, Lazy Loading, and Relationship Fix-Up
- Using T4 to Generate POCO Classes
- Creating a Model That Works with Preexisting Classes
- Code First: Using Entity Framework with No Model at All
- Summary
-
14. Customizing Entity Data Models Using the EDM Designer
- Mapping Table per Type Inheritance for Tables That Describe Derived Types
- Mapping Unique Foreign Keys
- Mapping an Entity to More Than One Table
- Splitting a Single Table into Multiple Entities
- Filtering Entities with Conditional Mapping
- Implementing Table per Hierarchy Inheritance for Tables That Contain Multiple Types
- Creating Complex Types to Encapsulate Sets of Properties
- Using Additional Customization Options
- Summary
- 15. Defining EDM Mappings That Are Not Supported by the Designer
-
16. Gaining Additional Stored Procedure and View Support in the Raw XML
- Reviewing Procedures, Views, and UDFs in the EDM
- Working with Stored Procedures That Return Data
- Executing Queries on Demand with ExecuteStoreQuery
- Adding Native Queries to the Model
- Adding Native Views to the Model
- Using Commands That Affect the Database
- Mapping Insert/Update/Delete to Types Within an Inheritance Structure
- Implementing and Querying with User-Defined Functions (UDFs)
- Summary
-
17. Using EntityObjects in WCF Services
- Planning for an Entity Framework–Agnostic Client
- Building a Simple WCF Service with EntityObjects
- Implementing the Service Interface
- Building a Simple Console App to Consume an EntityObject Service
-
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
- Understanding How WCF RIA Services Relates to the Entity Framework
- Summary
-
18. Using POCOs and Self-Tracking Entities in WCF Services
- Creating WCF-Friendly POCO Classes
- Building a WCF Service That Uses POCO Classes
- Using the Self-Tracking Entities Template for WCF Services
- Using POCO Entities with WCF Data and RIA Services
- Sorting Out the Many Options for Creating Services
- Summary
-
19. Working with Relationships and Associations
- Deconstructing Relationships in the Entity Data Model
- Understanding the Major Differences Between Foreign Key Associations and Independent Associations
- Deconstructing Relationships Between Instantiated Entities
-
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
- Summary
-
20. Real World Apps: Connections, Transactions, Performance, and More
- Entity Framework and Connections
- Fine-Tuning Transactions
- Understanding Security
- Fine-Tuning Performance
- Exploiting Multithreaded Applications
- Exploiting .NET 4 Parallel Computing
- Summary
-
21. Manipulating Entities with ObjectStateManager and MetadataWorkspace
-
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
- Building Dynamic Queries and Reading Results
- Creating and Manipulating Entities Dynamically
- Summary
-
Manipulating Entities and Their State with ObjectStateManager
-
22. Handling Exceptions
- Preparing for Exceptions
- Handling EntityConnectionString Exceptions
- Handling Query Compilation Exceptions
- 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
-
23. Planning for Concurrency Problems
- Understanding Database Concurrency Conflicts
- Understanding Optimistic Concurrency Options in the Entity Framework
-
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
- Handling Concurrency Exceptions at a Lower Level
- Handling Exceptions When Transactions Are Your Own
- Summary
-
24. Building Persistent Ignorant, Testable Applications
- Testing the BreakAway Application Components
- Getting Started with Testing
-
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
- Application Architecture Benefits from Designing Testable Code
- Considering Mocking Frameworks?
- Summary
-
25. Domain-Centric Modeling
- Creating a Model and Database Using Model First
- Using the Feature CTP Code-First Add-On
- Using SQL Server Modeling’s “M” Language
- Summary
- 26. Using Entities in Layered Client-Side Applications
-
27. Building Layered Web Applications
- Understanding How ObjectContext Fits into the Web Page Life Cycle
-
Building an N-Tier Web Forms Application
- Designing the Application
- Using the Existing Repositories
- 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
- Editing Entities and Graphs on an MVC Application
- Summary
- A. Entity Framework Assemblies and Namespaces
- B. Data-Binding with Complex Types
- C. Additional Details About Entity Data Model Metadata
- Index
- About the Author
- Colophon
- Copyright
Product information
- Title: Programming Entity Framework, 2nd Edition
- Author(s):
- Release date: August 2010
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9780596807269
You might also like
book
Programming Entity Framework: Code First
Take advantage of the Code First data modeling approach in ADO.NET Entity Framework, and learn how …
book
Programming Entity Framework: DbContext
The DbContext API captures Entity Framework’s (EF) most commonly used features and tasks, simplifying development with …
book
Entity Framework 6 Recipes, Second Edition
Entity Framework 6 Recipes provides an exhaustive collection of ready-to-use code solutions for Entity Framework, Microsoft's …
book
Entity Framework Core Cookbook - Second Edition
Leverage the full potential of Entity Framework with this collection of powerful and easy-to-follow recipes About …