各製品の資料を入手。
詳細はこちら →Entity Framework 6 からBigCommerce のデータに連携
この記事は、Entity Framework のcode-first アプローチを使って、BigCommerce に接続する方法を説明します。Entity Framework 6 は.NET 4.5 以上で利用可能です。
最終更新日:2022-04-04
この記事で実現できるBigCommerce 連携のシナリオ
こんにちは!ウェブ担当の加藤です。マーケ関連のデータ分析や整備もやっています。
Entity Framework はobject-relational mapping フレームワークで、データをオブジェクトとして扱うために使われます。Visual Studio のADO.NET Entity Data Model ウィザードを実行するとEntity Model を作成できますが、このモデルファーストアプローチでは、データソースに変更があった場合やエンティティ操作をより制御したい場合は不都合があります。この記事では、CData ADO.NET Provider を使いコードファーストアプローチでBigCommerce にアクセスします。
- Visual Studio を起動し、新しいWindows Form アプリケーションを作成します。ここでは、.NET 4.5 のC# プロジェクトを使います。
- Visual Studio の [パッケージ マネージャー コンソール]から'Install-Package EntityFramework' コマンドを実行し、最新のEntity Framework をインストールします。
- プロジェクトのApp.config ファイルを修正して、BigCommerce Entity Framework 6 アセンブリおよびコネクションストリングへの参照を追加します。
BigCommerce 認証は標準のOAuth フローに基づいています。
Store ID の取得
BigCommerce Store に接続するには、StoreId が必要です。Store Id を確認するには、以下の手順に従ってください。
- BigCommerce アカウントにログインします。
- ホームページから「Advanced Settings」->「API Accounts」 を選択します。
- 「Create API Account」->「Create V2/V3 API Token」をクリックします。
- 画面にAPI Path という名前のテキストボックスが表示されます。
- テキストボックス内に、次の構造のURL が表示されます:https://api.bigcommerce.com/stores/{Store Id}/v3。
- 上記で示したように、Store Id は'stores/' と'/v3' パスパラメータの間にあります。
- Store Id を取得したら、「キャンセル」 をクリックするか、まだ持っていない場合はAPI Account の作成に進むことができます。
パーソナルアクセストークンの取得
加えて、自分のデータをテストおよびアクセスするには、個人用トークンを取得する必要があります。個人用トークンを取得する方法は次のとおりです。
- BigCommerce アカウントにログインします。
- ホームページから「Advanced Settings」->「API Accounts」 を選択します。
- 「Create API Account」->「Create V2/V3 API Token」をクリックします。
- アカウント名を入力します。
- 作成するAPI Account の「OAuth Scopes」を選択します。CData 製品 は"None" とマークされたデータにアクセスできません。また、"read-only" とマークされたデータを変更できません。
- 「保存」をクリックします。
BigCommerce への認証
次に、以下を設定してデータに接続できます。- StoreId:API Path テキストボックスから取得したStore ID に設定。
- OAuthAccessToken:生成したトークンに設定。
- InitiateOAuth:OFF に設定。
<configuration> ... <connectionStrings> <add name="BigCommerceContext" connectionString="Offline=False;OAuthClientId=YourClientId; OAuthClientSecret=YourClientSecret; StoreId='YourStoreID'; CallbackURL='http://localhost:33333'" providerName="System.Data.CData.BigCommerce" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.BigCommerce" type="System.Data.CData.BigCommerce.BigCommerceProviderServices, System.Data.CData.BigCommerce.Entities.EF6" /> </providers> <entityFramework> </configuration> </code>
- インストールディレクトリの[lib] > 4.0 サブフォルダにあるSystem.Data.CData.BigCommerce.Entities.EF6.dll を設定し、プロジェクトを作成してEntity Framework 6 を使うためのセットアップを完了します。
- この時点でプロジェクトを作成し、すべてが正しく動作していることを確認してください。これで、Entity Framework を使ってコーディングを開始できます。
- プロジェクトに新しい.cs ファイルを追加し、そこにクラスを追加します。これがデータベースのコンテキストとなり、DbContext クラスを拡張します。この例では、クラス名はBigCommerceContext です。以下のサンプルコードは、OnModelCreating メソッドをオーバーライドして次の変更を加えます:
- PluralizingTableNameConvention をModelBuilder Conventions から削除。
- MigrationHistory テーブルへのリクエストを削除。
using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Entity.ModelConfiguration.Conventions; class BigCommerceContext :DbContext { public BigCommerceContext() { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // To remove the requests to the Migration History table Database.SetInitializer<BigCommerceContext>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } }
- もう一つ.cs ファイルを作成し、ファイル名を呼び出そうとしているBigCommerce のエンティティ、例えばCustomers にします。このファイルでは、エンティティとエンティティ設定の両方を定義します。以下に例を示します。
using System.Data.Entity.ModelConfiguration; using System.ComponentModel.DataAnnotations.Schema; public class Customers { [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public System.String Id { get; set; } public System.String FirstName { get; set; } } public class CustomersMap :EntityTypeConfiguration<Customers> { public CustomersMap() { this.ToTable("Customers"); this.HasKey(Customers => Customers.Id); this.Property(Customers => Customers.FirstName); } }
- エンティティの作成が済んだので、コンテキストクラスにエンティティを追加します:
public DbSet<Customers> Customers { set; get; }
- コンテキストとエンティティの作成が完了したら、別クラスでデータをクエリできます。例:
BigCommerceContext context = new BigCommerceContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.Customers select line;