Entity Framework はobject-relational mapping フレームワークで、データをオブジェクトとして扱うために使われます。Visual Studio のADO.NET Entity Data Model ウィザードを実行するとEntity Model を作成できますが、このモデルファーストアプローチでは、データソースに変更があった場合やエンティティ操作をより制御したい場合は不都合があります。この記事では、CData ADO.NET Provider を使いコードファーストアプローチでSage 200 にアクセスします。
- Visual Studio を起動し、新しいWindows Form アプリケーションを作成します。ここでは、.NET 4.5 のC# プロジェクトを使います。
- Visual Studio の [パッケージ マネージャー コンソール]から'Install-Package EntityFramework' コマンドを実行し、最新のEntity Framework をインストールします。
- プロジェクトのApp.config ファイルを修正して、Sage 200 Entity Framework 6 アセンブリおよびコネクションストリングへの参照を追加します。
- Schema: Determines which Sage 200 edition you are connecting to. Specify either StandardUK or ProfessionalUK.
- Subscription Key: Provides access to the APIs that are used to establish a connection. You will first need to log into the Sage 200 API website and subscribe to the API edition that matches your account. You can do so here: https://developer.columbus.sage.com/docs/services/api/uk. Afterwards, the subscription key may be found in your profile after logging into Sage 200.
<configuration>
... <connectionStrings>
<add name="Sage200Context" connectionString="Offline=False;SubscriptionKey=12345;Schema=StandardUK;" providerName="System.Data.CData.Sage200" />
</connectionStrings>
<entityFramework>
<providers>
... <provider invariantName="System.Data.CData.Sage200" type="System.Data.CData.Sage200.Sage200ProviderServices, System.Data.CData.Sage200.Entities.EF6" />
</providers>
<entityFramework>
</configuration>
</code>
- インストールディレクトリの[lib] > 4.0 サブフォルダにあるSystem.Data.CData.Sage200.Entities.EF6.dll を設定し、プロジェクトを作成してEntity Framework 6 を使うためのセットアップを完了します。
-
この時点でプロジェクトを作成し、すべてが正しく動作していることを確認してください。これで、Entity Framework を使ってコーディングを開始できます。
- プロジェクトに新しい.cs ファイルを追加し、そこにクラスを追加します。これがデータベースのコンテキストとなり、DbContext クラスを拡張します。この例では、クラス名はSage200Context です。以下のサンプルコードは、OnModelCreating メソッドをオーバーライドして次の変更を加えます:
- PluralizingTableNameConvention をModelBuilder Conventions から削除。
- MigrationHistory テーブルへのリクエストを削除。
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class Sage200Context :DbContext {
public Sage200Context() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { // To remove the requests to the Migration History table
Database.SetInitializer<Sage200Context>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
- もう一つ.cs ファイルを作成し、ファイル名を呼び出そうとしているSage 200 のエンティティ、例えばBanks にします。このファイルでは、エンティティとエンティティ設定の両方を定義します。以下に例を示します。
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;
public class Banks {
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public System.String Id { get; set; }
public System.String Id { get; set; }
}
public class BanksMap :EntityTypeConfiguration<Banks> {
public BanksMap() {
this.ToTable("Banks");
this.HasKey(Banks => Banks.Id);
this.Property(Banks => Banks.Id);
}
}
- エンティティの作成が済んだので、コンテキストクラスにエンティティを追加します:
public DbSet<Banks> Banks { set; get; }
- コンテキストとエンティティの作成が完了したら、別クラスでデータをクエリできます。例:
Sage200Context context = new Sage200Context();
context.Configuration.UseDatabaseNullSemantics = true;
var query = from line in context.Banks select line;