【C#】PostgreSQLでEntityFrameworkの使用方法#2

前回はGUIのエディタを使った方法を書きましたが今回はcsファイルを作成して直接接続する方法を上げたいと思います。GUIを使用した場合のデメリットであるインストールしたversionとの互換が無いとダメという問題がありません。つまりNpgsqlのバージョンアップに対応し易いということですね。

参照追加(Nuget)

Nuget実行方法はこちらの記事を参考下さい。
https://programchan.com/archives/135
検索ウィンドウに「npgsql」と入力して表示される「Npgsql for Entity Framework 6」をインストールします。
npgsql
これをインストールすると「Npgsql」「EntityFramework」が自動で追加されます。インストール済みを見るとこういう形になっているかと思います。
npgsql2

App.configの変更

DbProviderFactoriesに関する情報を追記する必要があります。「system.data」「DbProviderFactories」を「configuration」の前に追加して下さい。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql" />
      <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>
</configuration>

※3月4日追記
Npgsqlが既に端末に登録されている場合にはDbProviderFactoriesに<remove invariant=”Npgsql” />を追加しないとエラーとなりますので追加しました。

データベースクラスの作成

hogeと言うテーブルにidとnameがあった場合に作成するクラスは以下のようになります。EntityFrameworkはKeyを設定してやらないと動かないので注意が必要です。

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace pg
{
    [Table("hoge")]
    class Hoge
    {
        [Key]
        [Column("id")]
        public int id { get; set; }
        [Column("name")]
        public string name { get; set; }
    }
}

DbContextの作成

DbContextを継承してクラスを作ります。今回はFugaDbContextと言う名前で作成してみています。コンストラクタでNpgsqlConnectionを渡しているのとスキーマ(Shema)を変更する方法を書いています

using System.Data.Entity;
namespace pg
{
    class FugaDbContext : DbContext
    {
        private const string ConnectionString = "Server=127.0.0.1;User ID=user;Password=pass;Database=db;syncnotification=false;port=5432";
        /// <summary>
        /// Initializes a new instance of the <see cref="FugaDbContext"/> class.
        /// </summary>
        public FugaDbContext() : base(new NpgsqlConnection(ConnectionString), true) { }
        public DbSet<Hoge> Hoges { get; set; }
        //スキーマを変更する場合にはここに設定
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Configure default schema
            modelBuilder.HasDefaultSchema("schema_name");
        }
    }
}

使い方

DbContextは作成されているのでLinqなどを使用して取り出しましょう。

using System;
using System.Linq;
namespace pg
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new FugaDbContext())
            {
                var ret = context.Hoges.ToList();
                foreach (var v in ret)
                {
                    Console.WriteLine(string.Format("id:{0} name:{1}", v.id, v.name));
                }
                var some = context.Hoges.First(x => x.name == "some");
                Console.WriteLine(string.Format("id:{0} name:{1}", some.id, some.name));
            }
        }
    }
}

GUIを使用してのクラス作成が出来るようになったので簡単になったのはいいんですがNpgsqlのバージョンアップ時にどうしたものか悩ましい問題だったのが今回の直接作成で解決しますね。
それでもあまりに大きいデータベースの場合にはテーブルクラスの作成だけでもかなりの重労働?なのでそこはジェネレータで作成した方が刺し身たんぽぽから逃げれて嬉しいですね。そのうちジェネレータの使用方法を取り上げたいとは思います。

Follow me!

【C#】PostgreSQLでEntityFrameworkの使用方法#2” に対して1件のコメントがあります。

コメントは受け付けていません。