先從ORM說起吧,很多年前,由於.NET的開源組件不像現在這樣發達,更別說一個開源的ORM框架,出於項目需要,以及當時OOP興起(總不至於,在項目里面全是SQL語句),就自己開始寫ORM框架。要開發ORM框架首先要了解ORM概念。
ORM 對象關系映射,O(Object) 對象,在項目中就是實體,更加精確的來說就是數據Model,也可以說持久化類。R(Relation) 關系數據,M (Mapping)映射,將對象映射到關系數據,將關系數據映射到對象的過程。
更加直觀理解就是,ORM 就是以OOP思想,產生增刪改查SQL語句
了解ORM概念之后,你會發現,其實ORM框架,主要難點在M映射部分
O 創建簡單的實體對象就可以
R 關系數據庫中數據表
M 難點要如何把實體對象與關系數據庫具體數據表關聯起來,然后產生相應數據庫操作SQL?
當時,幸好.NET 有兩樣技術比較流行
1. 特性
2. 反射
當時主要利用 特性 來標識 實體 映射 具體數據庫 TableName ,屬性 對應的 具體表的ColumnName,還有主外建,是否自增量,默認值 等等,都用特性來標識。
將實體,屬性上的特性反射后,然后根據增刪改查操作方法,就可以產生對應的SQL語句。
至此一個簡單的ORM框架,就有了。
這幾年,.NET 技術有了飛速發展,有很多寫得不錯的開源組件如雨后春筍般涌現,也包含ORM框架
Hibernate , Drapper,EntityFramework 等等
EntityFramework 版本歷史簡介
EF版本 | .net framework和IDE版本 | 主要功能 |
EF(or EF3.5) | Visual Studio 2008 SP1 (.NET 3.5 SP1) | 基本的O/R映射支持,使用DB First開發模式 |
EF 4 | Visual Studio 2010 (.NET 4.0) | 支持POCO實體 延遲加載 提高單元測試能力 自定義的代碼生成機制 支持Model First開發模式 |
EF 4.1 | NuGet | 提供簡化的DbContext接口 支持Code First開發模式 |
EF 4.1.1 | 過渡版本 | 支持Power Tools工具 |
EF 4.2 | 過渡版本 | 解決bug並優化 |
EF 4.3 | Visual Studio 2010 (.NET 4.0) | 基於Code First開發模式的代碼遷移策略 Migrations |
EF 4.3.1 | Visual Studio 2012 (.NET 4.5) | 提供對 LocalDb 數據庫的支持 |
EF 5 | Visual Studio 2012 (.NET 4.5) | 提供對枚舉類型的支持 Table-Valued functions表值函數 空間數據類型(spatial types) 整體性能優化提升 實體模型設計器、多圖模型 批量導入存儲過程 |
EF 6 | Visual Studio 2013 (.NET 4.5) | Power Tools加強 EF脫離Visual Studio和.NET通過NuGet單獨發布 可配置不穩定連接的重試次數等 支持.NET 4.5中基於Task的異步編程模式 Async Query and Save 優化的配置選項 Code-Based Configuration 支持依賴注入和服務定位 Dependency Resolution 低級的EF攔截器及SQL日志 Interception/SQL Loggin 使用Mock單元測試 using a mocking framework or writing your own test doubles 使用已有的DbConnection創建DbContext 提升事務支持 Improved Transaction Support 優化LINQ to Entities查詢性能 優化(View Generation)性能 支持自定義實現Entity的Equals和GetHashCode方法 DBSet.AddRange/RemoveRange DbChangeTracker.HasChanges 擴展的SqlFunctions,SqlCeFunctions |
EF 6.0.1 | Visual Studio 2013 (.NET 4.5) | fix some performance issues during warm-up for EF models. |
EF 6.0.2 | Visual Studio 2013 (.NET 4.5) | The tooling for Visual Studio 2012 and Visual Studio 2013 is available on the Microsoft Download Center. You only need to install the tooling if you want to use Model First or Database First |
EF 6.1 | Visual Studio 2013 (.NET 4.5) | Tooling consolidation provides a consistent way to create a new EF model. This feature extends the ADO.NET Entity Data Model wizard to support creating Code First models, including reverse engineering from an existing database. These features were previously available in Beta quality in the EF Power Tools. Handling of transaction commit failures provides theCommitFailureHandler which makes use of the newly introduced ability to intercept transaction operations. The CommitFailureHandler allows automatic recovery from connection failures whilst committing a transaction IndexAttributeallows indexes to be specified by placing an [Index] attribute on a property (or properties) in your Code First model. Code First will then create a corresponding index in the database The public mapping API provides access to the information EF has on how properties and types are mapped to columns and tables in the database. In past releases this API was internal |
EntityFramework 剛剛出來時,反響就比較不錯。那時我有接觸一些,不多,也不知道當時為什么這么好的東西,沒有深耕一下。
2014 年,去了一家創業公司,做技術主管,做架構開發,當時定的 ASP.NET MVC 5&Entity Framework 6做開發框架。
再次了解到EntityFramework的強大。
Entity Framework 有三種實作方式
1. DataBase First 數據庫先行
2. Model First 模型先行
3. Code First 代碼先行
前兩種就是拖控件,按照指引一步步,就可以完成Entity Framework 實現。
Code First 就是代碼純手工打造。
Entity Framework 介紹暫時介紹完成,我們這個系列實作Entity Framework 方式是Code First,因為Code First 才能更加深入了解Entity Framework 工作原理。
我們這個系列采用是 Entity Framework 6.1 版本,最后這幾個版本區別不大。
CodePlex : http://entityframework.codeplex.com/
GitHub : https://github.com/aspnet/EntityFramework/
開發指南:https://docs.efproject.net/en/latest/
敬請期待
第二篇:Entity Framework CodeFirst & Model 映射