Dapper.DefaultTypeMap.MatchNamesWithUnderscores
不适用于插入。映射器适用于该Get<>
方法。我在我的 ASP.NET Core 1.0 RC2 项目中使用以下版本以及 postgres 数据库。
"dependencies": {
"Dapper": "1.50.0-rc2",
"Dapper.Contrib": "1.50.0-beta8"
}
代码片段
using (var conn = new NpgsqlConnection("connString"))
{
conn.Open();
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
long id = conn.Insert(new Foo { Name = "new foo", LocationId = 3});
return id;
}
执行的插入 SQL 语句
insert into foo ("Name", "LocationId") values ($1, $2) RETURNING Id
Foo 类
[Dapper.Contrib.Extensions.Table("foo")]
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public int LocationId { get; set; }
}
富表
CREATE TABLE "foo" (
"id" SERIAL PRIMARY KEY,
"name" VARCHAR(100) NOT NULL,
"location_id" INTEGER REFERENCES "location" (id)
);