Entity Framework 6 からOracle Service Cloud のデータに連携

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

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

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

    Oracle Service Cloud への認証には、以下を設定する必要があります。

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

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

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

 ダウンロード

詳細:

Oracle Service Cloud Icon Oracle Service Cloud ADO.NET Provider お問い合わせ

Oracle Service Cloud データと連携するパワフルな.NET アプリケーションを短時間・低コストで作成して配布できます。