附註參考資料工具應用程式程式碼 (EDM 範例應用程式)

附註研究工具 實體資料模型 (EDM) 應用程式是使用 Visual Studio 工具箱中的 Windows Form 和控制項所實作。

WebBrowser 控制項是用來顯示 Web 參考。各種文字方塊可讓使用者輸入會變成附註和連絡人資訊的資訊。指派給 Reference 執行個體之 Locator 屬性的 URL 字串是從建立 ReferenceDescriptor 類別執行個體時,顯示在 WebBrowser 內之頁面上的文件物件中取得。事件處理常式會執行為了回應使用者輸入的所有工作。如果要顯示 UI 的顯示畫面,請參閱附註和研究共同作業工具 (EDM 範例應用程式)

Exe.Config 檔和連接字串

下列 exe.config 檔包含 SQL Server 或 SQL Server Compact 3.5 資料庫的連接字串與組態資訊。也會參考包含連接和中繼資料使用之類別的 System.Data.EntityClient 命名空間。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="ResearchCollaborationData"
         connectionString="metadata=.;
             provider=System.Data.SqlClient;
             provider connection string='server=servername;
             database=ResearchCollaborationData;
             integrated security=true;
             multipleactiveresultsets=true'"
         providerName="System.Data.EntityClient"/>
  </connectionStrings>
</configuration>
Note附註

此連接字串會將 Multiple Active Result Set (MARS) 設定為 true,因為當相同連接上已經開啟另一個資料讀取器時,會需要這樣的設定來叫用關聯上的 Load 方法。

初始化儲存區的連接

當之前的 exe.config 檔位於可執行檔範圍中而且有了專案中 System.Data.Entity.dll 的參考之後,一行程式碼便足夠開啟這個應用程式使用之資料的連接。

    ResearchCollaborationData researchCollaborationData =
                                    new ResearchCollaborationData();

用來查詢或更新這個應用程式使用之資料的所有事件處理常式都會使用這項初始化。下列陳述式會關閉此連接:

    researchCollaborationData.Connection.Close();

建立參考描述項

下列程式碼會建立 ReferenceDescriptor 類別的執行個體,並將它儲存到儲存區。此序列會判斷 WebBrowser 視窗內的頁面是否以 Reference 物件的形式存在。如果它不存在,就會使用 ReferenceDescriptor 建立新的 Reference 執行個體。如果 Reference 未在儲存區中,則會以呼叫 AddToReference 的方式將它加入至儲存區,語法為 researchCollaborationData.AddToReferecne(newReference)。此外,會以呼叫 researchCollaborationData.AddToReferenceDescriptor(newReferenceDescriptor) 的方式將 ReferenceDescriptor 加入至儲存區。

ReferenceDescriptor 也會加入由名為 RefDescriptorsReference 導覽屬性所代表的集合中:newReference.RefDescriptors.Add(newReferenceDescriptor)

必須要有關聯的 ReferenceDescriptor,應用程式才會實作方法來建立 Reference 執行個體。

        private void buttonCreateRefDescriptor_Click(
            object sender, EventArgs e)
        {
            ResearchCollaborationData researchCollaborationData = null;
            try
            {
                using (researchCollaborationData =
                    new ResearchCollaborationData())
                {
                    ObjectParameter param = new ObjectParameter(
                        "p", webBrowser1.Document.Url.ToString());

                    if (!researchCollaborationData.Reference.Where(
                        "it.Locator = @p", param).Any())
                    {
                        Reference newReference = new Reference();
                        newReference.ReferenceID = Guid.NewGuid(); 
                        newReference.Locator = webBrowser1.Document.Url.ToString();

                        researchCollaborationData.AddToReference(newReference);

                        ReferenceDescriptor newReferenceDescriptor =
                             new ReferenceDescriptor();
                        newReferenceDescriptor.DescriptorID = Guid.NewGuid();

                        newReferenceDescriptor.Keyword = 
                            textBoxKeyWord.Text;

                        newReferenceDescriptor.Annotation = 
                            textBoxAnnotationResults.Text;

                        researchCollaborationData.AddToReferenceDescriptor(newReferenceDescriptor);

                        newReference.RefDescriptors.Add(
                            newReferenceDescriptor);
                    }
                    else
                    {
                        Reference reference = 
                            researchCollaborationData.Reference.Where(
                            "it.Locator = @p", param).First();

                        ReferenceDescriptor newReferenceDescriptor = 
                            new ReferenceDescriptor();
                        newReferenceDescriptor.DescriptorID = Guid.NewGuid();

                        newReferenceDescriptor.Keyword = 
                            textBoxKeyWord.Text;

                        newReferenceDescriptor.Annotation = 
                            textBoxAnnotationResults.Text;
                        researchCollaborationData.AddToReferenceDescriptor(newReferenceDescriptor);

                        reference.RefDescriptors.Add(
                            newReferenceDescriptor);
                    }

                    researchCollaborationData.SaveChanges();
                    

                    researchCollaborationData.Connection.Close();
                }
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.ToString());
                researchCollaborationData.Connection.Close();
            }
        }

建立 ContactPerson 執行個體

建立新的 ContactPerson 執行個體並讓它與 Reference 產生關聯時,所遵循的程序與建立 ReferenceDescriptor 並將它與 Reference 產生關聯的程序相同。可能的程式碼選項有四個,取決於 ContactPersonReference 的其中一個存在或兩個都存在而定。在之前建立 ReferenceDescriptor 的案例中,一定會建立新的描述項執行個體。將 ContactPersonReference 產生關聯的程序會決定 ContactPerson 的執行個體是否已經代表此連絡人。

ReferenceDescriptor 只會與一個 Reference 產生關聯,而 ContactPerson 則可以與多個 References 產生關聯。如需實作可支援此關聯性之多對多 Association 的詳細資訊,請參閱附註參考資料工具結構描述 (EDM 範例應用程式)

下列事件處理常式內的四個程式碼路徑全都會將 ContactPerson 實體的執行個體與 Reference 實體的執行個體連接。第一個選項會同時建立新的 ContactPerson 和新的 Reference。新的 ContactPerson 和新的 Reference 都會使用 AddToContactPersonAddToReference 加入至儲存區。因為這是多對多關聯,所以也必須具現化新的連結實體,而且必須初始化它的導覽屬性,才能連接 ContactPersonReference 的執行個體。

                    ContactPersonReference newLink = 
                        new ContactPersonReference();
                    newLink.ContactPersonRefID = Guid.NewGuid();

                    newLink.RelatedContact = newContact;
                    newLink.RelatedReference = newReference;

此程式碼序列會具現化名為 ContactPersonReference 的連結實體。此連結實體的導覽屬性代表關聯的兩端:RelatedContactRelatedReference。這些會指派新的 ContactPersonReference 執行個體 (已建立在此程式碼路徑中)。在其他案例中,不論 ContactPersonReference 其中一個還是兩個都存在,指派都會使用物件查詢已找到的現有執行個體。

新的連結實體會加入到儲存區,而且所有新的實體執行個體都會儲存在下列程式碼內。

        researchCollaborationData.AddToContactPersonReference(newLink);
        researchCollaborationData.SaveChanges();

下列序列顯示四種可能程式碼路徑中的兩種。如需完整的事件處理常式,請參閱本主題結尾之完整事件處理常式程式碼內的方法:private void buttonCreateRefPerson_Click(object sender, EventArgs e).

                    ObjectParameter paramContact = 
                        new ObjectParameter("p", textBoxEmail.Text);

                    if (!researchCollaborationData.ContactPerson.Where(
                        "it.Email = @p", paramContact).Any())
                    {
                        ObjectParameter paramReference = 
                            new ObjectParameter("p", 
                            webBrowser1.Document.Url.ToString());

                        if (!researchCollaborationData.Reference.Where(
                            "it.Locator = @p", paramReference).Any())
                        {
                            // Neither contact nor reference exist.
                            ContactPerson newContact = new ContactPerson();
                            newContact.ContactPersonID = Guid.NewGuid(); 
                            newContact.LastName = textBoxLastName.Text;
                            newContact.FirstName = textBoxFirstName.Text; 
                            newContact.Email = textBoxEmail.Text;

                            newContact.Title = 
                                textBoxTitlePosition.Text;

                            researchCollaborationData.AddToContactPerson(newContact);

                            Reference newReference = new Reference();
                            newReference.ReferenceID = Guid.NewGuid(); 
                            newReference.Locator = webBrowser1.Document.Url.ToString();

                            researchCollaborationData.AddToReference(newReference);

                            ContactPersonReference newLink = 
                                new ContactPersonReference();
                            newLink.ContactPersonRefID = Guid.NewGuid();

                            newLink.RelatedContact = newContact;
                            newLink.RelatedReference = newReference;
                            researchCollaborationData.AddToContactPersonReference(newLink);
                        }

                        else
                        {
                            // Reference exists but contact doesn't.
                            Reference reference = 
                                researchCollaborationData.Reference.
                                Where("it.Locator = @p", 
                                paramReference).First();

                            ContactPerson newContact = new ContactPerson();
                            newContact.ContactPersonID = Guid.NewGuid(); 
                            newContact.LastName = textBoxLastName.Text;
                            newContact.FirstName = textBoxFirstName.Text; 
                            newContact.Email = textBoxEmail.Text;

                            newContact.Title = 
                                textBoxTitlePosition.Text;

                            researchCollaborationData.AddToContactPerson(newContact);

                            ContactPersonReference newLink = new ContactPersonReference();
                            newLink.ContactPersonRefID = Guid.NewGuid();
                            newLink.RelatedContact = newContact;
                            newLink.RelatedReference = reference;
                            researchCollaborationData.AddToContactPersonReference(newLink);

                        }
                    }

使用參考描述項來搜尋參考

ReferenceDescriptor 實體的執行個體是用來描述及尋找 Web 資源的附註,所以該搜尋不必重複或是在某個其他地方編目。每一個 ReferenceDescriptor 都代表單一附註。許多 ReferenceDescriptor 執行個體都可描述網頁,但是每一個執行個體只能與一個頁面產生關聯。

藉由加入 ReferenceDescriptor 執行個體來標註 Web 參考,如同之前的程式碼區段所述。搜尋 ReferenceDescriptor 執行個體的 AnnotationKeyword 屬性提供了一個方法來重新配置有用的 Reference 頁面。當使用者在搜尋文字方塊中輸入關鍵字或搜尋片語,並按一下 Find 按鈕時,就會使用下列事件處理常式。

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            ResearchCollaborationData researchCollaborationData = null;
            try
            {
                using (researchCollaborationData = 
                    new ResearchCollaborationData())
                {
                    // Make a list of keywords to search for in annotatations.
                    List<string> keywords = new List<string>();
                    int i = 0;
                    int j = 0;
                    while (i < textBoxSearch.Text.Length)
                    {
                        j = textBoxSearch.Text.IndexOf(" ", i);
                        if (-1 == j) j = textBoxSearch.Text.Length;

                        keywords.Add(
                             textBoxSearch.Text.Substring(i, j - i));

                        i = ++j;
                    }

                    textBoxAnnotationResults.Text = "Results:";
                    foreach (string keyword in keywords)
                    {
                        // Create ObjectParameter from each keyword.
                        ObjectParameter paramKeyword = 
                            new ObjectParameter(
                            "p", "%" + keyword + "%");

                        ObjectQuery<ReferenceDescriptor> 
                            descriptorQuery = 
                            researchCollaborationData.
                            ReferenceDescriptor.Where(
                            "it.Annotation LIKE @p OR it.Keyword LIKE @p",
                            paramKeyword);

                        foreach (ReferenceDescriptor refDescriptor
                                                  in descriptorQuery)
                        {

                            textBoxAnnotationResults.Text = 
                                textBoxAnnotationResults.Text + "\n" +
                                refDescriptor.Keyword + "\n" + 
                                refDescriptor.Annotation;

                           refDescriptor.ReferenceReference.Load();

                            Reference reference = refDescriptor.Reference;

                            textBoxAnnotationResults.Text = 
                                textBoxAnnotationResults.Text + "\n" +
                                reference.Locator + "\n";

                            foreach (ContactPersonReference contactPersRef in
                                researchCollaborationData.ContactPersonReference)
                            {
                                contactPersRef.RelatedReferenceReference.Load();
                                if (contactPersRef.RelatedReferenceReference.Value.Equals(
                                    reference))
                                {
                                    contactPersRef.RelatedContactReference.Load();

                                    textBoxAnnotationResults.Text =
                                    textBoxAnnotationResults.Text +
                                    "\n" +
                                    "Relevant Contact:";

                                    textBoxAnnotationResults.Text =
                                        textBoxAnnotationResults.Text +
                                        "\n" +
                                        contactPersRef.
                                        RelatedContact.FirstName + " " +
                                        contactPersRef.RelatedContact.
                                        LastName +
                                        " Title: " + contactPersRef.
                                        RelatedContact.Title + " Email: "
                                        + contactPersRef.RelatedContact.
                                        Email
                                        + "\n";
                                }
                            }                            
                        }                        
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
            
        }

此程式碼由兩個基本序列所組成:一個是用來建立搜尋字串的 List<T>,另一個是查詢來比對 ReferenceDescriptor 執行個體中 KeywordAnnotation 屬性內的文字。

建立搜尋字串清單是藉由剖析輸入文字來完成。此查詢是用於迴圈內的參數化 ObjectQuery,可將每一個搜尋字串插入查詢中,並將它與指派給儲存區內 ReferenceDescriptor 執行個體之 KeywordAnnotation 屬性的文字相比較。下列程式碼區段中會具現化 ObjectParameterObjectQuery 類別。

               // Create ObjectParameter from each keyword.
               ObjectParameter paramKeyword = 
                    new ObjectParameter(
                    "p", "%" + keyword + "%");

                ObjectQuery<ReferenceDescriptor> 
                     descriptorQuery = 
                     researchCollaborationData.
                     ReferenceDescriptor.Where(
                     "it.Annotation LIKE @p OR it.Keyword LIKE @p",
                     paramKeyword);

ObjectQuery 傳回的每一個 ReferenceDescriptor 會進入一個迴圈來載入相關的 Reference (使用它針對這個用途所設計的 NavigationPropertyAssociation)。下列程式碼區段會載入 Reference、讀取它的 Locator 屬性,並將 Reference 的 URL 顯示為結果文字方塊內的連結。

                           refDescriptor.ReferenceReference.Load();

                            Reference reference = refDescriptor.Reference;

                            textBoxAnnotationResults.Text = 
                                textBoxAnnotationResults.Text + "\n" +
                                reference.Locator + "\n";

最後,此方法會尋找及顯示與 Reference 相關之連絡人的相關資訊。LinkTable_ReferenceAssociation 的靜態方法可用來尋找與 Reference 相關的連結資料表實體。ContactPersonReference 的執行個體 (包含 Reference 的連結資料表實體) 會進入一個迴圈來載入及顯示相關 ContactPerson 的屬性。FirstNameLastNameTitleEmail 屬性會與之前程式碼找到的 ReferenceAnnotationLocator 文字成對顯示。

                            foreach (ContactPersonReference contactPersRef in
                                researchCollaborationData.ContactPersonReference)
                            {
                                contactPersRef.RelatedReferenceReference.Load();
                                if (contactPersRef.RelatedReferenceReference.Equals(
                                    reference))
                                {
                                    contactPersRef.RelatedContactReference.Load();

                                    textBoxAnnotationResults.Text =
                                    textBoxAnnotationResults.Text +
                                    "\n" +
                                    "Relevant Contact:";

                                    textBoxAnnotationResults.Text =
                                        textBoxAnnotationResults.Text +
                                        "\n" +
                                        contactPersRef.
                                        RelatedContact.FirstName + " " +
                                        contactPersRef.RelatedContact.
                                        LastName +
                                        " Title: " + contactPersRef.
                                        RelatedContact.Title + " Email: "
                                        + contactPersRef.RelatedContact.
                                        Email
                                        + "\n";
                                }
                            }  

尋找與連絡人相關的參考

可以使用與之前程式碼區段相同的連結資料表實體和關聯來尋找與 ContactPerson 執行個體有關的 Reference 執行個體。此應用程式的使用者可以從連絡人尋找參考頁面,其方式是在 LastName 及 (或) Email 文字方塊中輸入文字,並按下標記為 Find Ref/Person 的按鈕。會從 LastNameEmail 文字建立新的 ObjectParameter 執行個體。ObjectQuery 會使用參數來搜尋姓氏或電子郵件地址相符的人。ObjectQueryAny 方法是用來測試這個查詢中是否有任何結果。

如果找到姓氏或電子郵件地址相符的 ContactPerson 執行個體,即會顯示連絡人資訊,連同關聯的 Reference 文件。就如同之前的方法一樣,找到相關的文件 (使用 ContactPersonReference 連結實體和它的導覽屬性)。

        private void buttonFindRefPerson_Click(object sender, EventArgs e)
        {
            ResearchCollaborationData researchCollaborationData = null;
            try
            {
                using (researchCollaborationData = 
                    new ResearchCollaborationData())
                {
                    // Use parameters from LastName and 
                    // Email text boxes in search.
                    ObjectParameter emailParam = new ObjectParameter(
                        "email", textBoxEmail.Text);
                    ObjectParameter nameParam = new ObjectParameter(
                        "name", textBoxLastName.Text);
                    ObjectParameter[] objParams = { emailParam, 
                        nameParam };
                    
                    ObjectQuery<ContactPerson> query = 
                        researchCollaborationData.ContactPerson.Where(
                        "it.Email = @email OR it.LastName = @name", 
                        objParams );

                    if (query.Any())
                    {
                        textBoxAnnotationResults.Text = 
                            "Contact and associated reference documents:\n";

                        ContactPerson person = null;
                        query.FirstOrDefault(out person);

                        // Display contact information and 
                        // related references.
                        textBoxAnnotationResults.Text = 
                            textBoxAnnotationResults.Text +
                            person.FirstName + " " + person.LastName +
                            " Title: " + person.Title +
                            " Email address: " + person.Email;

                        ObjectParameter contactParam = 
                            new ObjectParameter("p", 
                            person.ContactPersonID);

                        foreach (ContactPersonReference contactReference
                            in researchCollaborationData.
                            ContactPersonReference.Where(
                            "it.RelatedContact.ContactPersonID = @p",
                            contactParam))
                        {
                            contactReference.RelatedReferenceReference.
                                Load();

                            textBoxAnnotationResults.Text = 
                                textBoxAnnotationResults.Text + "\n" +
                                contactReference.RelatedReference.Locator;
                        }
                    }                                        
                }
            }

            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }

        }

完整的應用程式事件處理常式程式碼

下列程式碼包含用來初始化 EntityConnection 及搜尋和更新「研究共同作業資料模型」上建置之資料的所有事件處理常式。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.Objects;
using System.Linq;
using ResearchCollaborationDataModel;

namespace ResearchCollaboration
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            try
            {
                webBrowser1.Navigate(
                    "http://www.live.com/?searchonly=true");               

            }

            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void buttonNavigate_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(textBoxUri.Text);
        }

        private void buttonCreateRefDescriptor_Click(
            object sender, EventArgs e)
        {
            ResearchCollaborationData researchCollaborationData = null;
            try
            {
                using (researchCollaborationData =
                    new ResearchCollaborationData())
                {
                    ObjectParameter param = new ObjectParameter(
                        "p", webBrowser1.Document.Url.ToString());

                    if (!researchCollaborationData.Reference.Where(
                        "it.Locator = @p", param).Any())
                    {
                        Reference newReference = new Reference();
                        newReference.ReferenceID = Guid.NewGuid(); 
                        newReference.Locator = webBrowser1.Document.Url.ToString();

                        researchCollaborationData.AddToReference(newReference);

                        ReferenceDescriptor newReferenceDescriptor =
                             new ReferenceDescriptor();
                        newReferenceDescriptor.DescriptorID = Guid.NewGuid();

                        newReferenceDescriptor.Keyword = 
                            textBoxKeyWord.Text;

                        newReferenceDescriptor.Annotation = 
                            textBoxAnnotationResults.Text;

                        researchCollaborationData.AddToReferenceDescriptor(newReferenceDescriptor);

                        newReference.RefDescriptors.Add(
                            newReferenceDescriptor);
                    }
                    else
                    {
                        Reference reference = 
                            researchCollaborationData.Reference.Where(
                            "it.Locator = @p", param).First();

                        ReferenceDescriptor newReferenceDescriptor = 
                            new ReferenceDescriptor();
                        newReferenceDescriptor.DescriptorID = Guid.NewGuid();

                        newReferenceDescriptor.Keyword = 
                            textBoxKeyWord.Text;

                        newReferenceDescriptor.Annotation = 
                            textBoxAnnotationResults.Text;
                        researchCollaborationData.AddToReferenceDescriptor(newReferenceDescriptor);

                        reference.RefDescriptors.Add(
                            newReferenceDescriptor);
                    }

                    researchCollaborationData.SaveChanges();
                    

                    researchCollaborationData.Connection.Close();
                }
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.ToString());
                researchCollaborationData.Connection.Close();
            }
        }

 
        private void buttonSearch_Click(object sender, EventArgs e)
        {
            ResearchCollaborationData researchCollaborationData = null;
            try
            {
                using (researchCollaborationData = 
                    new ResearchCollaborationData())
                {
                    // Make a list of keywords to search for in annotatations.
                    List<string> keywords = new List<string>();
                    int i = 0;
                    int j = 0;
                    while (i < textBoxSearch.Text.Length)
                    {
                        j = textBoxSearch.Text.IndexOf(" ", i);
                        if (-1 == j) j = textBoxSearch.Text.Length;

                        keywords.Add(
                             textBoxSearch.Text.Substring(i, j - i));

                        i = ++j;
                    }

                    textBoxAnnotationResults.Text = "Results:";
                    foreach (string keyword in keywords)
                    {
                        // Create ObjectParameter from each keyword.
                        ObjectParameter paramKeyword = 
                            new ObjectParameter(
                            "p", "%" + keyword + "%");

                        ObjectQuery<ReferenceDescriptor> 
                            descriptorQuery = 
                            researchCollaborationData.
                            ReferenceDescriptor.Where(
                            "it.Annotation LIKE @p OR it.Keyword LIKE @p",
                            paramKeyword);

                        foreach (ReferenceDescriptor refDescriptor
                                                  in descriptorQuery)
                        {

                            textBoxAnnotationResults.Text = 
                                textBoxAnnotationResults.Text + "\n" +
                                refDescriptor.Keyword + "\n" + 
                                refDescriptor.Annotation;

                           refDescriptor.ReferenceReference.Load();

                            Reference reference = refDescriptor.Reference;

                            textBoxAnnotationResults.Text = 
                                textBoxAnnotationResults.Text + "\n" +
                                reference.Locator + "\n";

                            foreach (ContactPersonReference contactPersRef in
                                researchCollaborationData.ContactPersonReference)
                            {
                                contactPersRef.RelatedReferenceReference.Load();
                                if (contactPersRef.RelatedReferenceReference.Equals(
                                    reference))
                                {
                                    contactPersRef.RelatedContactReference.Load();

                                    textBoxAnnotationResults.Text =
                                    textBoxAnnotationResults.Text +
                                    "\n" +
                                    "Relevant Contact:";

                                    textBoxAnnotationResults.Text =
                                        textBoxAnnotationResults.Text +
                                        "\n" +
                                        contactPersRef.
                                        RelatedContact.FirstName + " " +
                                        contactPersRef.RelatedContact.
                                        LastName +
                                        " Title: " + contactPersRef.
                                        RelatedContact.Title + " Email: "
                                        + contactPersRef.RelatedContact.
                                        Email
                                        + "\n";
                                }
                            }                            
                        }                        
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
            
        }

        
        private void buttonCreateRefPerson_Click(object sender, 
            EventArgs e)
        {
            ResearchCollaborationData researchCollaborationData = null;
            try
            {
                using (researchCollaborationData = 
                    new ResearchCollaborationData())
                {
                    // There are four possible cases depending 
                    // whether contact and reference exist.

                    ObjectParameter paramContact = 
                        new ObjectParameter("p", textBoxEmail.Text);

                    if (!researchCollaborationData.ContactPerson.Where(
                        "it.Email = @p", paramContact).Any())
                    {
                        ObjectParameter paramReference = 
                            new ObjectParameter("p", 
                            webBrowser1.Document.Url.ToString());

                        if (!researchCollaborationData.Reference.Where(
                            "it.Locator = @p", paramReference).Any())
                        {
                            // Neither contact nor reference exist.
                            ContactPerson newContact = new ContactPerson();
                            newContact.ContactPersonID = Guid.NewGuid(); 
                            newContact.LastName = textBoxLastName.Text;
                            newContact.FirstName = textBoxFirstName.Text; 
                            newContact.Email = textBoxEmail.Text;

                            newContact.Title = 
                                textBoxTitlePosition.Text;

                            researchCollaborationData.AddToContactPerson(newContact);

                            Reference newReference = new Reference();
                            newReference.ReferenceID = Guid.NewGuid(); 
                            newReference.Locator = webBrowser1.Document.Url.ToString();

                            researchCollaborationData.AddToReference(newReference);

                            ContactPersonReference newLink = 
                                new ContactPersonReference();
                            newLink.ContactPersonRefID = Guid.NewGuid();

                            newLink.RelatedContact = newContact;
                            newLink.RelatedReference = newReference;
                            researchCollaborationData.AddToContactPersonReference(newLink);
                        }

                        else
                        {
                            // Reference exists but contact doesn't.
                            Reference reference = 
                                researchCollaborationData.Reference.
                                Where("it.Locator = @p", 
                                paramReference).First();

                            ContactPerson newContact = new ContactPerson();
                            newContact.ContactPersonID = Guid.NewGuid(); 
                            newContact.LastName = textBoxLastName.Text;
                            newContact.FirstName = textBoxFirstName.Text; 
                            newContact.Email = textBoxEmail.Text;

                            newContact.Title = 
                                textBoxTitlePosition.Text;

                            researchCollaborationData.AddToContactPerson(newContact);

                            ContactPersonReference newLink = new ContactPersonReference();
                            newLink.ContactPersonRefID = Guid.NewGuid();
                            newLink.RelatedContact = newContact;
                            newLink.RelatedReference = reference;
                            researchCollaborationData.AddToContactPersonReference(newLink);

                        }
                    }

                    else
                    {
                        // Contact exists but reference doesn't.
                        ObjectParameter paramReference = 
                            new ObjectParameter("p", 
                            webBrowser1.Document.Url.ToString());

                        if (!researchCollaborationData.Reference.Where(
                            "it.Locator = @p", paramReference).Any())
                        {
                            ContactPerson contact = 
                                researchCollaborationData.ContactPerson.
                                Where("it.Email = @p", 
                                paramContact).First();

                            Reference newReference = new Reference();
                            newReference.ReferenceID = Guid.NewGuid(); 
                            newReference.Locator = webBrowser1.Document.Url.ToString();

                            researchCollaborationData.AddToReference(newReference);

                            ContactPersonReference newLink = new ContactPersonReference();
                            newLink.ContactPersonRefID = Guid.NewGuid();

                            newLink.RelatedContact = contact;
                            newLink.RelatedReference = newReference;
                            researchCollaborationData.AddToContactPersonReference(newLink);

                        }

                        else
                        {
                            // Contact and reference both exist.
                            Reference reference = 
                                researchCollaborationData.Reference.
                                Where("it.Locator = @p", 
                                paramReference).First();

                            ContactPerson contact = 
                                researchCollaborationData.
                                ContactPerson.Where("it.Email = @p", 
                                paramContact).First();
                            
                            ContactPersonReference newLink = new ContactPersonReference();
                            newLink.ContactPersonRefID = Guid.NewGuid();
                            newLink.RelatedContact = contact;
                            newLink.RelatedReference = reference;
                            researchCollaborationData.AddToContactPersonReference(newLink);

                        }
                        
                    }

                    researchCollaborationData.SaveChanges();

                    researchCollaborationData.Connection.Close();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }

        }
        
        private void buttonFindRefPerson_Click(object sender, EventArgs e)
        {
            ResearchCollaborationData researchCollaborationData = null;
            try
            {
                using (researchCollaborationData = 
                    new ResearchCollaborationData())
                {
                    // Use parameters from LastName and 
                    // Email text boxes in search.
                    ObjectParameter emailParam = new ObjectParameter(
                        "email", textBoxEmail.Text);
                    ObjectParameter nameParam = new ObjectParameter(
                        "name", textBoxLastName.Text);
                    ObjectParameter[] objParams = { emailParam, 
                        nameParam };
                    
                    ObjectQuery<ContactPerson> query = 
                        researchCollaborationData.ContactPerson.Where(
                        "it.Email = @email OR it.LastName = @name", 
                        objParams );

                    if (query.Any())
                    {
                        textBoxAnnotationResults.Text = 
                            "Contact and associated reference documents:\n";

                        ContactPerson person = null;
                        query.FirstOrDefault(out person);

                        // Display contact information and 
                        // related references.
                        textBoxAnnotationResults.Text = 
                            textBoxAnnotationResults.Text +
                            person.FirstName + " " + person.LastName +
                            " Title: " + person.Title +
                            " Email address: " + person.Email;

                        ObjectParameter contactParam = 
                            new ObjectParameter("p", 
                            person.ContactPersonID);

                        foreach (ContactPersonReference contactReference
                            in researchCollaborationData.
                            ContactPersonReference.Where(
                            "it.RelatedContact.ContactPersonID = @p",
                            contactParam))
                        {
                            contactReference.RelatedReferenceReference.
                                Load();

                            textBoxAnnotationResults.Text = 
                                textBoxAnnotationResults.Text + "\n" +
                                contactReference.RelatedReference.Locator;
                        }
                    }                                        
                }
            }

            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }

        }

        private void textBoxUri_PreviewKeyDown(object sender, 
            PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Return))
                buttonNavigate_Click(this, System.EventArgs.Empty);
        }

        private void textBoxSearch_PreviewKeyDown(object sender, 
            PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Return))
                buttonSearch_Click(this, null);
        }

        private void textBoxAnnotationResults_LinkClicked(object sender, 
            LinkClickedEventArgs e)
        {
            // Display the Locator URL in Web browser.
            webBrowser1.Navigate(e.LinkText);
        }

        private void webBrowser1_DocumentCompleted(object sender, 
            WebBrowserDocumentCompletedEventArgs e)
        {
            textBoxUri.Text = webBrowser1.Document.Url.ToString();
        }

        private void textBoxEmail_MouseDoubleClick(object sender, 
            MouseEventArgs e)
        {
            ResearchCollaborationData researchCollaborationData = null;
            try
            {
                researchCollaborationData = 
                    new ResearchCollaborationData();

                textBoxAnnotationResults.Text = 
                    "Contacts and related references:";

                // Find and display all contacts and 
                // their related references.
                foreach (ContactPerson person in 
                    researchCollaborationData.ContactPerson)
                {
                    textBoxAnnotationResults.Text = 
                        textBoxAnnotationResults.Text + "\n\n" +
                        person.FirstName + " " + person.LastName + 
                        " Title: " + person.Title +
                        " Email address: " + person.Email;

                    ObjectParameter contactParam = new ObjectParameter(
                        "p", person.ContactPersonID);

                    foreach (ContactPersonReference contactReference in 
                        researchCollaborationData.ContactPersonReference.
                        Where("it.RelatedContact.ContactPersonID = @p",
                        contactParam))
                    {
                        contactReference.RelatedReferenceReference.Load();
                        textBoxAnnotationResults.Text = 
                            textBoxAnnotationResults.Text + 
                            "\n" + 
                            contactReference.RelatedReference.Locator;
                    }

                }     

            }

            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }

        }

        private void textBoxSearch_MouseDoubleClick(object sender,
            EventArgs e)
        {
            ResearchCollaborationData researchCollaborationData = null;
            try
            {
                researchCollaborationData = 
                    new ResearchCollaborationData();

                textBoxAnnotationResults.Text = 
                    "All references and related contacts:\n";

                foreach (ReferenceDescriptor refDescriptor in 
                    researchCollaborationData.ReferenceDescriptor)
                {

                    textBoxAnnotationResults.Text = 
                        textBoxAnnotationResults.Text + "\n" +
                        refDescriptor.Keyword + "\n" + 
                        refDescriptor.Annotation;
                    
                    refDescriptor.ReferenceReference.Load();

                    Reference reference = refDescriptor.Reference;

                    textBoxAnnotationResults.Text = 
                        textBoxAnnotationResults.Text + "\n" +
                        reference.Locator + "\n";

                    foreach (ContactPersonReference contactPersonRef in 
                        researchCollaborationData.ContactPersonReference)
                    {
                        if(contactPersonRef.Equals(reference))
                        {
                            contactPersonRef.RelatedContactReference.Load();

                            textBoxAnnotationResults.Text = 
                            textBoxAnnotationResults.Text + "\n" +
                            "Relevant Contact:";

                            textBoxAnnotationResults.Text = 
                            textBoxAnnotationResults.Text + "\n" +
                            contactPersonRef.RelatedContact.FirstName + " " + 
                            contactPersonRef.RelatedContact.LastName + " Title: " + 
                            contactPersonRef.RelatedContact.Title + " Email: " +
                            contactPersonRef.RelatedContact.Email + "\n";
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }

        }
        
    }
}

另請參閱

概念

附註和研究共同作業工具 (EDM 範例應用程式)
附註參考資料工具結構描述 (EDM 範例應用程式)