底下範例顯示之 AdventureWorks 用戶端應用程式所使用的程式碼會示範 實體資料模型 (EDM) 的幾項功能。之前 AdventureWorks 模型主題中所述的結構描述是本章節程式碼所使用之實體和關聯的基礎。
在結構描述中設計且對應到儲存區的型別會建置為可程式化物件模型。可以使用 Common Language Runtime (CLR) 語法來針對此模型上的資料進行程式設計,而不需要將 SQL 查詢當做字串內嵌在程式碼中。
應用程式組態
使用此物件模型需要連接到儲存應用程式資料的資料庫。此外,也需要讓實體連接到從結構描述建置之 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>
附註 |
|---|
此連接字串會將 Multiple Active Result Set (MARS) 設定為 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)
附註