AdventureWorks オブジェクト モデルの使用 (EDM)
以下の例に示す AdventureWorks クライアント アプリケーションが使用するコードは、エンティティ データ モデル (EDM) の機能をいくつか示しています。前の AdventureWorks モデルのトピックで説明したスキーマは、このセクションのコードで使用するエンティティおよびアソシエーションの基礎となります。
スキーマで設計され、ストレージにマップされる型は、プログラミング可能なオブジェクト モデルとして構築されます。このモデルのデータは、コードに SQL クエリを文字列として埋め込まずに、共通言語ランタイム (CLR) 構文を使用してプログラミングできます。
アプリケーション構成
オブジェクト モデルを使用するには、アプリケーション データが保存されているデータベースに接続する必要があります。スキーマから構築される DLL が提供するランタイム オブジェクトにエンティティを接続する必要もあります。
exe.config ファイルには、SQL Server データベースに接続し、エンティティ接続を確立するために使用する接続文字列が含まれています。エンティティ接続を確立すると、オブジェクト モデル内のエンティティやアソシエーションにコードからアクセスできるようになります。
接続文字列テキストは、開発者が exe.config ファイルに追加する必要があります。
providerName="System.Data.EntityClient"
の割り当ては、このセクションの他のトピックに示されているスキーマおよびスキーマに定義されているマッピングを使用するエンティティ接続を指定します。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="AdventureworksContext" connectionString='Metadata=.;
Provider=System.Data.SqlClient;
Provider Connection String="server=.\sqlExpress;
database=Adventureworks;
Integrated Security=true;Connection Timeout=5;
multipleactiveresultsets=true"'
providerName="System.Data.EntityClient"/>
</connectionStrings>
</configuration>
[!メモ]
この接続文字列は、複数のアクティブ結果セットを true に設定します。これは、別のデータ リーダーが同じ接続上で既に開いている場合にアソシエーションに対して Load メソッドを呼び出すために必要になります。
AdventureWorks エンティティおよびアソシエーションの使用
次のコードは、AdventureWorks セクションの他のトピックのスキーマに定義されているエンティティおよびアソシエーションに基づいたクエリを示します。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdventureWorks;
using System.Data.Objects;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Collections.ObjectModel;
namespace AdventureworksClient
{
class Program
{
static void Main(string[] args)
{
using (AdventureWorksContext objCtx = new AdventureWorksContext())
{
// find first 50 employees and display data.
foreach (Employee employee in
objCtx.Employee.Where("it.EmployeeID < 50"))
{
Console.WriteLine("EmployeeID: {0}",
employee.EmployeeID);
// Find employee job candidate and print resume.
employee.JobCandidate.Load();
foreach (JobCandidate jc in employee.JobCandidate)
Console.WriteLine("\tJobCandidate: {0} \n\t{1}",
jc.JobCandidateID, jc.Resume);
// Find manager of employee and display.
if (null != employee.Employee2)
{
employee.Employee2.Employee2Reference.Load();
Console.WriteLine("Employee: {0} Manager: {1}",
employee.EmployeeID.ToString(),
employee.Employee2.EmployeeID.ToString());
}
}
// Employees IDs 1 - 290.
// Find first 50 with contact information.
foreach (Contact contact in
objCtx.Contact.Where("it.ContactID < 50"))
{
Console.WriteLine("Contact: {0} {1} {2}",
contact.ContactID, contact.FirstName,
contact.LastName);
foreach (EmployeeAddress emplAddress in
objCtx.EmployeeAddress)
{
if (emplAddress.EmployeeID.Equals(contact.ContactID))
{
ObjectParameter param = new
ObjectParameter("p",
emplAddress.AddressID);
Address address =
objCtx.Address.Where(
"it.AddressID = @p",
param).First();
Console.WriteLine(
"\tEmployee address: {0}\n\t{1}\n\t{2}",
address.AddressLine1,
address.City, address.PostalCode);
}
}
}
// Find and display vendors.
foreach (Vendor vendor in objCtx.Vendor)
Console.WriteLine("Vendor: {0}", vendor.Name);
// Find and display store information.
foreach (Store store in objCtx.Store)
Console.WriteLine("Store: {0}", store.Name);
// Find and display product information.
foreach (Product product in objCtx.Product)
{
Console.WriteLine("Product Name: {0}", product.Name);
product.ProductModelReference.Load();
if (null != product.ProductModel)
{
Console.WriteLine("Model: {0}",
product.ProductModel.ProductModelID.ToString());
}
}
}
try
{
// Establish a connection to the underlying data provider by
// using the connection string specified in the config file.
using (EntityConnection connection =
new EntityConnection("Name=AdventureWorksContext"))
{
// Open the connection.
connection.Open();
// Access the metadata workspace.
MetadataWorkspace workspace =
connection.GetMetadataWorkspace();
// To run DisplayProperties(workspace, DataSpace.CSpace);
// to display the extended properties in the conceptual model.
// see Extended Property (CSDL) (link under See Also).
}
}
catch (System.Data.MetadataException exceptionMetadata)
{
Console.WriteLine("MetadataException: {0}",
exceptionMetadata.Message);
}
catch (System.Data.MappingException exceptionMapping)
{
Console.WriteLine("MappingException: {0}",
exceptionMapping.Message);
}
}
}
}
参照
概念
その他のリソース
EDM 仕様
スキーマおよびマッピング スキーマ (Entity Framework)
サンプル アプリケーション (Entity Framework)