EF產生的背景:
編寫ADO.NET訪問數據的代碼,是沉悶而枯燥的,所以微軟提供了一個對象關系映射框架(我們稱之為EF),通過EF可以自動幫助我們的程序自動生成相關數據庫。
Writing and managing ADO.Net code for data access is a tedious and monotonous job. Microsoft has provided an O/RM framework called "Entity Framework" to automate database related activities for your application.
EF的定義:
EF是一個對象關系映射【ORM】的框架,它保證程序員把相關的數據作為特別的領域對象,減少了程序員編寫大量的訪問數據庫的代碼。開發人員,使用LINQ來查詢,檢索,操作數據為強類型對象來處理。EF框架提供了更改跟蹤,身份識別,懶加載,和查詢轉換,所以開發人員可以專注于他們的程序中的邏輯代碼,而不是數據訪問基礎。
The Microsoft ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with relational data as domain-specific objects, eliminating the need for most of the data access plumbing code that developers usually need to write. Using the Entity Framework, developers issue queries using LINQ, then retrieve and manipulate data as strongly typed objects. The Entity Framework's ORM implementation provides services like change tracking, identity resolution, lazy loading, and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals.
EF的三個應用場景:
Entity framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database.
Entity framework is useful in three scenarios. First, if you already have existing database or you want to design your database ahead of other parts of the application. Second, you want to focus on your domain classes and then create the database from your domain classes. Third, you want to design your database schema on the visual designer and then create the database and classes.
The following figure illustrates the above scenarios.
1.根據已經存在的數據庫,生成相關類;
2.根據相關領域類,生成數據庫;
3.在可視化的設計器中,來創建類和數據庫;
As per the above figure, EF creates data access classes for your existing database, so that you can use these classes to interact with the database instead of ADO.Net directly.
EF can also create the database from your domain classes, thus you can focus on your domain-driven design.
EF provides you a model designer where you can design your DB model and then EF creates database and classes based on your DB model.
什么是O/RM?
ORM is a tool for storing data from domain objects to relational database like MS SQL Server, in an automated way, without much programming. O/RM includes three main parts: Domain class objects, Relational database objects and Mapping information on how domain objects map to relational database objects (tables, views & storedprocedures). ORM allows us to keep our database design separate from our domain class design. This makes the application maintainable and extendable. It also automates standard CRUD operation (Create, Read, Update & Delete) so that the developer doesn't need to write it manually.
A typical ORM tool generates classes for the database interaction for your application as shown below.
ORM是一個工具,它可以自動的存儲數據庫(比如MS SQL Server)中領域對象中的數據,而不需要很多編程。
O/RM包含3個主要的部分:
1.領域類對象
2.關系數據庫對象
3.映射信息(領域對象,怎樣映射到數據庫對象【表,視圖,存儲過程】?)
ORM允許我們把數據庫設計和領域類設計分開來,這樣保證了程序的可維護性和可擴展性,ORM同樣自動幫助我們生成了增刪查改(CURD)操作,所以我們不必手動去寫了。
Tips:要了解更多的ORM技術,可以訪問 這個鏈接: Object-relational Mapping
Visit wikipedia for more information on Object-relational Mapping
There are many ORM frameworks for .net in the market such as DataObjects.Net, NHibernate, OpenAccess, SubSonic etc. Entity Framework is an open source ORM framework from Microsoft.
Please note that Entity Framework is an open source framework by Microsoft. You can contribute to the Entity Framework project oncodeplex.
ORM框架分類:
-
DataObjects.Net
-
NHibernate
-
OpenAccess
-
SubSonic
-
請注意:EF是微軟的開源ORM框架,所謂開源就是你可以參與開發,有興趣的可以去這個網站看看:codeplex.
好了,EF的介紹到此結束了。后面繼續更新,覺得不錯的,園友可以點贊,你的支持是我前進的動力!
文章列表