创建实体之间的关联

您可以通过创建关联来定义业务数据连接 (BDC) 模型中的实体之间的关系。 Visual Studio 会生成方法为模型使用者提供每个关联的相关信息。 SharePoint Web 部件、列表或自定义应用程序可以使用这些方法在用户界面 (UI) 中显示数据关系。

创建关联

可以通过以下方式创建关联:在 Visual Studio “工具箱”中选择“关联”,单击第一个实体(称为源实体),然后单击第二个实体(称为目标实体)。 可以在**“关联编辑器”**中定义关联的详细信息。 有关更多信息,请参见如何:创建实体之间的关联

关联方法

应用程序(如 SharePoint 业务数据 Web 部件)通过调用实体的服务类中的方法使用关联。 可以通过在**“关联编辑器”**中选择方法来向实体的服务类添加方法。

默认情况下,**“关联编辑器”**会向源实体和目标实体添加关联导航方法。 源实体中的关联导航方法允许使用者检索目标实体列表。 目标实体中的关联导航方法允许使用者检索与目标实体关联的源实体。

必须将代码添加到上述各方法才能返回相应的信息。 还可以添加其他类型的方法以支持更高级的方案。 有关上述各方法的更多信息,请参见 Supported Operations(支持的操作)。

关联的类型

可以在 BDC 设计器中创建两种类型的关联:基于外键的关联和无外键的关联。

基于外键的关联

通过将源实体中的标识符与目标实体中定义的类型描述符相关联,可以创建基于外键的关联。 这种关系使模型的使用者可以为其用户提供增强的 UI。 例如,Outlook 中使用户能够创建以下拉列表形式显示客户的销售订单的窗体;或 SharePoint 中使用户能够打开客户配置页的销售订单列表。

若要创建基于外键的关联,请关联共享相同的名称和类型的标识符和类型描述符。 例如,您可以在 Contact 实体和 SalesOrder 实体之间创建基于外键的关联。 SalesOrder 实体返回 ContactID 类型描述符,作为 Finder 或特定 Finder 方法返回参数的一部分。 这两个类型描述符显示在**“关联编辑器”**中。 若要创建 Contact 实体和 SalesOrder 实体之间基于外键的关联,请选择每个字段旁边的 ContactID 标识符。

将代码添加到源实体中可返回目标实体集合的关联导航方法。 下面的示例返回一个联系人的销售订单。

Public Shared Function ContactToSalesOrder(ByVal contactID As Integer) As IEnumerable(Of SalesOrderHeader)
    Const ServerName As String = "MySQLServerName"
    Dim dataContext As AdventureWorksDataContext = _
        New AdventureWorksDataContext("Data Source=" & ServerName & _
            ";Initial Catalog=AdventureWorks;Integrated Security=True")

    Dim orderList As IEnumerable(Of SalesOrderHeader) = _
        From orders In dataContext.SalesOrderHeaders _
        Where orders.ContactID = contactID _
              Select orders
    Return orderList

End Function
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;
}

将代码添加到目标实中可返回源实体的关联导航方法。 下面的示例返回与销售订单相关的联系人。

Public Shared Function SalesOrderToContact(ByVal salesOrderID As Integer) As IEnumerable(Of Contact)

    Const ServerName As String = "MySQLServerName"
    Dim dataContext As AdventureWorksDataContext = _
        New AdventureWorksDataContext("Data Source=" & ServerName & _
            ";Initial Catalog=AdventureWorks;Integrated Security=True")

    Dim TempContactID As Integer = _
        (From orders In dataContext.SalesOrderHeaders _
        Where orders.SalesOrderID = salesOrderID _
        Select orders.ContactID).[Single]()

    Dim contactList As IEnumerable(Of Contact) = _
        From contacts In dataContext.Contacts _
        Where contacts.ContactID = TempContactID _
        Select contacts
    Return contactList

End Function
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 实体的关联导航方法中,可通过联接表或调用存储过程来返回 SalesOrderDetail 实体。

下面的示例通过联接表返回所有销售订单的详细信息。

Public Shared Function ContactToSalesOrderDetail(ByVal contactID As Integer) As IEnumerable(Of SalesOrderDetail)
    Const ServerName As String = "MySQLServerName"
    Dim dataContext As AdventureWorksDataContext = _
        New AdventureWorksDataContext("Data Source=" & ServerName & _
            ";Initial Catalog=AdventureWorks;Integrated Security=True")

    Dim orderList As IEnumerable(Of SalesOrderDetail) = _
        From orders In dataContext.SalesOrderHeaders _
        Join orderDetails In dataContext.SalesOrderDetails On _
            orders.SalesOrderID Equals orderDetails.SalesOrderID _
        Where orders.ContactID = contactID _
        Select orderDetails
    Return orderList

End Function
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 实体的关联导航方法中,将返回相关的 Contact。 下面的示例说明了这一点。

Public Shared Function SalesOrderDetailToContact(ByVal salesOrderID As Integer, ByVal salesOrderDetailID As Integer) As IEnumerable(Of Contact)
    Const ServerName As String = "MySQLServerName"
    Dim dataContext As AdventureWorksDataContext = _
        New AdventureWorksDataContext("Data Source=" & ServerName & _
            ";Initial Catalog=AdventureWorks;Integrated Security=True")

    Dim TempContactID As Integer = _
        (From orders In dataContext.SalesOrderHeaders _
        Where orders.SalesOrderID = salesOrderID _
        Select orders.ContactID).[Single]()

    Dim contactList As IEnumerable(Of Contact) = _
        From contacts In dataContext.Contacts _
        Where contacts.ContactID = TempContactID _
        Select contacts
    Return contactList

End Function
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;
}

请参见

其他资源

设计业务数据连接模型

如何:创建实体之间的关联