Entity Framework 總覽Entity Framework overview

Entity Framework 是 ADO.NET 中的一組技術,可支援資料導向軟體應用程式的開發。The Entity Framework is a set of technologies in ADO.NET that support the development of data-oriented software applications. 資料導向應用程式的架構設計人員和開發人員經常致力於滿足達成兩個迥異目標的需求。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. 這項資料可能會跨越多個儲存系統,而且每個系統都有自己的通訊協定 (Protocol)。即使是使用單一儲存系統的應用程式都必須在儲存系統的需求與撰寫有效率和可維護應用程式程式碼的需求之間取得平衡。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.

此 Entity Framework 可讓開發人員以網域特定物件和屬性(例如客戶和客戶位址)的形式來使用資料,而不需要顧慮其本身的基礎資料庫資料表和資料行。儲存.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. 有了 Entity Framework,開發人員在處理資料時可以在較高的抽象層級工作,而且能夠使用比傳統應用程式更少的程式碼來建立及維護資料導向應用程式。With the Entity Framework, developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code than in traditional applications. 由於 Entity Framework 是 .NET Framework 的元件,因此 Entity Framework 應用程式可以在已安裝 3.5 SP1 開始之 .NET Framework 的任何電腦上執行。Because the Entity Framework is a component of the .NET Framework, Entity Framework applications can run on any computer on which the .NET Framework starting with version 3.5 SP1 is installed.

將生命週期提供給模型Give life to models

傳統且常見的建置應用程式或服務之設計方式是將應用程式或服務分成三個部分:領域模型、邏輯模型和實體模型。A longstanding and common design approach when building an application or service is the division of the application or service into three parts: a domain model, a logical model, and a physical model. 網域模型會在建立模型的系統中定義實體和關聯性。The domain model defines the entities and relationships in the system that is 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.

雖然資料庫管理員會調整實體模型來改善效能,不過撰寫應用程式程式碼的程式設計人員主要會透過撰寫 SQL 查詢和呼叫預存程序 (Stored Procedure),限制他們自己使用邏輯模型。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. 網域模型通常會當做擷取和傳達應用程式需求的工具使用,而經常當做在專案早期階段中檢視和討論,然後放棄的惰性圖表使用。Domain models are generally used as a tool for capturing and communicating the requirements of an application, frequently as inert diagrams that are viewed and discussed in the early stages of a project and then abandoned. 許多開發小組會略過建立概念模型,而從指定關聯式資料庫中的資料表、資料行和索引鍵開始。Many development teams skip creating a conceptual model and begin by specifying tables, columns, and keys in a relational database.

Entity Framework 讓開發人員能夠查詢領域模型中的實體和關聯性(稱為 Entity Framework 中的概念模型),同時依賴 Entity Framework 將這些作業轉譯為數據源特定的命令,藉此提供模型的生命週期。The Entity Framework gives life to models by enabling developers to query entities and relationships in the domain model (called a conceptual model in the Entity Framework) while relying on the Entity Framework to translate those operations to data source–specific commands. 如此,應用程式便不再受限於特定資料來源的硬式編碼相依性。This frees applications from hard-coded dependencies on a particular data source.

使用 Code First 時,會將概念模型對應至程式碼中的儲存模型。When working with Code First, the conceptual model is mapped to the storage model in code. Entity Framework 可以根據物件類型和您所定義的其他設定來推斷概念模型。The Entity Framework can infer the conceptual model based on the object types and additional configurations that you define. 對應中繼資料會在執行階段產生,並以您定義網域類型的方式與您在程式碼中提供的其他組態資訊為基礎。The mapping metadata is generated during run time based on a combination of how you defined your domain types and additional configuration information that you provide in code. Entity Framework 視中繼資料而定,視需要產生資料庫。Entity Framework generates the database as needed based on the metadata. 如需詳細資訊,請參閱建立模型For more information, see Creating a Model.

使用實體資料模型工具時,概念模型、儲存體模型和兩者之間的對應會以 XML 架構的結構描述來表示,並定義於具有對應副檔名的檔案中。When working with the Entity Data Model Tools, the conceptual model, the storage model, and the mappings between the two are expressed in XML-based schemas and defined in files that have corresponding name extensions:

  • 概念結構定義語言 (CSDL) 會定義概念模型。Conceptual schema definition language (CSDL) defines the conceptual model. CSDL 是實體資料模型的 Entity Framework 執行。CSDL is the Entity Framework's implementation of the Entity Data Model. 副檔名是 .csdl。The file extension is .csdl.

  • 存放結構定義語言 (SSDL) 會定義儲存體模型,也稱為邏輯模型。Store schema definition language (SSDL) defines the storage model, which is also called the logical model. 副檔名是 .ssdl。The file extension is .ssdl.

  • 對應規格語言檔案 (MSL) 會定義儲存體模型和概念模型之間的對應。Mapping specification language (MSL) defines the mappings between the storage and conceptual models. 副檔名是 .msl。The file extension is .msl.

儲存體模型和對應可依需要變更,不需要變更概念模型、資料類別或應用程式程式碼。The storage model and mappings can change as needed without requiring changes to the conceptual model, data classes, or application code. 由於儲存模型是提供者特有的,所以您可以在各種資料來源中使用一致的概念模型。Because storage models are provider-specific, you can work with a consistent conceptual model across various data sources.

Entity Framework 會使用這些模型和對應檔,針對概念模型中的實體和關聯性,建立、讀取、更新和刪除作業,以對資料來源中的對等作業進行。The Entity Framework uses these model and mapping files to create, read, update, and delete operations against entities and relationships in the conceptual model to equivalent operations in the data source. Entity Framework 甚至支援將概念模型中的實體對應至資料來源中的預存程式。The Entity Framework even supports mapping entities in the conceptual model to stored procedures in the data source. 如需詳細資訊,請參閱CSDL、SSDL 和 MSL 規格For more information, see CSDL, SSDL, and MSL Specifications.

將物件對應至資料Map objects to data

物件導向程式設計會提出與資料儲存系統互動的挑戰。Object-oriented programming poses a challenge for interacting with data storage systems. 雖然類別的組織經常會與關聯式資料庫資料表的組織鏡像,但是這個符合並不完美。Although the organization of classes frequently mirrors the organization of relational database tables, the fit is not perfect. 多個正規化資料表經常會對應到單一類別,而且經常會使用與資料表之間的關聯性不同的方式來表示類別之間的關聯性。Multiple normalized tables frequently correspond to a single class, and relationships between classes are often represented differently than relationships between tables are represented. 例如,若要表示銷售訂單的客戶,Order 類別可能會使用包含 Customer 類別執行個體之參考的屬性,但是資料庫中的 Order 資料表資料列包含外部索引鍵資料行 (或資料行組),而且其值對應至 Customer 資料表中的主索引鍵值。For example, to represent the customer for a sales order, an Order class might use a property that contains a reference to an instance of a Customer class, while an Order table row in a database contains a foreign key column (or set of columns) with a value that corresponds to a primary key value in the Customer table. Customer 類別可能具有名為 Orders 的屬性,其中包含 Order 類別執行個體的集合,但是資料庫中的 Customer 資料表沒有任何可比較的資料行。A Customer class might have a property named Orders that contains a collection of instances of the Order class, while the Customer table in a database has no comparable column. Entity Framework 為開發人員提供以這種方式表示關聯性的彈性,或在資料庫中呈現更緊密的模型關聯性。The Entity Framework provides developers with the flexibility to represent relationships in this way, or to more closely model relationships as they are represented in the database.

現有的方案已經嘗試只將物件導向的類別和屬性對應至關聯式資料表和資料行,藉以填補這個間距 (經常稱為「阻抗不相符」)。Existing solutions have tried to bridge this gap, which is frequently called an "impedance mismatch", by only mapping object-oriented classes and properties to relational tables and columns. Entity Framework 將邏輯模型中的關聯式資料表、資料行和外鍵條件約束對應至概念模型中的實體和關聯性,而不是採用這種傳統方法。Instead of taking this traditional approach, the Entity Framework maps relational tables, columns, and foreign key constraints in logical models to entities and relationships in conceptual models. 這樣做可以在定義物件和最佳化邏輯模型方面提供更大的彈性。This enables greater flexibility both in defining objects and optimizing the logical model. 實體資料模型工具會根據概念模型產生可擴充的資料類別。The Entity Data Model tools generate extensible data classes based on the conceptual model. 這些類別是可以使用開發人員所加入的其他成員所擴充的部分類別。These classes are partial classes that can be extended with additional members that the developer adds. 根據預設,針對特定概念模型所產生的類別會衍生自提供服務來將實體具體化為物件以及追蹤和儲存變更的基底類別 (Base Class)。By default, the classes that are generated for a particular conceptual model derive from base classes that provide services for materializing entities as objects and for tracking and saving changes. 開發人員可以使用這些類別,將實體和關聯性當做經由關聯而相關的物件進行處理。Developers can use these classes to work with the entities and relationships as objects related by associations. 開發人員也可以自訂為概念模型產生的類別。Developers can also customize the classes that are generated for a conceptual model. 如需詳細資訊,請參閱使用物件For more information, see Working with Objects.

存取和變更實體資料Access and change entity data

Entity Framework 不只是另一個物件關聯式對應方案而已,它基本上是有關如何讓應用程式存取和變更表示成概念模型中之實體和關聯性的資料。More than just another object-relational mapping solution, the Entity Framework is fundamentally about enabling applications to access and change data that is represented as entities and relationships in the conceptual model. Entity Framework 會使用模型和對應檔中的資訊,將針對概念模型中表示之實體類型的物件查詢轉譯成資料來源特有的查詢。The Entity Framework uses information in the model and mapping files to translate object queries against entity types represented in the conceptual model into data source-specific queries. 查詢結果會具體化為 Entity Framework 所管理的物件。Query results are materialized into objects that the Entity Framework manages. Entity Framework 提供下列方法來查詢概念模型和傳回物件:The Entity Framework provides the following ways to query a conceptual model and return objects:

  • LINQ to Entities。LINQ to Entities. 針對查詢在概念模型中定義的實體類型,提供了語言整合式查詢(LINQ)支援。Provides Language-Integrated Query (LINQ) support for querying entity types that are defined in a conceptual model. 如需詳細資訊,請參閱LINQ to EntitiesFor more information, see LINQ to Entities.

  • Entity SQLEntity SQL第 1 課:建立 Windows Azure 儲存體物件{2}。. 與儲存體無關的 SQL 方言,可直接與概念模型中的實體搭配使用,並支援實體資料模型的概念。A storage-independent dialect of SQL that works directly with entities in the conceptual model and that supports Entity Data Model concepts. Entity SQLEntity SQL 用於物件查詢,以及使用 EntityClient 提供者執行的查詢。is used both with object queries and queries that are executed by using the EntityClient provider. 如需詳細資訊,請參閱Entity SQL 總覽For more information, see Entity SQL Overview.

Entity Framework 包含 EntityClient 資料提供者。The Entity Framework includes the EntityClient data provider. 這個提供者會管理連接、將實體查詢轉譯成資料來源特有的查詢,並傳回 Entity Framework 用來將實體資料具體化為物件的資料讀取器。This provider manages connections, translates entity queries into data source-specific queries, and returns a data reader that the Entity Framework uses to materialize entity data into objects. 不需要物件具體化時,EntityClient 提供者也可以當做標準 ADO.NET 資料提供者使用,方法是讓應用程式執行 Entity SQLEntity SQL 查詢並且取用傳回的唯讀資料讀取器。When object materialization is not required, the EntityClient provider can also be used like a standard ADO.NET data provider by enabling applications to execute Entity SQLEntity SQL queries and consume the returned read-only data reader. 如需詳細資訊,請參閱 適用於 Entity Framework 的 EntityClient 提供者For more information, see EntityClient Provider for the Entity Framework.

下圖說明用於處理資料的 Entity Framework 架構:The following diagram illustrates the Entity Framework architecture for accessing data:

Entity Framework 架構圖Entity Framework Architectural Diagram

實體資料模型工具可以產生衍生自 System.Data.Objects.ObjectContext 的類別,或代表概念模型中之實體容器的 System.Data.Entity.DbContextThe Entity Data Model Tools can generate a class derived from System.Data.Objects.ObjectContext or System.Data.Entity.DbContext that represents the entity container in the conceptual model. 這個物件內容會提供追蹤變更以及管理識別 (Identity)、並行和關聯性的機能。This object context provides the facilities for tracking changes and managing identities, concurrency, and relationships. 這個類別也會公開 (Expose) 可針對資料來源進行寫入、插入、更新和刪除作業的 SaveChanges 方法。This class also exposes a SaveChanges method that writes inserts, updates, and deletes to the data source. 就像查詢一樣,這些變更是由系統自動產生的命令所進行,或由開發人員指定的預存程序所進行。Like queries, these changes are either made by commands automatically generated by the system or by stored procedures that are specified by the developer.

資料提供者Data providers

EntityClient 提供者會藉由存取概念實體和關聯性方面的資料,來擴充 ADO.NET 提供者模型。The EntityClient provider extends the ADO.NET provider model by accessing data in terms of conceptual entities and relationships. 它將執行使用 Entity SQLEntity SQL 的查詢。It executes queries that use Entity SQLEntity SQL. Entity SQLEntity SQL 提供了可讓 EntityClient 與資料庫通訊的基礎查詢語言。provides the underlying query language that enables EntityClient to communicate with the database. 如需詳細資訊,請參閱 適用於 Entity Framework 的 EntityClient 提供者For more information, see EntityClient Provider for the Entity Framework.

Entity Framework 包含支援標準命令樹的更新 SqlClient Data Provider。The Entity Framework includes an updated SqlClient Data Provider that supports canonical command trees. 如需詳細資訊,請參閱SqlClient for the Entity FrameworkFor more information, see SqlClient for the Entity Framework.

實體資料模型工具Entity data model tools

與 Entity Framework 執行時間一起,Visual Studio 包含對應和模型工具。Together with the Entity Framework runtime, Visual Studio includes the mapping and modeling tools. 如需詳細資訊,請參閱模型化和對應For more information, see Modeling and Mapping.

深入了解Learn more

若要深入瞭解 Entity Framework,請參閱:To learn more about the Entity Framework, see:

消費者入門-提供如何使用快速入門快速啟動並執行的相關資訊,其中顯示如何建立簡單的 Entity Framework 應用程式。Getting Started - Provides information about how to get up and running quickly using the Quickstart, which shows how to create a simple Entity Framework application.

Entity Framework 的術語-定義實體資料模型和 Entity Framework 引進的許多詞彙,並用於 Entity Framework 檔。Entity Framework Terminology - Defines many of the terms that are introduced by the Entity Data Model and the Entity Framework and that are used in Entity Framework documentation.

Entity Framework 資源-提供概念主題的連結,以及外部主題和資源的連結,以建立 Entity Framework 的應用程式。Entity Framework Resources - Provides links to conceptual topics and links to external topics and resources for building Entity Framework applications.

另請參閱See also