ADO.NET Entity Framework 以 Entity Data Model (EDM) 为主,将数据逻辑层切分为三块,分别为 Conceptual Schema, Mapping Schema 与 Storage Schema 三层,其上还有 Entity Client,Object Context 以及 LINQ 可以使用。
下面这张图很好地说明了 EF 的基本构成体系,表明 EF 是建立在关系数据库上的一种抽象概念模型(Conceptual Model)框架。
相关名词说明:
- Data Source: 我们所熟知的数据库或其他可能的数据源,SQL Server、Oracle、DB2、MySQL 等等。
- Data Providers: ADO.NET Data Provider,貌似目前很多数据库厂商都提供了 EF 所需的 Data Provider。
- Entity Data Model (EDM): EF 用 CSDL、SSDL、MSL 三种专用语言来描述概念架构、存储架构和映射关系。
- Entity Client: 一组低阶数据访问 API,允许我们用类似 ADO.NET DbCommand 方式进行操作。
- Object Services: 提供 Entity Object 操作所需的各类功能,包括状态跟踪和缓存等等。
- Entity SQL (ESQL): 一种类似 T-SQL 且面向对象的查询语言,地位类似 Hibernate HQL,大大增强了复杂查询能力。
- LINQ to Entities: 如果你熟悉 LINQ to SQL,自然很容易上手这个。
1. Entity Data Model (EDM)
EF 专门定义三种基于 XML 的描述语言来规范 EDM 的定义,这么做的好处是具备更好的扩展和适应变化的能力。试想一下,当数据库字段发生变化时,我们只需修改 SSDL 的定义,而不用再重新编译程序。当然,我们还可以在运行时动态载入 SSDL 定义。
- Conceptual Schema Definition Language (CSDL): Conceptual layer、Conceptual schema、Conceptual model、C-side。
- Store Schema Definition Language (SSDL): Store/storage layer、Store/storage schema、Store/storage model、Store/storage metadata、Store/storage metadata schema、S-side。
- Mapping Specification Language (MSL): Mapping layer、Mapping specification、C-S side (referring to "conceptual to store")。
默认情况下,EF 会将 CSDL、SSDL、MSL 编译嵌入到目标程序集的资源中。我们可以在 EDMX 设计视图的属性窗口中进行修改。
Model1.ssdl
<?xml version="1.0" encoding="utf-8"?> <Schema Namespace="TestModel.Store" Alias="Self" Provider="System.Data.SqlClient" ...> <EntityContainer Name="TestModelStoreContainer"> <EntitySet Name="User" EntityType="TestModel.Store.User" store:Type="Tables" Schema="dbo" /> <AssociationSet Name="FK_Order_User" Association="TestModel.Store.FK_Order_User"> <End Role="User" EntitySet="User" /> </AssociationSet> </EntityContainer> <EntityType Name="User"> <Key> <PropertyRef Name="Id" /> </Key> <Property Name="Id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" /> <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="50" /> <Property Name="Age" Type="int" /> </EntityType> </Schema>
Model1.csdl
<?xml version="1.0" encoding="utf-8"?> <Schema Namespace="TestModel" Alias="Self" ...> <EntityContainer Name="TestEntities"> <EntitySet Name="User" EntityType="TestModel.User" /> </EntityContainer> <EntityType Name="User"> <Key> <PropertyRef Name="Id" /> </Key> <Property Name="Id" Type="Int32" Nullable="false" /> <Property Name="Name" Type="String" Nullable="false" MaxLength="50" ... /> <Property Name="Age" Type="Int32" /> </EntityType> </Schema>
Model1.msl
<?xml version="1.0" encoding="utf-8"?> <Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS"> <EntityContainerMapping StorageEntityContainer="TestModelStoreContainer" CdmEntityContainer="TestEntities"> <EntitySetMapping Name="User"> <EntityTypeMapping TypeName="IsTypeOf(TestModel.User)"> <MappingFragment StoreEntitySet="User"> <ScalarProperty Name="Id" ColumnName="Id" /> <ScalarProperty Name="Name" ColumnName="Name" /> <ScalarProperty Name="Age" ColumnName="Age" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> </EntityContainerMapping> </Mapping>
很好理解。
2. Data Providers
ADO.NET Team Blog 2009.01.26 列出的 Providers List。
-------------------
ADO.NET 3.5 Data Providers with Support for the Entity Framework
The ADO.NET Entity Framework simplifies data access code in applications by enabling developers to create a customizable conceptual model for data from any source and easily map it to business requirements.
The ADO.NET Entity Framework is database independent and built upon a standard ADO.NET Provider model that allows access to third-party databases through the use of ADO.NET Data Providers. These third-party providers are currently available.
Devart
Devart's offers ADO.NET Data Providers supporting access to Oracle, MySQL, and PostgreSQL databases via the .NET Framework 3.5 Service Pack 1 and Visual Studio 2008 Service Pack 1, including the ADO.NET Entity Framework.
Phoenix Software Solutions
Phoenix Software Solutions offers an ADO.NET Data Provider supporting access to SQLite via the .NET Framework 3.5 Service Pack 1 and Visual Studio 2008 Service Pack 1, including support for the ADO.NET Entity Framework.
Npgsql
Npgsql is an ADO.NET provider for PostgreSQL. It is built with 100% managed code and provides .NET applications with the latest ADO.NET features for accessing a PostgreSQL database.
OpenLink Software
OpenLink's Virtuoso ADO.NET Entity Framework provider allows developers to access native Virtuoso data (SQL, XML, and RDF) and any Virtuoso Linked Tables from external ODBC and JDBC accessible data sources using LINQ, Entity SQL and ADO.NET Data Services. Known-compatible external data sources include Oracle (versions 7.x to 11.x), Microsoft SQL Server (6.x to 2005), IBM DB2 , Sybase (4.2 to 12.x+), IBM Informix (5.x to 11.x), Ingres (6.4 to 9.x), Progress (7.x to 10.x), MySQL, PostgreSQL, and Firebird.
Sybase SQL Anywhere
The release of SQL Anywhere 11 includes Sybase iAnywhere's ADO.NET Data Provider that includes Entity Framework support, allowing Sybase developers to access SQL Anywhere databases via LINQ, Entity SQL and ADO.NET Data Services.
IBM
IBM's Data Server Provider for .NET allows developers to access DB2, Informix and U2 databases via LINQ, Entity SQL and ADO.NET Data Services.
Firebird
Written in C#, the .NET Data Provider software is an official sub-project of Firebird, developing and maintaining a high-performance native implementation of the API functions exposed as Firebird's client layer. This enables .NET applications to access Firebird databases without installing the Firebird client library.
资料来源:
http://blogs.msdn.com/adonet/
http://www.microsoft.com/sqlserver/2008/en/us/ado-net-entity.aspx
http://www.rainsts.net/article.asp?id=789