本記事では CData サポート担当からこんなことを聞かれたらどこを確認すべきか?という観点で、よく頂くお問合せ内容をご紹介します。
記事はこちら →Entity Framework はobject-relational mapping フレームワークで、データをオブジェクトとして扱うために使われます。Visual Studio のADO.NET Entity Data Model ウィザードを実行するとEntity Model を作成できますが、このモデルファーストアプローチでは、データソースに変更があった場合やエンティティ操作をより制御したい場合は不都合があります。この記事では、CData ADO.NET Provider を使いコードファーストアプローチでMoney Forward Expense にアクセスします。
MoneyForward Expense はOAuth 2 認証標準を利用しています。MoneyForward Expense にアプリケーションを登録し、OAuthClientId およびOAuthClientSecret を取得する必要があります。認証方法については、ヘルプドキュメントの「MoneyForward Expense への接続」を参照してください。
また、ほとんどのテーブルを操作するためにOfficeId プロパティを指定する必要があります。
<configuration>
... <connectionStrings>
<add name="MFExpenseContext" connectionString="Offline=False;OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;InitiateOAuth=GETANDREFRESH" providerName="System.Data.CData.MFExpense" />
</connectionStrings>
<entityFramework>
<providers>
... <provider invariantName="System.Data.CData.MFExpense" type="System.Data.CData.MFExpense.MFExpenseProviderServices, System.Data.CData.MFExpense.Entities.EF6" />
</providers>
<entityFramework>
</configuration>
</code>
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class MFExpenseContext :DbContext {
public MFExpenseContext() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { // To remove the requests to the Migration History table
Database.SetInitializer<MFExpenseContext>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;
public class Offices {
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public System.String Id { get; set; }
public System.String Id { get; set; }
}
public class OfficesMap :EntityTypeConfiguration<Offices> {
public OfficesMap() {
this.ToTable("Offices");
this.HasKey(Offices => Offices.Id);
this.Property(Offices => Offices.Id);
}
}
public DbSet<Offices> Offices { set; get; }
MFExpenseContext context = new MFExpenseContext();
context.Configuration.UseDatabaseNullSemantics = true;
var query = from line in context.Offices select line;