ノーコードでクラウド上のデータとの連携を実現。
詳細はこちら →CData
こんにちは!ウェブ担当の加藤です。マーケ関連のデータ分析や整備もやっています。
Entity Framework はobject-relational mapping フレームワークで、データをオブジェクトとして扱うために使われます。Visual Studio のADO.NET Entity Data Model ウィザードを実行するとEntity Model を作成できますが、このモデルファーストアプローチでは、データソースに変更があった場合やエンティティ操作をより制御したい場合は不都合があります。この記事では、CData ADO.NET Provider を使いコードファーストアプローチでSingleStore にアクセスします。
データに接続するには、次の接続プロパティが必要です。
また、オプションで以下を設定することもできます。
標準認証で認証するには、次を設定します。
標準のユーザー名とパスワードを提供する代わりに、Windows 認証を介して信頼されたされたユーザーをサーバーに認証できます。
SSL 認証を活用してセキュアなセッションを介してSingleStore データに接続できます。次の接続プロパティを設定し、データに接続します。
SSH を使用して、セキュアにリモートマシンにログインできます。SingleStore データにSSH 経由でアクセスするには、次の接続プロパティを設定します。
<configuration>
... <connectionStrings>
<add name="SingleStoreContext" connectionString="Offline=False;User=myUser;Password=myPassword;Database=NorthWind;Server=myServer;Port=3306;" providerName="System.Data.CData.SingleStore" />
</connectionStrings>
<entityFramework>
<providers>
... <provider invariantName="System.Data.CData.SingleStore" type="System.Data.CData.SingleStore.SingleStoreProviderServices, System.Data.CData.SingleStore.Entities.EF6" />
</providers>
<entityFramework>
</configuration>
</code>
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
class SingleStoreContext :DbContext {
public SingleStoreContext() { }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { // To remove the requests to the Migration History table
Database.SetInitializer<SingleStoreContext>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;
public class Orders {
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public System.String Id { get; set; }
public System.String ShipName { get; set; }
}
public class OrdersMap :EntityTypeConfiguration<Orders> {
public OrdersMap() {
this.ToTable("Orders");
this.HasKey(Orders => Orders.Id);
this.Property(Orders => Orders.ShipName);
}
}
public DbSet<Orders> Orders { set; get; }
SingleStoreContext context = new SingleStoreContext();
context.Configuration.UseDatabaseNullSemantics = true;
var query = from line in context.Orders select line;