製品をチェック

無償トライアル:

無償トライアルへ

製品の情報と無償トライアルへ:

Azure Analysis Services ADO.NET Provider

Azure Analysis Services データに連携する.NET アプリケーションを素早く、簡単に開発できる便利なドライバー。

データ連携でお困りですか?

お問い合わせ

Entity Framework 6 からAzure Analysis Services データに連携


この記事は、Entity Framework のcode-first アプローチを使って、Azure Analysis Services に接続する方法を説明します。Entity Framework 6 は.NET 4.5 以上で利用可能です。


azureanalysisservices ロゴ画像
ado ロゴ画像

ADO.NET

Entity Framework ロゴ画像


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

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

    To connect to Azure Analysis Services, set the Url property to a valid server, for instance, asazure://southcentralus.asazure.windows.net/server, in addition to authenticating. Optionally, set Database to distinguish which Azure database on the server to connect to.

    Azure Analysis Services uses the OAuth authentication standard. OAuth requires the authenticating user to interact with Azure Analysis Services using the browser. You can connect without setting any connection properties for your user credentials. See the Help documentation for more information.

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