Chapter List for Programming Entity Framework 2nd Edition
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 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.
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.
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 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 real-world 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.
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 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.