The Entity Framework is a set of technologies in ADO.NET that supports development of data-oriented software applications. The Entity Framework enables developers to work with data in the form of domain-specific objects and properties, such as customers and customer addresses, without having to concern themselves with the underlying database tables and columns where this data is stored.
实体框架是一组内建于ADO.NET的用于支持开发基于数据软件应用的技术。实体框架使得开发人员通过特定的领域对象及属性来使用数据,例如客户和客户地址,而不需要关心他们存储在底层数据库时用的表以及列的信息。
The primary benefit of the ADO.NET Entity Framework is to elevate the level of abstraction at which developers can work when they deal with data and to decrease the amount of code that is required to create and maintain data-oriented applications.
ADO.NET 实体框架的主要好处在于提高了开发人员处理数据时候的抽象级别,从而降低了创建和维护面向数据的应用程序的代码量。
Background/背景资料
Architects and developers of data-oriented applications have struggled with the need to achieve two very different objectives. They must model the entities, relationships, and logic of the business problems they are solving, and they must also work with the data engines used to store and retrieve the data. The data may span multiple storage systems, each with its own protocols; even applications that work with a single storage system must balance the requirements of the storage system against the requirements of writing efficient and maintainable application code.
软件架构师和基于数据应用程序的开发人员经常很辛苦地去实现两个非常不一致的目的。他们必须为他们要解决的业务问题的实体、关系以及逻辑进行建模。同时他们还必须使用数据引擎来存储和关系相关数据。而这些数据可能分别在多个存储系统,每个都拥有各自特有的协议;即使应用程序只使用单一的存储系统,也必须在存储系统的性能和高效编写并维护代码之间进行权衡。
In the early days of programming, applications consisted of libraries of functions for performing specific tasks. Interaction with data took the form of many discrete procedures for retrieving or modifying data. This style of programming gradually gave way to object-oriented techniques that replaced flat function libraries with more easily approachable and maintainable systems of inter-related classes that contain properties, methods, and events.
在软件设计早期,应用程序由一组完成特定功能的函数库组成。和数据库的交互时,以使用一系列存储过程的方式来获取或修改数据。这种样子的程序正逐渐地被面向对象技术所改变。我们将这些简单的函数库转变成更容易理解和维护的一组相互关联的类以及他们的属性、方法和事件。
Object-oriented programming created new challenges for interacting with data storage systems. Although the organization of classes often closely mirrors the organization of relational database tables, the fit is not perfect. Multiple normalized tables often correspond to a single class, and relationships between classes are represented differently from relationships between tables. For example, to represent the customer for a sales order, an Order class uses a property that contains a reference to an instance of a Customer class, but an Order table row in a database contains a foreign key column (or set of columns) with a value corresponding to a primary key value in the Customer table. A Customer class might have a property named Orders that contain a collection of instances of the Order class, but the Customer table in a database has no comparable column.
面向对象编程使得应用程序与数据存储系统交互出现了新的挑战。尽管类的组织形式和关系型数据表的组织形式非常接近,但是它们并不是完全一致的。一个类通常会和多个基础表进行交互,类与类之间的关系和表与表之间的关系表达的含义也不尽相同。例如,为了表示一个销售订单的相关客户,订单类使用一个属性来引用一个顾客类的实例。但是数据库里订单表的数据行则使用一个引用自顾客表主关键字值的外关键字段值(或组合外关键字)。一个顾客类可能有一个名叫Orders的属性,代表一系列 订单类的实例,但是在数据库里的顾客表则没有可以与之匹配的列。
Connecting Objects to Data/连接对象与数据
In electrical engineering, the difficulty of one system to handle inputs from another system is called an impedance mismatch, a term often applied to the difficulty of connecting object-oriented programming systems to stored data. The ADO.NET Entity Framework addresses this impedance mismatch between object-oriented programming and data storage.
在电子工程学中,当一个系统从另一个系统获取输入时出现的问题被称为阻抗失配,这个问题也经常出现在关联面向对象系统和数据存储的时候。ADO.NET 实体框架试图在面向对象的程序设计与数据存储之间解决这一问题。
Rather than supporting direct mapping of object-oriented classes and properties to relational tables and columns, the Entity Framework maps relational tables, columns, and foreign key constraints to entities and relationships in conceptual models. The Entity Framework generates extensible classes that developers can use to work with the entities and relationships in the conceptual model. Instances of these classes are populated with data and data changes are saved based on the defined mappings between the conceptual and storage models.
与直接将面向对象的类及其属性映射成关系型数据表和列不同,实体框架将数据库内的关系表、列以及外键约束映射成概念模型里的实体和关系。实体框架生成可扩展的类,使得开发人员能够将这些类和概念模型中的实体以及类一起协同工作。实际上,这些类封装了数据并根据概念模型和存储模型之间的映射定义来保存数据更改。
Giving Life to Conceptual Models/赋予概念模型新的生命力
A longstanding and common design pattern for data modeling is the division of the data model into three parts: a conceptual model, a logical model, and a physical model. The conceptual model defines the entities and relationships in the system being modeled. The logical model for a relational database normalizes the entities and relationships into tables with foreign key constraints. The physical model addresses the capabilities of a particular data engine by specifying storage details such as partitioning and indexing.
数据建模中一种常见并被长期使用的设计模式是将数据模型分为三部分:概念模型、逻辑模型和物理模型。在系统建模时,数据概念模型定义了实体及其相关的关系。关系型数据库的逻辑模型进一步规范化这些实体和关系,并形成带有主外键约束的表。物理模型则根据特定的数据引擎的情况规定特殊的存储细节,例如索引和表分割。
The physical model is refined by database administrators to improve performance, but programmers writing application code primarily confine themselves to working with the logical model by writing SQL queries and calling stored procedures. Conceptual models are generally used as a tool for capturing and communicating the requirements of an application, often as inert diagrams that are viewed and discussed in the early stages of a project and then abandoned. Many development teams skip the creation of a conceptual model and begin by specifying tables, columns, and keys in a relational database.
数据库物理模型由数据库管理员精密设计,以提高性能。而程序员写程序的时候从本质上却只能通过逻辑模型使用SQL查询或调用存储过程来进行工作。概念模型则经常只被用来作为一个获取及交互需求的工具,往往类似inert diagrams那样在项目初期被传阅并讨论然后被扔到一边。不少开发团队则干脆跳过了创建概念模型步骤,直接在关系型数据库中定义数据表、列以及各种键。
The Entity Framework gives life to conceptual models by enabling them to be programmable and connected to underlying logical and physical models. Developers who work with the Entity Framework can write code that operates on conceptual entities and relationships, relying on the Entity Framework to map those operations to storage-specific relational commands. This frees applications from hard-coded dependencies on a particular data engine or even a particular logical model. A storage-specific logical model and the mapping between it and the conceptual model are captured in an external specification that can change as needed without changing the application code. An additional benefit is that developers can work with a consistent conceptual model across multiple storage engines.
实体框架给概念模型带来了新的生命力,实体框架支持对概念模型进行设计并连接到逻辑模型以及物理模型。使用实体框架的开发人员能够编写代码来操作概念模型和关系,然后依靠实体框架型将这些操作映射到特定的相关命令。这样就可以将应用程序从一个靠硬编码关联的特定数据引擎甚至特定的逻辑模型中解放出来。一个与特定储存系统相关的逻辑模型与概念模型之间的映射可以由外部进行说明,并可以按需要进行更改而不影响应用程序代码。一个附带的好处是,开发人员可以在一个稳定的概念模型上进行工作,即使跨越多个数据引擎。
Models, Mappings, and Objects/模型、映射和对象
Using the ADO.NET Entity Framework to develop an application requires creation of a conceptual entity data model, a storage entity model, and a mapping between the two. This metadata takes the form of three types of XML files that have corresponding file name extensions:
使用ADO.NET 实体框架来开发应用程序需要创建一个概念实体数据模型,一个存储实体模型,以及两者之间的映射关系.这些元数据由以下三种特定后缀名的XML文件定义:
Conceptual schema definition language (.csdl) 概念构架定义语言
Store schema definition language (.ssdl)存储构架定义语言
Mapping specification language (.msl)映射格式规范语言
Using this metadata, the Entity Framework generates a set of classes that programmers use to interact directly with the conceptual model and indirectly with the storage model and the underlying data store. These classes are partial classes that can be extended with additional members added by the developer. The classes generated for a particular conceptual model derive from base classes that provide Object Services for materializing entities as objects and for tracking and saving changes.
通过这些元数据,实体框架生成一系列的类让编码人员能与概念模型交互,并间接地与存储模型和底层数据存储交互。这些类都是部分类,因此开发人员能对它们添加额外的成员。这些类按照特定的概念模型派生自可以将实体物化成对象的支持对象服务的基类从而可以实现跟踪和保存变更。
Visual Studio 2008 includes an evolving set of tools that help you create data applications built on an Entity Data Model (EDM). The tools include the EdmGen.exe command-line tool as well as a Visual Studio item template and a wizard that generates model mapping schemas.
Visual Studio 2008 内置了一些扩展工具来帮助你创建基于实体数据模型的数据应用程序。该工具包括了EdmGen.exe命令行工具、一个Visual Studio 项模板和一个向导,用来生模型映射构架。
Querying and Updating Data as Entities/以实体方式查询与更改数据
The classes in the System.Data.EntityClient namespace consist of a .NET Framework data provider that is storage independent. When you construct an EntityConnection object, you reference a set of metadata that contains the necessary models and mapping, and also a storage-specific data provider name and connection string. Once the EntityConnection is in place, entities can be accessed through the classes generated from the conceptual model.
System.Data.EntityClient命名空间内的类作为.NET框架的数据提供者被单独存放。当你构造一个实体连接对象( EntityConnection),你必须应用一系列包含必要的模型和映射的元数据,以及一个特定的数据提供者名称和连接字符串。一旦实体连接( EntityConnection)设置完成,就可以通过从概念模型生成类来访问实体。
The Entity Framework generates a class derived from ObjectContext that represents the entity container in the conceptual model, and this class exposes a SaveChanges method that triggers updates to the underlying database. These updates can either use SQL statements automatically generated by the system or they can use stored procedures specified by the developer.
实体框架生成一个派生自ObjectContext 的类,以在概念模型中提供了实体容器。这个类公开了一个SaveChanges的方法来引发底层数据库的更新。这些更新既可以使用由系统自动生成的SQL语句也可以使用由开发者自定义的存储过程。
Entity SQL/实体SQL
The Entity SQL language is a storage-independent dialect of SQL that works directly with conceptual entity schemas and that supports EDM features such as inheritance and relationships. When you construct EntityCommand objects, you can use Entity SQL to specify the text for queries. The Entity Framework works with storage-specific data providers to translate generic Entity SQL into storage-specific queries. Entity SQL also provides a useful alternative to Language-Integrated Query (LINQ) for some applications.
实体SQL语言是一种独立于存储系统的SQL方言。它能直接在概念模型架构上工作,并且支持EDM(实体数据模型)功能,例如继承和关系。当你创建实体命令对象( EntityCommand),你能使用实体SQL来指定查询的文本。实体框架借助于和具体的数据提供者将通用的实体SQL转换成特定的数据库查询语句。实体SQL还为另一些应用程序提供一种有用的可选方案--集成语言查询(Linq)
LINQ to Entities/实体综合语言查询
LINQ to Entities supports queries against objects representing a conceptual entity data model. Developers can use LINQ to Entities to create strongly typed, language-integrated queries against objects representing a conceptual entity data model. LINQ to Entities queries do not require that you use System.Data.EntityClient objects and Entity SQL strings in code.
实体Linq基于概念实体数据模型的对象的查询。开发者能使用实体Linq来创建强大的类型。在使用实体LINQ时,你不需要在代码中使用System.Data.EntityClient的对象以及实体SQL字符串。