How This Book Is Organized
本书组织结构
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. Followingis a brief description of each chapter:
Programming Entity Framework 第二版,你可以从两个方面来学习,如果你偏好从实例开始,你将会找到许多演练和代码案例贯穿于本书,如果你喜欢看书,你也将看到有些章节在概念层的深入讲解。我尽量平衡实例演练和概念信息。第一部分主要是介绍性的,第二部分则更加深入。下面是每个章节的简要介绍。
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.
第1章:介绍 ado.net entity framework
本章是ado.net entity framework概述,介绍一下其来源,解决什么问题,和以很高高度来看看EF是什么。本节还会解答一些常见的关于EF的疑问,比如EF在.NET Framework中的地位,他能使用什么数据库, EF适合什么样的程序,EF和ORM有何不同,他如何跟最新的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.
第2章,探究EDM(实体数据模型)
EDM中EF和核心,本章节解释EDM是什么,并且教会你如何使用EDM向导去创建一个EDM和使用设计器如何去控制你的Model。演示一下EDM的各个组成部分,使用设计器或者规则XML来查看EDM
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.
第3章,查询实体数据模型
EF提供了许多查询edm的方法,比如linq to entities,基于ObjectQuery 的entity sql,entityClient,等方法。每种方法都有其利弊,通过基础原理的学习能够获取查询背后的方方面面,以便灵活掌控各种查询方法的使用。
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.
第4章,深入探究 linq to entities
掌握查询基础后,可以开始学习如何执行各种不同技巧的查询,投影转换,过滤,聚合等等,对待查询的对象是有关系的,可以使用它们的关系进行查询。本节将引领你使用各种linq toentitys的查询技巧,虽然不是所有的查询技巧都能够讲解到位,但是至少是个非常好的开始。
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.
第5章,深入探究 entity sql
本章回顾第4章的linq to entities 查询,并展示如果使用entity sql来实现同类的查询。当然你也能学到一些entity sql特别的查询技巧
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.
第6章,修改实体并保存修改
本节以高视角来看EF如果跟踪实体变化,处理更新和建立最终在数据库中执行的查询。离了接EF的默认功能后,准备涉及普遍关注的安全和性能方面的问题,此外,理解默认处理对后续章节的学习理解非常有意义。
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.
第7章,EDM中使用存储过程
深入EF中的存储过程的使用分两部分。本章是第一部分,讲解 EDM设计器中提供的对场景集的支持,第16章讲解关于场景集更有难度的东西。
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.
第 8章, 实现一个更真实的模型
至此在这本书中,你会一直与非常简单数据库和模式打交道,这样你可以专注于所有的新工具。这一章介绍了一种更大的模型和数据库就是虚拟旅游探险公司极客冒险的商业模式,在本书其余部分中你将用到,使用此模型,你会更好地理解建立和自定义模型。14 和 15 章会更进一步到自定义具有先进的建模和映射模型。
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.
第9章 在windows forms 和wpf应用程序中使用数据绑定
本章提供windows forms 和wpf应用程序中使用数据绑定的2个演示。在这些演示过程中,你会学到许多针对EF数据绑定的技巧.
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.
Preface | xxvii
Download from Library of Wow! eBook <www.wowebook.com>
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
xxviii | Preface
Download from Library of Wow! eBook <www.wowebook.com>
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 realworld
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.
Preface | xxix
Download from Library of Wow! eBook <www.wowebook.com>
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
xxx | Preface
Download from Library of Wow! eBook <www.wowebook.com>
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.