Entity Framework 6 からFreshteam のデータに連携

加藤龍彦
加藤龍彦
デジタルマーケティング
この記事は、Entity Framework のcode-first アプローチを使って、Freshteam に接続する方法を説明します。Entity Framework 6 は.NET 4.5 以上で利用可能です。



Entity Framework はobject-relational mapping フレームワークで、データをオブジェクトとして扱うために使われます。Visual Studio のADO.NET Entity Data Model ウィザードを実行するとEntity Model を作成できますが、このモデルファーストアプローチでは、データソースに変更があった場合やエンティティ操作をより制御したい場合は不都合があります。この記事では、CData ADO.NET Provider を使いコードファーストアプローチでFreshteam にアクセスします。

  1. Visual Studio を起動し、新しいWindows Form アプリケーションを作成します。ここでは、.NET 4.5 のC# プロジェクトを使います。
  2. Visual Studio の [パッケージ マネージャー コンソール]から'Install-Package EntityFramework' コマンドを実行し、最新のEntity Framework をインストールします。
  3. プロジェクトのApp.config ファイルを修正して、Freshteam Entity Framework 6 アセンブリおよびコネクションストリングへの参照を追加します。

    まず、Profile 接続プロパティをディスク上の Freshteam プロファイルの場所に設定します(例:C:\profiles\Freshteam.apip)。次に、ProfileSettings 接続プロパティを Freshteam の接続文字列に設定します(以下を参照)。

    Freshteam API プロファイル設定

    Freshteam アカウント画面右上の API Key セクションで API キーを確認できます。AccountName は Freshteam URL のサブドメインです。

    
    
    <configuration>
       ... 
      <connectionStrings>
        <add name="APIContext" connectionString="Offline=False;Profile=C:\profiles\Freshteam.apip;ProfileSettings='APIKey=your_api_key;AccountName=your_account_name';" providerName="System.Data.CData.API" />
      </connectionStrings>
      <entityFramework>
        <providers>
           ... <provider invariantName="System.Data.CData.API" type="System.Data.CData.API.APIProviderServices, System.Data.CData.API.Entities.EF6" />
        </providers>
      <entityFramework>
    </configuration>
    </code>
    
  4. インストールディレクトリの[lib] > 4.0 サブフォルダにあるSystem.Data.CData.API.Entities.EF6.dll を設定し、プロジェクトを作成してEntity Framework 6 を使うためのセットアップを完了します。
  5. この時点でプロジェクトを作成し、すべてが正しく動作していることを確認してください。これで、Entity Framework を使ってコーディングを開始できます。
  6. プロジェクトに新しい.cs ファイルを追加し、そこにクラスを追加します。これがデータベースのコンテキストとなり、DbContext クラスを拡張します。この例では、クラス名はAPIContext です。以下のサンプルコードは、OnModelCreating メソッドをオーバーライドして次の変更を加えます:
    • PluralizingTableNameConvention をModelBuilder Conventions から削除。
    • MigrationHistory テーブルへのリクエストを削除。
    
    
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Entity.ModelConfiguration.Conventions;
    class APIContext :DbContext {
    	public APIContext() { }
    	protected override void OnModelCreating(DbModelBuilder modelBuilder) {  // To remove the requests to the Migration History table
    		Database.SetInitializer<APIContext>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    	}
    }
    
  7. もう一つ.cs ファイルを作成し、ファイル名を呼び出そうとしているFreshteam のエンティティ、例えばBranches にします。このファイルでは、エンティティとエンティティ設定の両方を定義します。以下に例を示します。
    
    
    using System.Data.Entity.ModelConfiguration;
    using System.ComponentModel.DataAnnotations.Schema;
    public class Branches {
    	[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    	public System.String Id { get; set; }
    	public System.String Id { get; set; }
    }
    public class BranchesMap :EntityTypeConfiguration<Branches> {
    	public BranchesMap() {
    		this.ToTable("Branches");
    		this.HasKey(Branches => Branches.Id);
    		this.Property(Branches => Branches.Id);
    	}
    }
    
  8. エンティティの作成が済んだので、コンテキストクラスにエンティティを追加します:
    
    
    public DbSet<Branches> Branches { set; get; }
    
  9. コンテキストとエンティティの作成が完了したら、別クラスでデータをクエリできます。例:
    
    APIContext context = new APIContext();
    context.Configuration.UseDatabaseNullSemantics = true;
    var query = from line in context.Branches select line;
    

はじめる準備はできましたか?

API Driver で Freshteam のライブデータに接続

Freshteam に接続