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

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

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

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

    Adobe Commerce はOAuth 1 認証標準を使用します。Adobe Commerce REST API に接続するには、Adobe Commerce システムにアプリを登録してOAuthClientId、OAuthClientSecret、およびCallbackURL 接続プロパティの値を取得する必要があります。 OAuth 値を取得して接続するには、ヘルプドキュメントの「はじめに」を参照してください。

    また、Adobe Commerce システムへのURL を提供する必要があります。URL は、Adobe Commerce REST API を顧客として使用しているか管理者として使用しているかによって異なります。

    • Customer: Adobe Commerce を顧客として使用するには、事前にAdobe Commerce のホームページで顧客アカウントを作成します。これを行うには、「アカウント」->「登録」をクリックします。それからURL 接続プロパティをAdobe Commerce システムのエンドポイントに設定します。

    • Administrator: Adobe Commerce を管理者として使用するには、代わりにCustomAdminPath を設定します。この値は、「Admin」メニューの「Advanced」設定で取得できます。「System」->「Configuration」->「Advanced」->「Admin」->「Admin Base URL」を選択することでアクセスできます。

      このページ上の「Use Custom Admin Path」設定がYES に設定されている場合、値は「Custom Admin Path」テキストボックス内にあります。それ以外の場合は、CustomAdminPath 接続プロパティをデフォルト値の"admin" に設定します。

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

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

Adobe Commerce Data Provider の無料トライアルをダウンロードしてお試しください:

 ダウンロード

詳細:

Adobe Commerce Icon Adobe Commerce ADO.NET Provider お問い合わせ

Adobe Commerce データ連携のパワフルな.NET アプリケーションを素早く作成して配布。