批注研究工具应用程序代码(EDM 示例应用程序)

批注研究工具 实体数据模型 (EDM) 应用程序是通过使用 Windows 窗体和 Visual Studio 工具箱中的控件实现的。

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注意

该连接字符串将多个活动结果集设置为 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)

该应用程序未实现用来创建没有关联 ReferenceDescriptorReference 实例的方法。

        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。使用 AddToContactPersonAddToReference 将新的 ContactPerson 和新的 Reference 添加到存储中。因为这是多对多关联,所以必须实例化一个新的链接实体,并对其导航属性进行实例化,以便连接 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 都进入一个循环,该循环使用相关 ReferenceNavigationProperty 和为此目的设计的 Association 加载该相关引用。下面的代码段加载 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 示例应用程序)