ノーコードでクラウド上のデータとの連携を実現。
詳細はこちら →CData
こんにちは!ウェブ担当の加藤です。マーケ関連のデータ分析や整備もやっています。
Entity Framework はobject-relational mapping フレームワークで、データをオブジェクトとして扱うために使われます。Visual Studio のADO.NET Entity Data Model ウィザードを実行するとEntity Model を作成できますが、このモデルファーストアプローチでは、データソースに変更があった場合やエンティティ操作をより制御したい場合は不都合があります。この記事では、CData ADO.NET Provider を使いコードファーストアプローチでGoogle Drive にアクセスします。
Google Drive はOAuth 認証標準を利用しています。各ユーザーやドメイン内のユーザーの代わりに、CData 製品がGoogle API にアクセスすることを許可できます。 接続すると、CData 製品はデフォルトブラウザでOAuth エンドポイントを開きます。ログインして、アプリケーションにアクセス許可を与えます。CData 製品がOAuth プロセスを完了します。
詳細はヘルプドキュメントを参照してください。
尚、CData 製品はGoogle Drive のファイルの一覧表示やユーザー管理情報の取得用です。Google Drive に保管されているExcel、CSV、JSON などのファイル内のデータを読み込みたい場合には、Excel Driver、CSV Driver、JSON Driver をご利用ください。
<configuration>
... <connectionStrings>
<add name="GoogleDriveContext" connectionString="Offline=False;" providerName="System.Data.CData.GoogleDrive" />
</connectionStrings>
<entityFramework>
<providers>
... <provider invariantName="System.Data.CData.GoogleDrive" type="System.Data.CData.GoogleDrive.GoogleDriveProviderServices, System.Data.CData.GoogleDrive.Entities.EF6" />
</providers>
<entityFramework>
</configuration>
</code>
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class GoogleDriveContext :DbContext {
public GoogleDriveContext() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { // To remove the requests to the Migration History table
Database.SetInitializer<GoogleDriveContext>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;
public class Files {
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public System.String Id { get; set; }
public System.String Name { get; set; }
}
public class FilesMap :EntityTypeConfiguration<Files> {
public FilesMap() {
this.ToTable("Files");
this.HasKey(Files => Files.Id);
this.Property(Files => Files.Name);
}
}
public DbSet<Files> Files { set; get; }
GoogleDriveContext context = new GoogleDriveContext();
context.Configuration.UseDatabaseNullSemantics = true;
var query = from line in context.Files select line;