各製品の資料を入手。
詳細はこちら →Entity Framework 6 からPresto のデータに連携
この記事は、Entity Framework のcode-first アプローチを使って、Presto に接続する方法を説明します。Entity Framework 6 は.NET 4.5 以上で利用可能です。
最終更新日:2022-04-04
この記事で実現できるPresto 連携のシナリオ
こんにちは!ウェブ担当の加藤です。マーケ関連のデータ分析や整備もやっています。
Entity Framework はobject-relational mapping フレームワークで、データをオブジェクトとして扱うために使われます。Visual Studio のADO.NET Entity Data Model ウィザードを実行するとEntity Model を作成できますが、このモデルファーストアプローチでは、データソースに変更があった場合やエンティティ操作をより制御したい場合は不都合があります。この記事では、CData ADO.NET Provider を使いコードファーストアプローチでPresto にアクセスします。
- Visual Studio を起動し、新しいWindows Form アプリケーションを作成します。ここでは、.NET 4.5 のC# プロジェクトを使います。
- Visual Studio の [パッケージ マネージャー コンソール]から'Install-Package EntityFramework' コマンドを実行し、最新のEntity Framework をインストールします。
- プロジェクトのApp.config ファイルを修正して、Presto Entity Framework 6 アセンブリおよびコネクションストリングへの参照を追加します。
Presto への接続には、まずはServer およびPort を接続プロパティとして設定します。それ以外の追加項目は接続方式によって異なります。
TLS/SSL を有効化するには、UseSSL をTRUE に設定します。
LDAP で認証
LDAP で認証するには、次の接続プロパティを設定します:
- AuthScheme: LDAP に設定。
- User: LDAP で接続するユーザー名。
- Password: LDAP で接続するユーザーのパスワード。
Kerberos 認証
KERBEROS 認証を使う場合には、以下を設定します:
- AuthScheme: KERBEROS に設定。
- KerberosKDC: 接続するユーザーのKerberos Key Distribution Center (KDC) サービス。
- KerberosRealm: 接続するユーザーのKerberos Realm 。
- KerberosSPN: Kerberos Domain Controller のService Principal Name。
- KerberosKeytabFile: Kerberos principals とencrypted keys を含むKeytab file。
- User: Kerberos のユーザー。
- Password: Kerberos で認証するユーザーのパスワード。
<configuration> ... <connectionStrings> <add name="PrestoContext" connectionString="Offline=False;Server=127.0.0.1;Port=8080;" providerName="System.Data.CData.Presto" /> </connectionStrings> <entityFramework> <providers> ... <provider invariantName="System.Data.CData.Presto" type="System.Data.CData.Presto.PrestoProviderServices, System.Data.CData.Presto.Entities.EF6" /> </providers> <entityFramework> </configuration> </code>
- インストールディレクトリの[lib] > 4.0 サブフォルダにあるSystem.Data.CData.Presto.Entities.EF6.dll を設定し、プロジェクトを作成してEntity Framework 6 を使うためのセットアップを完了します。
- この時点でプロジェクトを作成し、すべてが正しく動作していることを確認してください。これで、Entity Framework を使ってコーディングを開始できます。
- プロジェクトに新しい.cs ファイルを追加し、そこにクラスを追加します。これがデータベースのコンテキストとなり、DbContext クラスを拡張します。この例では、クラス名はPrestoContext です。以下のサンプルコードは、OnModelCreating メソッドをオーバーライドして次の変更を加えます:
- PluralizingTableNameConvention をModelBuilder Conventions から削除。
- MigrationHistory テーブルへのリクエストを削除。
using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Entity.ModelConfiguration.Conventions; class PrestoContext :DbContext { public PrestoContext() { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // To remove the requests to the Migration History table Database.SetInitializer<PrestoContext>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } }
- もう一つ.cs ファイルを作成し、ファイル名を呼び出そうとしているPresto のエンティティ、例えば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 FirstName { get; set; } } public class CustomerMap :EntityTypeConfiguration<Customer> { public CustomerMap() { this.ToTable("Customer"); this.HasKey(Customer => Customer.Id); this.Property(Customer => Customer.FirstName); } }
- エンティティの作成が済んだので、コンテキストクラスにエンティティを追加します:
public DbSet<Customer> Customer { set; get; }
- コンテキストとエンティティの作成が完了したら、別クラスでデータをクエリできます。例:
PrestoContext context = new PrestoContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.Customer select line;