ADO.NET Provider for Airtable

Build 22.0.8479

コードファーストアプローチ

本製品 からモデルを確認するもう1つの方法は、モデルクラスを記述することです。これは、Entity Framework におけるコードファーストアプローチで、アプリケーションで使用するデータモデルをより詳細に制御することができます。

Entity Framework のインストール

Entity Framework をインストールするか、選択したバージョンのEntity Framework で必要とされるアセンブリへの参照を追加します。Entity Framework 6 の使用については、EF 6の使用 を参照してください。本製品 に付属するすべてのアセンブリの詳細については、インストールされるアセンブリ を参照してください。

プロバイダーの登録

App.Config またはWeb.config に接続文字列を追加します。多くの場合、connectionStrings ノードはルート構成ノードのconfigSection ノードの直下にあります。

<configuration>
  ...
  <connectionStrings>
    <add name="AirtableContext" connectionString="APIKey=keymz3adb53RqsU;BaseId=appxxN2fe34r3rjdG7;TableNames=TableA,TableB,TableC;ViewNames=TableA.ViewA,TableA.ViewB,..,TableX.ViewY;" providerName="System.Data.CData.Airtable" />
  </connectionStrings>
  ...
</configuration>

コンテキストクラスの作成

最初に、Airtable コンテキストクラスを作成します。これは、DbContext を拡張し、データソース内のテーブルを示すDbSet プロパティを公開する基本オブジェクトです。次に、OnModelCreating メソッドをオーバーライドして、DbContext クラスの一部のデフォルト機能をオーバーライドします。次のコードでは、これらのプロパティについて説明しています。

C#

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;

class AirtableContext : DbContext {
  public AirtableContext() { }

  public DbSet<SampleTable_1> SampleTable_1 { set; get; }
  
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    // To remove the requests to the Migration History table
    Database.SetInitializer<AirtableContext>(null);
    // To remove the plural names
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();    
    //For versions of EF before 6.0, uncomment the following line to remove calls to EdmTable, a metadata table
    //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
	//To remove the default schema "dbo" 
	modelBuilder.HasDefaultSchema("");
  }  
}

VB.NET

Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure
Imports System.Data.Entity.ModelConfiguration.Conventions

Class AirtableContext 
Inherits DbContext 

  Public Sub New()
  End Sub
  
  public property SampleTable_1 As DbSet(Of SampleTable_1)
  
 Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
    ' To remove the requests to the Migration History table
    Database.SetInitializer(of AirtableContext)(Nothing)
    ' To remove the plural names
    modelBuilder.Conventions.Remove(Of PluralizingTableNameConvention)
    ' For versions of EF before 6.0, uncomment the following line to remove calls to EdmTable, a metadata table
    ' modelBuilder.Conventions.Remove(Of IncludeMetadataConvention)()
	' To remove the default schema "dbo" 
	modelBuilder.HasDefaultSchema("")
 End Sub

End Class

テーブルモデルの作成

最後に、コンテキストクラスのDbSet プロパティで定義された各テーブルのクラスを定義します。テーブルクラスには、そのテーブルの各フィールドに対応するプロパティのリストが含まれている必要があります。テーブルクラスの各プロパティの属性を設定するには、対応するマップクラスを定義する必要があります。

C#

using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema; //EF 6 and later
//using System.ComponentModel.DataAnnotations //For versions of EF before 6


[System.ComponentModel.DataAnnotations.Schema.Table("SampleTable_1")]
public class SampleTable_1  {
  [System.ComponentModel.DataAnnotations.Key]
  public System.String Id { get; set; }
  public System.String Column1 { get; set; }
}

VB.NET

Imports System.Data.Entity.ModelConfiguration
Imports System.ComponentModel.DataAnnotations.Schema 'EF 6 and later
'Imports System.ComponentModel.DataAnnotations 'For versions of EF before 6


<System.ComponentModel.DataAnnotations.Schema.Table("SampleTable_1")>
public class SampleTable_1  
  <System.ComponentModel.DataAnnotations.Key>
  public property Id As System.String
  public property Column1 As System.String
End Class

コードでLINQ コマンドを実行する

これで、コードでLINQ を使用する準備が整いました。ファイルで必ず (c#)"using System.Linq"/(VB.Net)"Imports System.Linq" を宣言してください。

C#

AirtableContext ents = new AirtableContext();
var SampleTable_1Query = from SampleTable_1 in ents.SampleTable_1
                   orderby SampleTable_1.Column1
                   select SampleTable_1;

VB.Net

    Dim ents As AirtableContext = New AirtableContext()
    Dim SampleTable_1Query = From SampleTable_1 In ents.SampleTable_1
	Order By SampleTable_1.Column1
	Select SampleTable_1

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8479