Preface

In June 2006, I was invited to attend a meet-up for data geeks at TechEd North America. As it was early enough not to compete with the many fun evening parties at TechEd, 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, TechEd 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.

Note

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.

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, 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!

Over these past two years, I have torn apart the Entity Framework to figure out how it works and how to take advantage of it to access data as well as work with the data that is returned. I have worked very closely with the Entity Framework team and am grateful to them for listening to my feedback and helping me to better understand this technology, as well as for the encouragement and support they have given me while writing this book. In the course of presenting on the Entity Framework at .NET User Groups and conferences around the world, as well as spending a lot of time in the MSDN forums interacting with others who are investigating and implementing the Entity Framework into their applications, I have been able to look at the Entity Framework through the eyes and questioning minds of hundreds of developers, which has been so educational.

This book is the culmination of understanding how the Entity Framework version 1 can meet not only my demands as a developer, but also those of the many developers and architects I have interacted with in the past few years.

Who This Book Is For

This book is written for developers who are familiar with .NET programming, whether you are entirely new to the Entity Framework or have been using it and want to solidify your current understanding as well as go deeper. The first half of the book (Chapters 114) covers introductory topics, and the latter half (Chapters 1522) dives under the covers to give you a deep understanding 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, Web Services, and WCF 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. Its focus is the version 1 that was released as part of Visual Studio Service Pack 1 and .NET 3.5 Service Pack 1.

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, topics on tools that leverage the Entity Framework, such as ADO.NET Data Services (a.k.a. Astoria) and the ASP.NET Dynamic Data Controls, are not included.

Some of the Entity Framework’s features are comparable to LINQ to SQL and other object relational models such as NHibernate and LLBLGen. Apart from a few paragraphs in Chapter 1, this book does not directly position the Entity Framework against these object relational models.

Note

All of the code samples in Programming Entity Framework are provided in both Visual Basic and C#. The book includes many debugger screenshots, and unless noted, these have been taken from the Visual Basic IDE.

How This Book Is Organized

Programming Entity Framework 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 deeply 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 method. 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 data that results.

Chapter 4, Exploring EDM Queries 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 huge variety of queries, and for each task you will see how to write the query using the various methods of querying. 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. There are more than 100 query samples in this chapter.

Chapter 5, 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 6, 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 13 covers the set of scenarios that require more effort.

Chapter 7, Tuning Up a 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, 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. Chapter 12 will go even further into customizing the model with advanced modeling and mappings.

Chapter 8, 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 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 9, 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 10, 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 19.

Chapter 11, Using the ASP.NET EntityDataSource Control

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. Later chapters will teach you what you need to know to overcome these hurdles yourself, and Chapter 20 leverages this knowledge to address building layered ASP.NET applications rather than putting the logic in the UI.

Chapter 12, Customizing Entity Data Models

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, 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 13, Working with Stored Procedures When Function Mapping Won’t Do

Chapter 6 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 its most commonly used features.

Chapter 14, Using Entities with Web and 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 an ASMX (“classic”) Web Service and then build and consume a WCF service. If you have never created services before, have no fear. The walkthroughs will help you with step-by-step instructions. This chapter is the first of two that address building services. Chapter 22 revisits building WCF services, leveraging knowledge you will gain in the latter portion of the book.

The preceding chapters will have provided you with a solid base of understanding and working with the Entity Framework. Starting with Chapter 15, you will learn about the Entity Framework’s advanced topics:

Chapter 15, 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. In fact, relationships are instantiated as objects. 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 should have a deep comprehension of how relationships work in the model and your Entity Framework code. This chapter will provide you with that.

Chapter 16, Making It Real: 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 17, Controlling Objects 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 EntityObjects 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 18, Handling Entity Framework 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. 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 the OptimisticConcurrencyException to detect these problems. The chapter will also show you how to take advantage of this.

Chapter 19, Using Your Own Custom Classes

In Chapter 10, you learned how to customize the classes generated from the model. However, you can use your own classes as well and still take advantage of the model, querying, and the change tracking and relationship management features of Object Services. In this chapter, you will learn two mechanisms for implementing your own classes into an Entity Framework-based application: inheriting from EntityObject and implementing the IPOCO interfaces.

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 next three chapters focus on challenges and solutions for using the Entity Framework in enterprise applications. The book concludes with a forward look at what’s down the road for the Entity Framework, and an appendix that serves as a guide to the assemblies and namespaces of the Entity Framework:

Chapter 20, Using the Entity Framework in n-Tier Client-Side Applications

The earlier Windows application walkthroughs 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.

Chapter 21, Using the Entity Framework in n-Tier ASP.NET Applications

Chapter 11 focused on building 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 EntityObjects, 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 walks through a solution that leverages the ASP.NET ObjectDataSource control to work with classes that control all of the interaction with the Entity Framework. The sample in this chapter provides a first step toward concepts that will help you architect applications to fit your own domain model.

Chapter 22, Implementing a Smarter WCF Service for Working with Entities

Like the preceding two chapters, this chapter revisits WCF using all of the lessons you’ve learned about the Entity Framework since Chapter 14. In this chapter, you will see a WCF service that leverages Data Transfer Objects (DTOs) to not only create a much more succinct and lightweight payload for the consumer, but also build in properties you can use to overcome the challenges of change tracking across tiers.

Chapter 23, The Entity Framework, Today and Tomorrow

The Entity Framework described in this book is version 1. As this book is going to print, the Entity Framework team is hard at work on version 2. This chapter introduces you to some additional resources that will fill in some of the gaps in version 1 that you can use today, and it introduces you to some of the features you will see when the next iteration of the Entity Framework is released as part of .NET 4.0 and Visual Studio 2010.

Appendix A

This appendix is a guide to the physical files that are part of the Entity Framework and each namespace in the programming model.

What You Need to Use This Book

The Entity Framework is part of Microsoft Visual Studio 2008 Service Pack 1. You can use the Entity Framework with any of the Visual Studio 2008 versions, from Express through Team System.

Although the Entity Framework can work with many database providers, the SqlClient provider is part of Visual Studio 2008, and therefore all of the samples here are based on SQL Server. You can use SQL Server Express or Standard, and the Entity Framework will recognize versions 2000, 2005, and 2008. The first draft of this book was written against SQL Server 2005 and the final version used SQL Server 2008.

Following is a specific list of system requirements:

  • Windows XP with SP2, Windows Server 2003, or Windows Vista and SP1

  • Microsoft SQL Server 2000, Microsoft SQL Server 2005, Microsoft SQL Server 2005 Express Edition, Microsoft SQL Server 2008, or Microsoft SQL Server 2008 Express Edition

  • Microsoft Visual Studio 2008 with SP1

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. Scripts for SQL Server 2005 and SQL Server 2008 are provided.

  • The sample applications from the book in both Visual Basic and C#. Note that there are also hundreds of code samples in the book. You will find many but not all of them on the website as well.

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

Note

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

This icon indicates a Visual Basic code sample.

This icon indicates a C# code sample.

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, by Julia Lerman. Copyright 2009 Julia Lerman, 978-0-596-52028-1.”

If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at .

Safari® Books Online

Note

When you see a Safari® Books Online icon on the cover of your favorite technology book, that means the book is available online through the O’Reilly Network Safari Bookshelf.

Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.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)

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/9780596520281/

To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our website at:

http://www.oreilly.com/

Acknowledgments

Where do I begin? I have had great help and support from many people at Microsoft and many developers around the globe. At Microsoft, I would like to thank many people on the Entity Framework and Data Programmability teams for their support and continual efforts to help me not only understand the Entity Framework, but also enable me to share my understanding with so many others through speaking at conferences and user groups as well as through articles. Elisa Flasko and Mike Pizzo helped me to better understand the big picture of the Entity Framework and have ensured that I have access to the information I need. Technically, I couldn’t have done any of this without the support of Danny Simmons, Zlatko Michailov, Pablo Castro, Noam Ben-Ami, Colin Meek, Sanjay Nagamangalam, Diego Vega, Mike Kaufman, Jeff Reed, Alex James, Jeff Derstadt, Brad Sarsfield, and Bruno Gardia Robles. Many of these folks patiently answered my questions in the forums as well as in elaborate email threads (even on nights and weekends). They also looked over many of the chapters to make sure I didn’t mislead anyone, and to provide further enlightenment.

Throughout the course of writing this book, a number of folks in the community looked at what I was doing and helped me shape the content. Everyone reading this book owes these people a debt of gratitude, as their feedback and comments had a huge impact on the final product. My harshest critic, whom I thank profusely for that criticism, was Jim Wooley, coauthor of the beautifully written LINQ in Action. My muse for making sure I wasn’t too confusing for those brand-new to the Entity Framework was Camey Combs, who I’m proud to report recently gave her first Code Camp presentation in Seattle and her topic was the Entity Framework. Fabulous feedback in critical areas from Shawn Wildermuth, Markus Rytterkull, Jarod Ferguson, Tony Sneed, and Paul Gielens also had a big impact on the final product. But the man to blame for this whole adventure is Guy Barrette, who gave me the final push from contemplating this book to committing to it.

I am grateful for the friendship and support of three brilliant women who have been through this process before: Kathleen Dollard, Kate Gregory, and Michele Leroux Bustamante.

Thanks also to TechSmith (http://www.techsmith.com/), which provided me with a license to its awesome screen capture utility, SnagIt, which I used for all of the screenshots in the book. And if not for Tangible’s Instant C# and Instant VB (http://tangiblesoftwaresolutions.com/) code conversion tools, I might still be sitting here pounding out the dual C# and VB samples used throughout the book.

Thanks to my editor, John Osborn, for keeping me on track and giving me lots of virtual pats on the back when I experienced some of the difficulties of book writing that many other technical authors warned me about, and in general to O’Reilly Media for so much support and encouragement and boxes full of chocolate goodies when I needed them most. (Thanks, Laurel!) As this is my first book, I had no idea what I was getting into. John was a great mentor with respect to understanding how to bring structure to the flurry of ideas that I was eager to get down on paper.

Audrey Doyle, the copyeditor, was heroic in not only cleaning up my words, but also keeping me honest when I drifted off course. At a later stage in the process, O’Reilly’s production editor, Loranah Dimant, was equally patient, tireless, and meticulous as we polished everything up to get it ready for the printer.

My mother is an author whose very first novel, written in her early 30s, was put up for a Pulitzer Prize by her publisher, DoubleDay Press. She’s a tough act to follow for sure, but here I am having found my own path to writing. She’ll not understand a word of this book, but we have found new entertainment in comparing our books’ daily rankings on Amazon.

The guy who suffered most through this long and arduous process was my very patient husband, Rich Flynn, who thankfully expanded his culinary expertise beyond Kraft Macaroni and Cheese as I emerged less frequently from my office to make dinner. In so many ways, Rich truly enabled me to hunker down to work on this book so that I didn’t have to worry too much about the day-to-day things in our lives, even though much of what I was doing was a complete mystery to him.

In the middle of this process, during one awful week, I suffered the painful loss of my two beloved Newfoundland dogs, Tasha and Daisy. At ages 14½ and 13½, these dear, dear girls had lived wonderfully long lives, which is very unusual for “Newfies.” I have put off truly mourning them so that I could get through writing this book, and I thank their lovely spirits for their patience.

Get Programming Entity Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.