ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

46
1 ADO.NET Entity Framework 技技技技技技技 奚奚奚 奚奚 奚奚奚奚奚 奚奚奚奚 //

Transcript of ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

Page 1: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

ADO.NET Entity Framework技術指引與應用

奚江華作家/微軟講師/技術顧問

Page 2: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

2

AgendaOOP vs. Relational databaseEntity Data Model( EDM)ADO.NET Entity Framework架構Entity Framework程式設計

Page 3: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

3

OOP vs. Relational Database

Discusses窺探 Entity Framework架構OOP vs. Relational DatabaseObjects != Relational Data

Page 4: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

4

Entity Framework資料存取架構圖

VS 2008 SP1 EDM工具

程式設計

Page 5: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

5

OOP v.s. Relational Database

Object Oriented Programming has been around for decadesRelational databases have been around for even longerBridging the gap between these two has been a mostly manual task

Modulo 3rd party solutionsThe core problem remains

Page 6: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

6

Objects != Relational Data

The fundamental problem is that relational data and objects in a programming language aren’t the same!

They have different semantics, languages, etcBoth are needed – how many applications don’t use a relational database, or have some type of data model?Object / Relational Mapping

Page 7: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

7

OOP to Data Engine DisconnectRelational Model

Page 8: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

8

OOP to Data Engine DisconnectObject Model

Page 9: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

9

Entity Data Model( EDM)

DiscussesE-R Model & Entity Data ModelEDM設計目標EDM主要成員一體通用的 Data Model資料模型

Page 10: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

10

什麼是 Entity Data Model?

A abstract conceptual model of dataBased on Entity-Relationship Model by Dr. Peter Chen (1976)Entities and Relationships between those Entities

Entity Types Association TypesNavigation Properties

The EDM is able to bring relational data and OOP together

Page 11: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

11

EDM設計目標EDM可以讓 Domain Model與組織使思考與使用資料達成一致性。EDM也是微軟對於橫跨開發人員/伺服器技術之核心資料模型的主要目標。EDM Models可應用於

ADO.NET Entity FrameworkADO.NET Data Services FrameworkADO.NET Dynamic Data FrameworkInput to ReportingVisualization applicationsIntranet portal applicationsworkflow applications

Page 12: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

12

Sample Entity Data Model for a Blogging DatabaseApplication-level Domain

Page 13: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

13

Entity Framework And EDM

How does it bridge the gap?A model that works for bothCommon type systemInheritanceComplex types

EDM is not focused on application design as a whole, but on the design of your data

Page 14: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

14

Entities As Objects

Like an objectKnown typeDistinct identityProperties hold scalar valuesProperties hold references to other entities

Unlike an objectEntity identity (A.K.A. primary key)Associations to other entitiesLives within a collection

Page 15: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

15

Entities As Relational Data

Like relational dataPrimary keyNo behavior definedRelationships (associations) to other entitiesLives within an entity set

Unlike relational dataNo physical storage knowledge (tables, indexes, etc)Supports inheritanceComplex types

Page 16: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

16

What Does This Mean?

Every layer in an application has unique requirements of the data, but they all use data in terms of EntitiesThe EDM is the Lingua Franca of the stack – it bridges the gap between OOP and relational dataYour data model is no longer tightly bound to your database or your application

Page 17: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

17

一體通用的 Data Model資料模型

Where you create a single model…Can be stored in your databaseThat represents your business, but can be specializedThat is used in your Web sites, Rich Client Applications, Mid tier solutions, etcThat can be exposed via Web servicesThat you can base your reports onThat can be used as the model for your data cubes

Page 18: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

18

Entity Framework架構

DiscussesEntity Framework介紹Entity Data Model工具建立 Entity Data ModelEDM主要定義檔成員

Page 19: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

19

ADO.NET Entity Framework介紹目的

EF能夠讓開發人員以 Domain-Specific物件及屬性來處理資料,例如顧客、顧客地址等等不必擔心底層儲存的 Database、 Tables及Column讓開發人員在進行資料處理時可以提升抽象層次減少建立及維護以資料為中心 AP所需的程式碼實作

EF負責 Conceptual Model / Storage Model Mapping開發人員只需與 Conceptual Model物件互動EF 負責轉換 Query與 Database之查詢作業

Page 20: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

20

ADO.NET Entity Framework Features

EntitiesProperties – primitive types or complex typesNavigation property – represents relationship between entitiesMethods – implemented with partial classes

LINQ to EntitiesTranslates object queries to data store queryAlternate method based query syntax

Page 21: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

21

ADO.NET Entity Framework Benefits

Developers work with business objectsNo requirement of relational structure knowledgeRelational structure changes don’t mean recodeNavigation of relationships by propertiesDoes not require writing SQL DML queriesInteraction with data store managed by frameworkIntelliSense

PerformanceDeferred loading of data when needed

Page 22: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

22

The Entity Framework

ADO.NET Entity Framework是第一個實作 EDM的產品EF是一個應用程式開發的 framework

EDM Modeling toolsMapping EDM model to/from DB and CLRObject Services

Page 23: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

23

Entity Data Model工具Entity Data Model WizardEntity Data Model DesignerUpdate Model Wizard

Page 24: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

24

建立 Entity Data Model

Page 25: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

25

以 EDM Wizard建立EDM

demoApplication-LevelDomain

Page 26: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

26

EDM主要定義檔成員Conceptual Model定義 Entities及 RelationshipsConceptual schema definition language file (.csdl)

Storage Model( Logical Model)目的是為了關聯式資料庫,將 Entities及Relationships正規化成具有 foreign key constrains的 Table資料表Store schema definition language file (.ssdl)

Mappings定義 Storage 及 Conceptual Model二者間的對應Mapping specification language file (.msl)

Page 27: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

27

檢視 EDM定義檔

demo.csdl.ssdl.msl

Page 28: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

28

Entity Framework程式設計

DiscussesEntity Framework細部架構圖Object Services服務ObjectContext & ObjectQueryO bject Query查詢的三種方法

Page 29: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

29

Entity Framework細部架構圖

Page 30: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

30

Object Services物件服務它是 EF的一個元件System.Data.Entity.dll在 System.Data.Objects & System.Data.Objects.DataClass兩大命名空間實作兩大命名空間中包含一群服務類別對 EDM中 Entity Types作業之中介服務Query、 Insert、 Update、 Delete

Page 31: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

31

System.Data.Objects類別CompiledQueryCurrentValueRecordObjectContextObjectParameterObjectParameterCollectionObjectQueryObjectQuery<T>ObjectResult<T>ObjectStateEntryObjectStateManager

MergeOption列舉RefreshMode列舉

Page 32: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

32

System.Data.Objects.DataClass類別介面列舉

ComplexObjectEdmComplexPropertyAttributeEdmComplexTypeAttributeEdmEntityTypeAttributeEdmPropertyAttributeEdmRelationshipAttributeEdmRelationshipNavigationPropertyAttributeEdmScalarPropertyAttributeEdmSchemaAttributeEdmTypeAttributeEntityCollection<TEntity>EntityObjectEntityReferenceEntityReference<TEntity>RelatedEndRelationshipManagerStructuralObject

Page 33: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

33

Object Services物件服務功能Querying data as objects Shaping query results Composing queries using builder methodsAdding, changing, and deleting objects Saving changes to the data source Binding objects to controls Attaching objects / Detaching objects Serializing objects

Page 34: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

34

Object Services物件服務功能 .續

Managing object identities and tracking changes Managing concurrency Managing connections Using custom objects with an Entity Data Model

Page 35: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

35

ObjectContext類別EDM中 Entities Type的 Instance對於 Entity資料提供 Object型式互動的能力ObjectContext類別中包含了:

Connection to databaseMetadataWorkspace物件ObjectStateManager物件

Page 36: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

36

ObjectQuery類別ObjectQuery泛型類別代表了回傳零或多個強型別 Entity Objects的查詢。一個 Object Query永遠屬於一個既存的Object Context,意即在使用ObjectQuery之前,一定要先建立一個Object Context。因為 Object Context提供了建構與執行查詢所需的 Connection連線及Metadata資訊。

Page 37: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

37

Object Query之執行預設為遞延查詢( Defered Execution)立即執行( Immediate Execution)foreach (C#) or For Eachfill a List collectionExecute methodLINQ query operator- First or Any

Page 38: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

38

Programming The Entity Framework

Page 39: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

39

三種 Object Query查詢語法類型

LINQ to Entity( Language-Integrated Query )Entity SQLObject Query Builder Methods

Page 40: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

40

以 LINQ To Entities進行查詢Queries against the generated classesAll the benefits of LINQ

Compile time type checkingIntellisenseEtc

Query results are materialized objectsEntity TypesAnonymous TypesScalar values

Page 41: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

41

LINQ to Entity運作流程

Step 1:建立 ObjectContext

Step 2:建立 ObjectQuery & LINQ Query

Step 3: LINQ Query Command Tree(EDM)

Step 4: EntityClient Command

Tree(DBMS)

Step 5:將結果以物件型式回傳

Page 42: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

42

以 LINQ to Entity查詢

demo以 Products資料表為例

Page 43: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

43

以 Entity SQL進行查詢SQL variant for working with Entity Data Model

Understands inheritance, associations, etcString based, but not dynamic SQLResults can be either objects, or EntityDataReader

Allows for higher performance when you don’t need objects

Page 44: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

44

EF的未來

Page 45: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

www.microsoft.com/teched Tech·Talks Tech·Ed BloggersLive Simulcasts Virtual Labs

http://microsoft.com/msdn

Developer’s Kit, Licenses, and MORE!

Resources for Developers

Page 46: ADO.NET Entity Framework 技術指引與應用 奚江華 作家/微軟講師/技術顧問.

46

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED

OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.