LoginSignup
2
1

More than 1 year has passed since last update.

Gtk3アプリ Dapperのアンダーバーの修正

Last updated at Posted at 2021-08-21

Dapperのアンダーバーを使えるようにする修正

DapperでDbのカラム名にアンダーバーがあるとsplitで割って片方を名前を参照する仕様になっている
アンダーバーを使えるようにする。

Dapperだけを利用する場合

Dapperがカラム名のアンダースコアを無視する設定を追加します。

Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;

Dapper Extensionを利用する場合
案1

DapperExtensionのソースを書き換える。
ReflectionHelper.csの中にSplitで割る箇所があるので削除してみる。

var entityPropertyName = propertyName;//.Split('_').Last();
案2

DapperExtensionのClassMapを使い、大文字小文字とアンダーバーのマッピングを行う

Modelファイル側 キャメルケース 大文字小文字
Db側       スネークケース アンダーバー

ClassMapのサンプルはDapperExtensionの中のテストケースファイルが参考になる

public class test
{
    public long aId { get; set; }
    public string tes1Tes2 { get; set; }
}

public class testMap : ClassMapper<test>
{
    public testMap()
    {
        Table("test");
        AutoMap();
        Map(f => f.tes1Tes2).Column("tes1_tes2");
    }
}

//mapperを指定する
 DapperExtensions.DapperExtensions.DefaultMapper = typeof (testMap);
//複数
 DapperExtensions.DapperExtensions.SetMappingAssemblies(new[]
 {
     typeof (testMap).Assembly,
     typeof (testMap).Assembly
 });

Dbからモデルを生成する

とりあえず案1で様子を見ることにする

Gtk3アプリ TreeViewとデータ各種処理に続く

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1