各製品の資料を入手。
詳細はこちら →Entity Framework 6 からZoho Inventory のデータに連携
この記事は、Entity Framework のcode-first アプローチを使って、Zoho Inventory に接続する方法を説明します。Entity Framework 6 は.NET 4.5 以上で利用可能です。
最終更新日:2022-04-04
この記事で実現できるZoho Inventory 連携のシナリオ
こんにちは!ウェブ担当の加藤です。マーケ関連のデータ分析や整備もやっています。
Entity Framework はobject-relational mapping フレームワークで、データをオブジェクトとして扱うために使われます。Visual Studio のADO.NET Entity Data Model ウィザードを実行するとEntity Model を作成できますが、このモデルファーストアプローチでは、データソースに変更があった場合やエンティティ操作をより制御したい場合は不都合があります。この記事では、CData ADO.NET Provider を使いコードファーストアプローチでZoho Inventory にアクセスします。
- Visual Studio を起動し、新しいWindows Form アプリケーションを作成します。ここでは、.NET 4.5 のC# プロジェクトを使います。
- Visual Studio の [パッケージ マネージャー コンソール]から'Install-Package EntityFramework' コマンドを実行し、最新のEntity Framework をインストールします。
- プロジェクトのApp.config ファイルを修正して、Zoho Inventory Entity Framework 6 アセンブリおよびコネクションストリングへの参照を追加します。
Zoho Inventory 接続プロパティの取得・設定方法
以下の接続プロパティを使用して、取得されるZoho Inventory データを正確に絞り込むことができます。
- Region:サーバーURL のトップレベルドメイン(TLD)。アカウントが米国以外のドメインにある場合は、リージョンを適宜変更してください。
- OrganizationId(オプション):接続先の特定のZoho Inventory 組織に関連付けられたID。
- Organization Id の値が接続文字列で指定されていない場合、ドライバーは利用可能なすべての組織を自動的に取得し、最初のOrganization Id をデフォルトとして選択します。
Zoho Inventory への認証
ドライバーはOAuth を使用して認証を行います。認証方法は、ヘルプドキュメントの「はじめに」セクションを参照してください。
<configuration> ... <connectionStrings> <add name="ZohoInventoryContext" connectionString="Offline=False;OrganizationId=YourOrganizationId;AccountsServer=YourAccountServerURL;" providerName="System.Data.CData.ZohoInventory" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.ZohoInventory" type="System.Data.CData.ZohoInventory.ZohoInventoryProviderServices, System.Data.CData.ZohoInventory.Entities.EF6" /> </providers> <entityFramework> </configuration> </code>
- インストールディレクトリの[lib] > 4.0 サブフォルダにあるSystem.Data.CData.ZohoInventory.Entities.EF6.dll を設定し、プロジェクトを作成してEntity Framework 6 を使うためのセットアップを完了します。
- この時点でプロジェクトを作成し、すべてが正しく動作していることを確認してください。これで、Entity Framework を使ってコーディングを開始できます。
- プロジェクトに新しい.cs ファイルを追加し、そこにクラスを追加します。これがデータベースのコンテキストとなり、DbContext クラスを拡張します。この例では、クラス名はZohoInventoryContext です。以下のサンプルコードは、OnModelCreating メソッドをオーバーライドして次の変更を加えます:
- PluralizingTableNameConvention をModelBuilder Conventions から削除。
- MigrationHistory テーブルへのリクエストを削除。
using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Entity.ModelConfiguration.Conventions; class ZohoInventoryContext :DbContext { public ZohoInventoryContext() { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // To remove the requests to the Migration History table Database.SetInitializer<ZohoInventoryContext>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } }
- もう一つ.cs ファイルを作成し、ファイル名を呼び出そうとしているZoho Inventory のエンティティ、例えばContacts にします。このファイルでは、エンティティとエンティティ設定の両方を定義します。以下に例を示します。
using System.Data.Entity.ModelConfiguration; using System.ComponentModel.DataAnnotations.Schema; public class Contacts { [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public System.String Id { get; set; } public System.String Id { get; set; } } public class ContactsMap :EntityTypeConfiguration<Contacts> { public ContactsMap() { this.ToTable("Contacts"); this.HasKey(Contacts => Contacts.Id); this.Property(Contacts => Contacts.Id); } }
- エンティティの作成が済んだので、コンテキストクラスにエンティティを追加します:
public DbSet<Contacts> Contacts { set; get; }
- コンテキストとエンティティの作成が完了したら、別クラスでデータをクエリできます。例:
ZohoInventoryContext context = new ZohoInventoryContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.Contacts select line;