實體之間的關聯
您可以藉由建立關聯,在商務資料連接 (BDC) 模型中定義實體之間的關聯性。 Visual Studio 會產生方法,以提供模型取用者與每個關聯的相關資訊。 這些方法可以由 SharePoint Web 組件、清單或自訂應用程式加以使用,以便在使用者介面 (UI) 中顯示資料關聯性。
建立關聯
在 Visual Studio [工具箱] 中選擇 [關聯] 控制項,然後選擇第一個實體 (稱為來源實體),然後選擇第二個實體 (稱為目的地實體)。 您可以在 關聯編輯器 中定義關聯的詳細資料。 如需詳細資訊,請參閱 如何: 建立實體之間的關聯。
關聯方法
SharePoint 商務資料 Web 元件之類的應用程式會藉由呼叫實體服務類別中的方法來取用關聯。 您可以在 關聯編輯器 中選取方法,以將方法新增至實體的服務類別。
根據預設,關聯編輯器 會將關聯導覽方法新增至來源和目的地實體。 來源實體中的關聯導覽方法可讓取用者擷取目的地實體的清單。 目的地實體中的關聯導覽方法可讓取用者擷取與目的地實體相關的來源實體。
您必須將程式碼新增至每個方法,才能傳回適當的資訊。 您也可以新增其他類型的方法,以支援更進階的案例。 如需這些方法的詳細資訊,請參閱 支援的作業。
關聯類型
您可以在 BDC 設計工具中建立兩種類型的關聯: 外部索引鍵型關聯和外部無索引鍵關聯。
外部索引鍵型關聯
您可以建立外部索引鍵型關聯,方法是將來源實體中的識別碼關聯至目的地實體中定義的類型描述元。 此關聯性可讓模型的取用者為其使用者提供增強的 UI。 例如,Outlook 中的表單可讓使用者建立銷售訂單,以在下拉式清單中顯示客戶;或 SharePoint 中的銷售訂單清單,可讓使用者開啟客戶的設定檔頁面。
若要建立外部索引鍵型關聯,請建立共用相同名稱和類型的識別碼和類型描述元之間的關聯。 例如,您可以在 Contact
實體與 SalesOrder
實體之間建立外部索引鍵型關聯。 SalesOrder
實體會傳回 ContactID
類型描述元,做為 Finder 或 Specific Finder 方法傳回參數的一部分。 這兩個類型描述元都會出現在 關聯編輯器中。 若要在 Contact
實體與 SalesOrder
實體之間建立外部索引鍵型關聯,請選擇每個欄位旁的 ContactID
識別碼。
將程式碼新增至來源實體的 Association Navigator 方法,以傳回目的地實體的集合。 下列範例會傳回連絡人的銷售訂單。
public static IEnumerable<SalesOrderHeader> ContactToSalesOrder(int contactID)
{
const string ServerName = "MySQLServerName";
AdventureWorksDataContext dataContext = new AdventureWorksDataContext
("Data Source=" + ServerName + ";" +
"Initial Catalog=AdventureWorks;Integrated Security=True");
IEnumerable<SalesOrderHeader> orderList =
from orders in dataContext.SalesOrderHeaders
where orders.ContactID == contactID
select orders;
return orderList;
}
將程式碼新增至傳回來源實體之目的地實體的 Association Navigator 方法。 下列範例會傳回與銷售訂單相關的連絡人。
public static IEnumerable<Contact> SalesOrderToContact(int salesOrderID)
{
const string ServerName = "MySQLServerName";
AdventureWorksDataContext dataContext = new AdventureWorksDataContext
("Data Source=" + ServerName + ";" +
"Initial Catalog=AdventureWorks;Integrated Security=True");
int TempContactID = (from orders in dataContext.SalesOrderHeaders
where orders.SalesOrderID == salesOrderID
select orders.ContactID).Single();
IEnumerable<Contact> contactList = from contacts in dataContext.Contacts
where contacts.ContactID == TempContactID
select contacts;
return contactList;
}
外部無索引鍵關聯
您可以建立關聯,而不需將識別碼對應至欄位類型描述元。 當來源實體與目的地實體沒有直接關聯性時,請建立這種關聯。 例如,SalesOrderDetail
資料表沒有對應至 Contact
資料表中主索引鍵的外部索引鍵。
如果您想要在與 Contact
相關的 SalesOrderDetail
資料表中顯示資訊,您可以在 Contact
實體與 SalesOrderDetail
實體之間建立外部無索引鍵關聯。
在 Contact
實體的 Association Navigation 方法中,藉由聯結資料表或呼叫預存程序,傳回 SalesOrderDetail
實體。
下列範例會聯結資料表,傳回所有銷售訂單的詳細資料。
public static IEnumerable<SalesOrderDetail> ContactToSalesOrderDetail(int contactID)
{
const string ServerName = "MySQLServerName";
AdventureWorksDataContext dataContext = new AdventureWorksDataContext
("Data Source=" + ServerName + ";" +
"Initial Catalog=AdventureWorks;Integrated Security=True");
IEnumerable<SalesOrderDetail> orderList =
from orders in dataContext.SalesOrderHeaders
join orderDetails in dataContext.SalesOrderDetails on
orders.SalesOrderID equals orderDetails.SalesOrderID
where orders.ContactID == contactID
select orderDetails;
return orderList;
}
在 SalesOrderDetail
實體的 Association Navigation 方法中,傳回相關的 Contact
。 下列範例示範此作業。
public static IEnumerable<Contact> SalesOrderDetailToContact(int salesOrderID, int salesOrderDetailID)
{
const string ServerName = "MySQLServerName";
AdventureWorksDataContext dataContext = new AdventureWorksDataContext
("Data Source=" + ServerName + ";" +
"Initial Catalog=AdventureWorks;Integrated Security=True");
int TempContactID = (from orders in dataContext.SalesOrderHeaders
where orders.SalesOrderID == salesOrderID
select orders.ContactID).Single();
IEnumerable<Contact> contactList = from contacts in dataContext.Contacts
where contacts.ContactID == TempContactID
select contacts;
return contactList;
}