In June 2006, I was invited to attend a meet-up for data geeks at Tech Ed North America. As the meet-up was early enough not to compete with the many fun evening parties at Tech Ed, 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, Tech Ed 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.
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 on the first edition, 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!
I spent almost a year writing the book that ended up at a little over 800 pages and more than two pounds on the scale. When the book was released in February 2009, Microsoft was already well underway on the next version of Entity Framework, which was going through major changes. After spending some time with the early releases of what was to become Entity Framework 4, and with some trepidation, I finally decided to revise the book. This was after having enlisted many friends to “please, just shoot me” if I ever talked about writing another book. Thankfully, nobody took me up on the request. They admitted it was because they wanted a new version of my book targeted at the new version of Entity Framework.
Once again, I had no idea what I was in for. This edition has been much more than a revision. I have had to rethink every sentence in the book, throw out entire chapters, add new chapters, rethink and rewrite most of the code samples, and of course, learn about a slew of major features that have been added to Entity Framework. I spent over nine months of constant effort writing this new edition, and now here it is.
This book is written for developers who are familiar with .NET programming, whether they are entirely new to the Entity Framework or have been using it and want to solidify their current understanding as well as go deeper. Throughout the book, I highlight notable changes for developers who have been using the first version of Entity Framework. The first half of the book (Chapters 1–12) covers introductory topics, and the latter half (Chapters 13–27) 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, WCF services, and WCF Data 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. This second edition focuses on the version of Entity Framework in Visual Studio 2010 and .NET 4.
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, many topics on tools that leverage the Entity Framework, such as WCF RIA Services (a.k.a. Astoria) and SQL Modeling, are touched on but not covered in depth.
Some of the Entity Framework’s features are comparable to LINQ to SQL and other object relational models such as NHibernate and LLBLGen Pro. Apart from a few paragraphs in Chapter 1, this book does not directly position the Entity Framework against these object relational models.
All of the code samples in Programming Entity Framework, Second Edition, are provided in C#. Where there are significant syntax differences, Visual Basic is included as well.
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:
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.
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.
The Entity Framework provides a number of methods for
querying against the EDM—LINQ to Entities, Entity SQL with
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
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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
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
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.
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
QueryView, and even nonexistent
database views and stored procedures that you can define directly
in the Entity Framework metadata.
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.
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
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
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:
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.
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?
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.
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.
This chapter follows up what you learned about exception
handling in Chapter 22 with details on
a particular type of exception: the
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 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.
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.
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.
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.
This appendix is a guide to the physical files that are part of the Entity Framework and each namespace in the programming model.
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.
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.
This book focuses on the release of Entity Framework that is part of Microsoft Visual Studio 2010 and .NET 4. You can use any of the Visual Studio 2010 versions, from Express through Ultimate.
Although the Entity Framework can work with many database
SqlClient provider is
part of Visual Studio 2010, and therefore all of the samples here are
based on SQL Server. You can use SQL Server Express or Standard, and
although the Entity Framework runtime will recognize versions 2000,
2005, and 2008, the design tools will not work with SQL Server 2000.
This book was written against SQL Server 2008 Standard.
Following is a specific list of system requirements:
Windows XP with SP2, Windows Server 2003, Windows Vista and SP1, or Windows 7
Microsoft SQL Server 2005, Microsoft SQL Server 2005 Express Edition, Microsoft SQL Server 2008, or Microsoft SQL Server 2008 Express Edition
Microsoft Visual Studio 2010
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.
The sample applications from the book. I will do my best to provide Visual Basic versions of many of the book’s samples. Note that there are also hundreds of small code samples in the book. In general, you will not find these small examples replicated on the website, although I will provide some of them for varying reasons.
The following typographical conventions are used in this book:
Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities
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
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
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, Second Edition, by Julia Lerman. Copyright 2010 Julia Lerman, 978-0-596-80726-9.”
If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at firstname.lastname@example.org.
Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.
With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.
O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.
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)|
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:
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:
And now for the most rewarding writing task after completing over 800 pages of technical writing—thanking the Academy. My academy is a host of bright, generous, and dedicated geeks (and a few nongeeks) who have helped make this book the best it can be.
First nods go to the technical reviewers. These are the folks who were willing to read the book in its roughest format and provide feedback to help me make it more useful and comprehensible to you, the readers of the final version. The award for helping to keep me from exposing myself to humiliation over my nascent C# skills goes to Wesley Bakker, a Dutch developer who does code reviews for a living. I learned a lot from Wes and am grateful for his patience and kid-glove handling of my poor ego. I also had a number of EF and EF 4 newbies on board to help ensure that I didn’t make any leaps without bringing them along. You who are new to EF should thank them as well: Camey Combs, Suzanne Shushereba, Doug Holland, and John McConnell. Ward Bell’s brilliant architectural mind was displayed in comments that nearly exceeded my own text. He kept me honest and kept me thinking. Everyone should email Ward and beg him to write a book. I don’t care what the topic is. Ward has deep EF knowledge, as does Per Okvist, whose feedback was also invaluable. Two database gurus were enormously helpful: Bob Beauchemin and Anil Das. Their meticulous minds helped me in areas that reached much further than discussions about database specifics.
I also brought in some big guns to look at particular chapters in their area of expertise. Thanks so much to Greg Young, Bobby Johnson, Jarod Ferguson, and Mike Campbell for helping me with my education in persistence ignorance and related topics and for looking over the critical chapter on PI and testing to make sure that I had learned my lessons well. I was close, but they helped guide me where I had strayed. K. Scott Allen and Imar Spaanjaars, both ASP.NET gurus, provided some additional guidance and a read-through of a number of chapters.
And then there was the real editing—the organization and flow of the text. John Osborn, who was the editor on the first edition of this book, was engaged to edit this edition as well. It’s hard for me to express my gratitude for the incredible dedication and expertise he provided. Even though I thought myself much more experienced this time around, John took every chapter and reorganized it, clarifying its focus and flow. He is an incredible editor and I was very lucky to have him work on my book again.
Along the way, of course, I had help from so many people at Microsoft on the Entity Framework team and beyond. There is no way I can list them all, but here’s my best shot (not in any special order): Danny Simmons, Elisa Flasko, Noam Ben-Ami, Diego Vega, Kati Iceva, Srikanth Mandadi, Alex James, Jarek Kowalski, Jeff Derstadt, Rowan Miller, Craig Lee, David Annesley-DeWinter, Adi Unnithan, Andrew Peters, Shyam Pather, and Tim Laverty. Forgive me if I’ve neglected to mention someone.
You’ll find that I have used (and recommended) a few additional tools throughout the book. The publishers generously provided me free licenses for which I’m grateful. The recommendations are because they are great tools, not because I didn’t have to pay for them. The tools include LINQPad, written by another O’Reilly author, Joseph Albahari; and ReSharper from JetBrains. ReSharper was my first line of defense for ensuring that my C# code wasn’t an embarrassment, while Wesley Bakker was my second. I learned so much from both of them. Entity Framework Profiler is an awesome tool for keeping track of what’s going on in your database when using Entity Framework. I also used two tools for producing images in this book. The first is Snagit from TechSmith, which was completely invaluable for capturing and editing screenshots. The second is Balsamiq Mockups, which enabled me to have a little fun creating mock-ups of application UIs in a number of chapters. Finally, thanks to Red Gate, a great company with many awesome tools. For this book, I used its .NET Reflector to inspect some assemblies, and I’ve used their SQL Packager for creating a simple-to-install version of the sample databases for you to use.
My publisher has, as usual, provided great support for me. I had not one, but two editors—this is not the job of editing the book, but of counseling me and holding my hand throughout the process. Thanks to Laurel Ruma (who moved on to become O’Reilly’s über–Government 2.0 guru), and Mike Hendrickson who brings years of experience (not saying he’s old) for keeping me focused and helping me avoid being taken away in a funny white coat. I was also lucky to have Audrey Doyle as my copy editor again. She did an amazing job on the first edition, so I begged O’Reilly to contract her again. Lucky me, they did. (She is going to hate that last nonsentence; I dare you to leave it in, Audrey.)
If you read the Preface of my first book, you’ll be happy to know that this time around I have no heart-wrenching pet losses to report, so you can put away the tissues you may have prepared yourself with. In fact, we adopted a teenage Newfoundland dog named Sampson just as I began to write this edition. Thank goodness for his needed afternoon walks and his constantly entertaining personality, without which I’d have gone completely mad during the time I have been writing this book. You can meet this silly boy on my blog at http://thedatafarm.com/blog/tags/Sampson.
Somehow I have managed to retain my patient husband, Rich Flynn, to whom I promised “don’t worry, never again” when I finished the first edition. He has just suffered through another year of spaghetti, dirty dishes, ravaged potato chip supplies, and having to cede a little more space in bed as my waistline expanded thanks to my life in the computer chair (and all those potato chips).
And finally, thanks to all of the incredible support that has come from the .NET community. I’m very proud of the first edition of the book, and each private “thank you” or complimentary public review on places like Amazon.com and your blogs has meant so much to me. This truly kept me going through what my Twitter followers know only too well was an arduous process in writing this second edition.
Oh, and to anyone who gave me chocolate…thanks!