Annotation Research ツールのアプリケーション コード (EDM サンプル アプリケーション)

Annotation Research ツール エンティティ データ モデル (EDM) アプリケーションは、Windows フォームと Visual Studio ツールボックスのコントロールを使用して実装されています。

このアプリケーションでは、WebBrowser コントロールを使用して Web 参照を表示します。ユーザーは、さまざまなテキスト ボックスを使用して、注釈や連絡先情報となる情報を入力できます。Reference インスタンスの Locator プロパティに割り当てられる URL 文字列は、ReferenceDescriptor クラスのインスタンスが作成されたときに WebBrowser に表示されていたページのドキュメント オブジェクトから取得されます。すべての処理は、ユーザー入力に応答するイベント ハンドラによって実行されます。UI の画面については、「Annotation and Research Collaboration ツール (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>

[!メモ]

この接続文字列は、複数のアクティブ結果セットを true に設定します。これは、別のデータ リーダーが同じ接続上で既に開いている場合にアソシエーションに対して Load メソッドを呼び出すために必要になります。

ストレージへの接続の初期化

上の exe.config ファイルが実行可能ファイルのスコープに置かれていて、System.Data.Entity.dll への参照がプロジェクトに含まれていれば、次の 1 行のコードだけで、このアプリケーションで使用するデータへの接続を開くことができます。

    ResearchCollaborationData researchCollaborationData =
                                    new ResearchCollaborationData();

この初期化は、このアプリケーションでデータのクエリや更新のために使用されるすべてのイベント ハンドラで使用されます。接続を閉じるには次のステートメントを使用します。

    researchCollaborationData.Connection.Close();

参照記述子の作成

次のコードは、ReferenceDescriptor クラスのインスタンスを作成してストレージに保存するコードです。このシーケンスでは、WebBrowser ウィンドウ内のページが既に Reference オブジェクトとして存在しているかどうかを確認しています。存在しない場合は、ReferenceDescriptor を使用して Reference の新しいインスタンスを作成します。Reference がまだストレージにない場合は、AddToReference を呼び出して追加します (researchCollaborationData.AddToReferecne(newReference))。ReferenceDescriptor をストレージに追加するには、researchCollaborationData.AddToReferenceDescriptor(newReferenceDescriptor) を呼び出します。

ReferenceDescriptor は、RefDescriptors という名前の Reference のナビゲーション プロパティによって表されるコレクションにも追加されています (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 の一方または両方が既に存在するかどうかに応じて 4 つのパターンがあります。前の ReferenceDescriptor を作成するケースでは常に新しい記述子インスタンスが作成されますが、ContactPersonReference に関連付けるプロセスでは、その連絡先が既に ContactPerson のインスタンスによって表されているかどうかを確認します。

ReferenceDescriptor は 1 つの Reference にのみ関連付けられるのに対し、ContactPerson は多数の Reference に関連付けることができます。このリレーションシップをサポートする多対多の Association の実装の詳細については、「Annotation Research ツールのスキーマ (EDM サンプル アプリケーション)」を参照してください。

次のイベント ハンドラには 4 つのコード パスがあります。すべてのコード パスで、ContactPerson エンティティのインスタンスが Reference エンティティのインスタンスに関連付けられます。1 つ目のパターンでは、新しい ContactPerson と新しい Reference の両方が作成され、AddToContactPersonAddToReference を使用してストレージに追加されます。これは多対多のアソシエーションなので、新しいリンク エンティティをインスタンス化し、そのナビゲーション プロパティを初期化して、ContactPersonReference のインスタンスを関連付ける必要もあります。

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

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

このコード シーケンスでは、ContactPersonReference という名前のリンク エンティティをインスタンス化しています。このリンク エンティティには、アソシエーションの両側の End (RelatedContactRelatedReference) を表すナビゲーション プロパティがあります。これらのプロパティに、このコード パスで作成した ContactPersonReference の新しいインスタンスが割り当てられています。ContactPersonReference の一方または両方が既に存在するその他のケースでは、オブジェクト クエリによって特定された既存のインスタンスがこの割り当てに使用されます。

次のコードでは、この新しいリンク エンティティをストレージに追加して、すべての新しいエンティティ インスタンスを保存しています。

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

次のシーケンスは、可能な 4 つのコード パスのうちの 2 つを示しています。完全なイベント ハンドラについては、このトピックの最後にあるイベント ハンドラの完全なコードで、メソッド 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 はそれぞれ 1 つの注釈を表します。ReferenceDescriptor の多数のインスタンスで 1 つの Web ページについて記述することはできますが、各インスタンスはそれぞれ 1 つのページにしか関連付けられません。

Web 参照に注釈を付けるには、ReferenceDescriptor インスタンスを前のコードのように追加します。ReferenceDescriptor インスタンスの Annotation プロパティと Keyword プロパティを検索することにより、有用な 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());
            }
            
        }

このコードは 2 つの基本的なシーケンスで構成されています。1 つは検索文字列の List<T> を作成するシーケンス、もう 1 つは、ReferenceDescriptor インスタンスの Keyword プロパティと Annotation プロパティで一致するテキストのクエリを実行するシーケンスです。

検索文字列のリストは、入力テキストを解析することによって作成されています。クエリはパラメータ化された ObjectQuery で、ループの中で使用されています。ループでは、各検索文字列をこのクエリに挿入して、ストレージ内の ReferenceDescriptor インスタンスの Keyword プロパティと Annotation プロパティに割り当てられているテキストと比較しています。ObjectParameter クラスと ObjectQuery クラスは次のコードでインスタンス化されます。

               // 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 を読み込みます。その際には、その NavigationProperty と、この目的のために用意されている Association を使用します。次のコードでは、Reference を読み込み、その Locator プロパティを読み取って、Reference への URL を結果のテキスト ボックスにリンクとして表示しています。

                           refDescriptor.ReferenceReference.Load();

                            Reference reference = refDescriptor.Reference;

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

最後に、Reference に関連付けられている連絡先の情報を検索して表示します。LinkTable_Reference の静的メソッド Association を使用して、Reference に関連付けられているリンク テーブル エンティティを特定し、ContactPersonReference のインスタンス (Reference を含むリンク テーブル エンティティ) をループで処理して、関連付けられている ContactPerson のプロパティを読み込んで表示します。FirstNameLastNameTitle、および Email の各プロパティが、前のコードで検索した ReferenceAnnotation および Locator のテキストと組み合わせて表示されます。

                            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 を初期化して、Research Collaboration データ モデルに基づいて作成されたデータを検索および更新するために使用されている、すべてのイベント ハンドラが含まれています。

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());
            }

        }
        
    }
}

参照

概念

Annotation and Research Collaboration ツール (EDM サンプル アプリケーション)
Annotation Research ツールのスキーマ (EDM サンプル アプリケーション)