Entity Framework 6 からJD Edwards のデータに連携
Entity Framework はobject-relational mapping フレームワークで、データをオブジェクトとして扱うために使われます。Visual Studio のADO.NET Entity Data Model ウィザードを実行するとEntity Model を作成できますが、このモデルファーストアプローチでは、データソースに変更があった場合やエンティティ操作をより制御したい場合は不都合があります。この記事では、CData ADO.NET Provider を使いコードファーストアプローチでJD Edwards にアクセスします。
- Visual Studio を起動し、新しいWindows Form アプリケーションを作成します。ここでは、.NET 4.5 のC# プロジェクトを使います。
- Visual Studio の [パッケージ マネージャー コンソール]から'Install-Package EntityFramework' コマンドを実行し、最新のEntity Framework をインストールします。
- プロジェクトのApp.config ファイルを修正して、JD Edwards Entity Framework 6 アセンブリおよびコネクションストリングへの参照を追加します。
ドライバーは Application Interface Services(AIS)Server を経由して JD Edwards に接続します。以下の接続プロパティを設定します。
- URL:AIS Server のベース HTTPS URL(例:https://jde-ais.example.com:8300)。
- User:JD Edwards のユーザー名。
- Password:JD Edwards のパスワード。
- Environment(オプション):使用する JD Edwards の環境(例:本番環境なら PD920、開発環境なら DV920)。指定しない場合は、AIS Server のデフォルト環境が使用されます。
- Role(オプション):セッションで使用する JD Edwards のロール。指定しない場合は、AIS Server のデフォルトロールが使用されます。
- DeviceName(オプション):接続元のデバイスまたはアプリケーションの識別子。AIS Server での監査やログ記録に使用されます。
- Jasserver(オプション):リクエストのルーティング先となる Java Application Server(JAS)インスタンス。クラスタ環境で便利です。
公開するデータの選択
JD Edwards ではテーブルとビジネスビューが System Code ごとに整理されており、ドライバーは各 System Code をそれぞれ個別のスキーマとして公開します。利用できるスキーマは、以下のプロパティで制御できます。
- DataModel:System Code をスキーマとして公開する ERP モジュール(カンマ区切りで複数指定可)。接続先インスタンスのすべての System Code を公開する場合は All を指定します。デフォルトは FinancialManagement です。
- SystemCodes:DataModel の System Code に加えて公開したい System Code のカンマ区切りリスト(例:42,43)。
接続時、ドライバーは認証情報を AIS Server に送信してセッショントークンを取得し、キャッシュします。セッションの有効期限が切れる前に、新しいトークンが自動でリクエストされます。
<configuration> ... <connectionStrings> <add name="JDEdwardsContext" connectionString="Offline=False;URL=https://your-jde-environment-app.example.com;User=admin;Password=myPassword;" providerName="System.Data.CData.JDEdwards" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.JDEdwards" type="System.Data.CData.JDEdwards.JDEdwardsProviderServices, System.Data.CData.JDEdwards.Entities.EF6" /> </providers> <entityFramework> </configuration> </code> - インストールディレクトリの[lib] > 4.0 サブフォルダにあるSystem.Data.CData.JDEdwards.Entities.EF6.dll を設定し、プロジェクトを作成してEntity Framework 6 を使うためのセットアップを完了します。
- この時点でプロジェクトを作成し、すべてが正しく動作していることを確認してください。これで、Entity Framework を使ってコーディングを開始できます。
- プロジェクトに新しい.cs ファイルを追加し、そこにクラスを追加します。これがデータベースのコンテキストとなり、DbContext クラスを拡張します。この例では、クラス名はJDEdwardsContext です。以下のサンプルコードは、OnModelCreating メソッドをオーバーライドして次の変更を加えます:
- PluralizingTableNameConvention をModelBuilder Conventions から削除。
- MigrationHistory テーブルへのリクエストを削除。
using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Entity.ModelConfiguration.Conventions; class JDEdwardsContext :DbContext { public JDEdwardsContext() { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // To remove the requests to the Migration History table Database.SetInitializer<JDEdwardsContext>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } - もう一つ.cs ファイルを作成し、ファイル名を呼び出そうとしているJD Edwards のエンティティ、例えばAccountsPayable.AccountLedger にします。このファイルでは、エンティティとエンティティ設定の両方を定義します。以下に例を示します。
using System.Data.Entity.ModelConfiguration; using System.ComponentModel.DataAnnotations.Schema; public class AccountsPayable.AccountLedger { [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public System.String Id { get; set; } public System.String DocumentNumber { get; set; } } public class AccountsPayable.AccountLedgerMap :EntityTypeConfiguration<AccountsPayable.AccountLedger> { public AccountsPayable.AccountLedgerMap() { this.ToTable("AccountsPayable.AccountLedger"); this.HasKey(AccountsPayable.AccountLedger => AccountsPayable.AccountLedger.Id); this.Property(AccountsPayable.AccountLedger => AccountsPayable.AccountLedger.DocumentNumber); } } - エンティティの作成が済んだので、コンテキストクラスにエンティティを追加します:
public DbSet<AccountsPayable.AccountLedger> AccountsPayable.AccountLedger { set; get; } - コンテキストとエンティティの作成が完了したら、別クラスでデータをクエリできます。例:
JDEdwardsContext context = new JDEdwardsContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.AccountsPayable.AccountLedger select line;