Agenda ADO NET Entity Framework ADO NET Entity

  • Slides: 39
Download presentation

Agenda ADO. NET Entity Framework の全体像 ADO. NET Entity Framework の使い方 ADO. NET Entity

Agenda ADO. NET Entity Framework の全体像 ADO. NET Entity Framework の使い方 ADO. NET Entity Framework v. Next の 新機能 まとめ

ADO. NET Entity Framework 概要 ~柔軟な概念モデリングとデータアクセスが可能~ LINQ Entity SQL IEnumerable<T> BEC Entity Data Model

ADO. NET Entity Framework 概要 ~柔軟な概念モデリングとデータアクセスが可能~ LINQ Entity SQL IEnumerable<T> BEC Entity Data Model O-C Map SELECT VALUE c FROM Customers Data Reader Entity CCTSQL CCT SELECT VALUE c FROM Customers Client View Engine üObject. Context üObject. Query<T> üData Class Entity. Client üEntity. Connection üEntity. Command üEntity. Data. Reader T-CCT SQL Data Reader SELECT * FROM Customers MSL ストアスキーマ ADO. NET Entity Framework Object Services Entity. Client Data Provider C-S Map SSDL LINQ to Entities Object Services 概念スキーマ CSDL From Customers Select name Sql. Client Data Provider Oracle. Client Data Provider SQL Server Oracle Other Data Provider その他 DB ADO. NET 2. 0 üConnection üCommand üData. Reader

Entity. Client Data Provider 概要 EDM でモデリングした概念モデルへの問い 合わせ処理を提供 問い合わせ言語(Entity SQL)をサポート 結果として Entity. Data. Reader

Entity. Client Data Provider 概要 EDM でモデリングした概念モデルへの問い 合わせ処理を提供 問い合わせ言語(Entity SQL)をサポート 結果として Entity. Data. Reader のインスタンスを取得 ストアドプロシージャをサポート Data Reader Entity SQL SELECT VALUE c FROM Customers c Client View Engine Entity. Client Data Provider CCT Entity. Client üEntity. Connection üEntity. Command üEntity. Data. Reader

Entity. Client Data Provider による問い合わせ ~概念モデルへの問い合わせ(その1)~ using (Entity. Connection connection = new       Entity.

Entity. Client Data Provider による問い合わせ ~概念モデルへの問い合わせ(その1)~ using (Entity. Connection connection = new       Entity. Connection("Name=Northwind. JEntities")) { connection. Open(); Entity. Command command = connection. Create. Command(); command. Command. Text = “SELECT VALUE d from Northwind. JEntities. Drink. Products AS d"; Northwind. JEntities. Drink. Products AS d Entity. Data. Reader edr =    command. Execute. Reader(Command. Behavior. Sequential. Access);  connection. close(); } 実際に実行される T-SQL (一部省略) SELECT [Extent 1]. [Product. ID] AS [Product. ID], [Extent 2]. [Product. Kana] AS [Product. Kana], ・・・・・・・・・ FROM [dbo]. [Drink. Details] AS [Extent 1] INNER JOIN [dbo]. [Products] AS [Extent 2]      ON [Extent 1]. [Product. ID] = [Extent 2]. [Product. ID]

Object Services 概要 EDM でモデリングした概念モデルへの問い合わせ処理を提 供 2つの問い合わせ言語 Entity SQL (Query Builder) LINQ to Entities

Object Services 概要 EDM でモデリングした概念モデルへの問い合わせ処理を提 供 2つの問い合わせ言語 Entity SQL (Query Builder) LINQ to Entities 結果として IEnumerable のインスタンスを取得 DML(INSERT/UPDATE/DELETE) をメソッドでサポート ストアドプロシージャのサポート LINQ IEnumerable<T> Entity SQL SELECT VALUE c FROM Customers Object Services CCT From Customers Select name LINQ to Entities Object Services üObject. Context üObject. Query<T> üData Class

Object Services による問い合わせ ~概念モデルへの問い合わせ(その 2)~ Entity SQL using (Northwind. JEntities context = new Northwind.

Object Services による問い合わせ ~概念モデルへの問い合わせ(その 2)~ Entity SQL using (Northwind. JEntities context = new Northwind. JEntities()) {  Object. Query<Drink. Product> drink. Products = Object. Query<Drink. Product>     context. Create. Query<Drink. Product>("SELECT VALUE d from       Northwind. JEntities. Drink. Products AS d"); Northwind. JEntities. Drink. Products AS d } 実際に実行される T-SQL (一部省略) SELECT [Extent 1]. [Product. ID] AS [Product. ID], [Extent 2]. [Product. Kana] AS [Product. Kana], ・・・・・・・・・ FROM [dbo]. [Drink. Details] AS [Extent 1] INNER JOIN [dbo]. [Products] AS [Extent 2]      ON [Extent 1]. [Product. ID] = [Extent 2]. [Product. ID]

Object Services による問い合わせ ~概念モデルへの問い合わせ(その 3)~ LINQ to Entities using (Northwind. JEntities context = new

Object Services による問い合わせ ~概念モデルへの問い合わせ(その 3)~ LINQ to Entities using (Northwind. JEntities context = new Northwind. JEntities()) {   var drink. Products = context. Drink. Products; } 実際に実行される T-SQL (一部省略) SELECT [Extent 1]. [Product. ID] AS [Product. ID], [Extent 2]. [Product. Kana] AS [Product. Kana], ・・・・・・・・・ FROM [dbo]. [Drink. Details] AS [Extent 1] INNER JOIN [dbo]. [Products] AS [Extent 2]      ON [Extent 1]. [Product. ID] = [Extent 2]. [Product. ID]

Object Services による DML 操作 ~Insert 編~ Drink. Product p = new Drink. Product();

Object Services による DML 操作 ~Insert 編~ Drink. Product p = new Drink. Product(); p. Product. ID = 150; p. Product. Name = “新リボン”; p. Size = 300; context. Add. To. Drink. Products(dp); context. Save. Changes(); 実際に実行される T-SQL (一部省略) insert [dbo]. [Products]([Product. ID], [Product. Name]) values (150, ‘新リボン'); insert [dbo]. [Drink. Details]([Product. ID], [Size]) values (150, 300);

Object Services による DML 操作 ~Update 編~ Drink. Product dp = context. Drink. Products.

Object Services による DML 操作 ~Update 編~ Drink. Product dp = context. Drink. Products. Where(d => d. Product. ID == 150). First(); dp. Product. Name = "改良型リボン"; dp. Size = 1000; context. Save. Changes(); 実際に実行される T-SQL (一部省略) update [dbo]. [Products] set [Product. Name] = '改良型リボン' where ([Product. ID] = 150); update [dbo]. [Drink. Details] set [Size] = 1000 where ([Product. ID] = 150);

Object Services による DML 操作 ~Delete 編~ Drink. Product dp = context. Drink. Products.

Object Services による DML 操作 ~Delete 編~ Drink. Product dp = context. Drink. Products. Where(d => d. Product. ID == 150). First(); context. Delete. Object(dp); context. Save. Changes(); 実際に実行される T-SQL (一部省略) delete [dbo]. [Products] where ([Product. ID] = 150); delete [dbo]. [Drink. Details] where ([Product. ID] = 150);

Object Services による DML 操作 ~トランザクション~ Using (Transaction. Scope transaction = new Transaction. Scope())

Object Services による DML 操作 ~トランザクション~ Using (Transaction. Scope transaction = new Transaction. Scope()) { Drink. Product dp = context. Drink. Products. Where(d => d. Product. ID == 150). First(); context. Delete. Object(dp); context. Save. Changes(false); Drink. Product p = new Drink. Product(); p. Product. ID = 151; p. Product. Name = “新リボン”; p. Size = 1000; context. Add. To. Drink. Products(dp); context. Save. Changes(false); //トランザクションの確定 transaction. Complete(); } // トラッキング情報のクリア Context. Accept. All. Changes(); ※このサンプルでは本来 Save. Changes の呼び出しは一回で事足りるが、 トランザクション処理を説明するために複数回コールしている。

ストアドプロシージャによる DML 処理 ~Entity Client~ entity. Command. Type =        System. Data. Command. Type. Stored.

ストアドプロシージャによる DML 処理 ~Entity Client~ entity. Command. Type =        System. Data. Command. Type. Stored. Procedure; // 事前にモデルブラウザから「関数インポートの作成」を実行 entity. Command. Text = string. Format("{0}. {1}", context. Default. Container. Name, "Insert. Drink. Product. Import"); // ストアドプロシージャのパラメータをセット Entity. Parameter ep 1 = new Entity. Parameter(); ep 1. Parameter. Name = @"productid"; ep 1. Db. Type = System. Data. Db. Type. Int 32; ep 1. Value = 2101; entity. Command. Parameters. Add(ep 1); ・ ・ ・(中略) ・ ・ ・ Eentity. Connection. Open(); entity. Command. Execute. Scalar(); entity. Connection. Close(); 実際に実行されるストアドプロシージャ exec [dbo]. [Insert. Drink. Product] @productid=2101, @productname=N'Test. Product', @size=1000

ADO. NET Entity Framework v. Next の新機能 ~ADO. NET Entity Framework のさらなる進化~

ADO. NET Entity Framework v. Next の新機能 ~ADO. NET Entity Framework のさらなる進化~

EDM の拡張 ~Complex Type をデザイナから生成可能に~ csdl ファイルの定義 <Complex. Type Name=“CAddress"> <Property Name=“Street. Address" Type="String“/>

EDM の拡張 ~Complex Type をデザイナから生成可能に~ csdl ファイルの定義 <Complex. Type Name=“CAddress"> <Property Name=“Street. Address" Type="String“/> <Property Name=“City" Type="String" /> <Property Name=“Postal Code" Type="String" /> <Property Name=“Country" Type="String" /> </Complex. Type> <Entity. Type Name="Customer"> <Key> <Property. Ref Name="Customer. Id" /> </Key> <Property Name="Customer. Id" Type="Int 32" Nullable="false" /> <Property Name="Contact. Name" Type="String" /> <Property Name="Address" Type="Self. CAddress" Nullable="false" /> </Entity. Type> <Entity. Type Name=“Employee"> <Key> <Property. Ref Name=“Employee. Id" /> </Key> <Property Name=“Employee. Id" Type="Int 32" Nullable="false" /> <Property Name=“Employee. Name" Type="String" /> <Property Name="Address" Type="Self. CAddress" Nullable="false" /> </Entity. Type>

EDM の拡張 ~ユーザー定義 テーブル値関数の活用~ var cp = from p in context. Products     select

EDM の拡張 ~ユーザー定義 テーブル値関数の活用~ var cp = from p in context. Products     select new { Product = p,    Customers = context. Get. Customers. For. Product(p) }; foreach(Product p in cp. Products) { foreach(Customer c in p. Customers) { ユーザー定義 テーブル値関数の例 CREATE FUNCTION Get. Customers. For. Product(@Product. ID int) RETURNS @ret. Customers. Information TABLE ( -- Columns returned by the function Customer. ID int PRIMARY KEY NOT NULL,    Name nvarchar(50) NULL, ) AS BEGIN DECLARE

EDM の拡張 ~ Model Defined Functions~ CSDLファイルの定義 様々な型を指定可能 • Scalar • Entity • Complex

EDM の拡張 ~ Model Defined Functions~ CSDLファイルの定義 様々な型を指定可能 • Scalar • Entity • Complex • Row. Type • Ref. Type <Function Name=“Get. Age" Return. Type=“Edm. Int 32"> Get. Age <Parameter Name=“Customer" Type=“Edm. Customer" /> <Defining. Expression> Edm. Date. Diff(“y”, GETDATE(), Customer. Birthday) </Defining. Expression> </Function> Entity SQLを記述 Model Defined Functions 利用方法 var Customer. U 20 = from c in context. Customers where c. Get. Age(c) < 20 Get. Age select c;

POCO(Plain Old CLR Object) ~プラットフォームに依存しない persistence ignorance(PI) 型開発~ デザイナにより自動生成されるBEC(抜粋) public partial class Order :

POCO(Plain Old CLR Object) ~プラットフォームに依存しない persistence ignorance(PI) 型開発~ デザイナにより自動生成されるBEC(抜粋) public partial class Order :       global: : System. Data. Objects. Data. Classes. Entity. Object { public int Order. Code { get { return this. _Order. Code; } set { this. On. Order. Code. Changing(value); this. Report. Property. Changing("Order. Code"); this. _Order. Code =global: : System. Data. Objects. Data. Classes. Structural. Object. Set. Vali d. Value(value); this. Report. Property. Changed("Order. Code"); this. On. Order. Code. Changed(); }    } POCOベースで実装したBEC(抜粋)    partial void On. Order. Code. Changing(int value); public class Order partial void On. Order. Code. Changed(); { ・ ・ ・ ・ public int Order. Code { get; set; } public string Customer. ID { get; set; } public string Customer. Name { get; set; }    ・ ・ ・ ・ }

遅延ロード ~現バージョンにおける動作~ 社員名と担当地域を表示したい・・・・ Northwind. JEntities context = new Northwind. JEntities(); var emps = context.

遅延ロード ~現バージョンにおける動作~ 社員名と担当地域を表示したい・・・・ Northwind. JEntities context = new Northwind. JEntities(); var emps = context. Employees; foreach (var emp in emps){ Console. Write. Line(emp. Name); foreach (var et in emp. Territories){ Console. Write. Line(" " + et. Territory. Description);

遅延ロード ~現バージョンにおける 2 つの対処方法(その 1)~ 「社員名」と「担当地域」を表示したい・・・・ Northwind. JEntities context = new Northwind. JEntities(); var

遅延ロード ~現バージョンにおける 2 つの対処方法(その 1)~ 「社員名」と「担当地域」を表示したい・・・・ Northwind. JEntities context = new Northwind. JEntities(); var emps = context. Employees. Include("Territories"); foreach (var emp in emps){ Console. Write. Line(emp. Name); foreach (var et in emp. Territories) { Console. Write. Line(“ ” + et. Territory. Description);

遅延ロード ~現バージョンにおける 2 つの対処方法(その 2)~ 「社員名」と「担当地域」を表示したい・・・・ Northwind. JEntities context = new Northwind. JEntities(); var

遅延ロード ~現バージョンにおける 2 つの対処方法(その 2)~ 「社員名」と「担当地域」を表示したい・・・・ Northwind. JEntities context = new Northwind. JEntities(); var emps = context. Employees; foreach (var emp in emps){ Console. Write. Line(emp. Name); if (!emp. Territories. Is. Loaded) emp. Territories. Load(); foreach (var et in emp. Territories) { Console. Write. Line(“ ” +et. Territory. Description);

遅延ロード ~v. Next では透過的にサポート~ 「社員名」と「担当地域」を表示したい・・・・ Northwind. JEntities context = new Northwind. JEntities(); context. Deferred.

遅延ロード ~v. Next では透過的にサポート~ 「社員名」と「担当地域」を表示したい・・・・ Northwind. JEntities context = new Northwind. JEntities(); context. Deferred. Loading. Enabled = true; var emps = context. Employees; foreach (var emp in emps){ Console. Write. Line(emp. Name); foreach (var et in emp. Territories) { Console. Write. Line(“ " + et. Territory. Description);

APPENDIX 井上大輔のblog http: //blogs. msdn. com/daisukei Entity Framework Design(英語) http: //blogs. msdn. com/efdesign/ ADO.

APPENDIX 井上大輔のblog http: //blogs. msdn. com/daisukei Entity Framework Design(英語) http: //blogs. msdn. com/efdesign/ ADO. NET team blog(英語) http: //blogs. msdn. com/adonet/default. aspx MSDNマガジン(日本語) Entity Framework で柔軟なデータ モデリングを実現する http: //msdn. microsoft. com/ja-jp/magazine/cc 700331. aspx Entity Framework に関する Q&A http: //msdn. microsoft. com/ja-jp/magazine/cc 507640. aspx エンティティ データ モデルをデザインする http: //msdn. microsoft. com/ja-jp/magazine/cc 163286. aspx 階層型アーキテクチャの Entity Framework http: //msdn. microsoft. com/ja-jp/magazine/cc 700340. aspx LINQ to SQL と Entity Framework による柔軟なデータ アクセス http: //msdn. microsoft. com/ja-jp/magazine/dd 263098. aspx MSDN Code Gallery http: //code. msdn. microsoft. com/adonetefx MSDN Library http: //msdn. microsoft. com/ja-jp/library/bb 399572. aspx

プロバイダ サポート状況 現在提供中、もしくは近々提供予定のもの Devart Oracle, My. SQL, Postgre. SQL, SQLite Firebird databases IBM DB

プロバイダ サポート状況 現在提供中、もしくは近々提供予定のもの Devart Oracle, My. SQL, Postgre. SQL, SQLite Firebird databases IBM DB 2 data server , Informix Dynamic Server (IDS) Npgsql Postgre. SQL database versions 7. 3+ and 8. x Open. Link Software Open. Link Virtuoso, Oracle, Informix, Ingres, Sybase, My. SQL, Postgre. SQL, DB 2 Phoenix Software Solutions SQLite databases Sun Microsystems My. SQL databases Sybase SQL Anywhere databases Vista. DB Software Vista. DB database Synergex Synergy/DE databases Data. Direct Technologies Oracle, Sybase, Microsoft SQL Server, DB 2

© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows Vista and other product names

© 2009 Microsoft Corporation. All rights reserved. Microsoft, 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.