Dapper 插入语法

Dapper insert syntax

我使用 c# 应用程序中的 orm dapper 进行 mysql 数据库访问。它工作正常。只有语法问题。我有一个具有许多属性的 class。所有这些属性与数据库 table 字段完全匹配。所以 Select 请求很短:

 var listOfInstances = con.Query<MyClass>("Select * From myTable");

但是当我需要向数据库中插入一些东西时,我必须写下所有那些看起来有点难看的属性名称:

con.Execute(@"Insert into myTable values(@Id, @Property1, @Property2, @Property3, @Property4, ....)", listOfInstances);

我想知道是否有任何更短的语法来插入数据,至少对于所有 class 属性与数据库 table 字段完全匹配的情况。

P.S。更新请求也是如此

P.P.S。老实说,我刚开始使用包含许多 table 的数据库,所以我不得不为每个 table 编写基本函数 get/add/change 实例,这很烦人列出他们所有的领域。

基本上你需要安装一个名为 Dapper.Contrib 的 nuget 包 这是回购协议 https://github.com/StackExchange/dapper-dot-net/tree/master/Dapper.Contrib

也看看这个https://samsaffron.com/archive/2012/01/16/that-annoying-insert-problem-getting-data-into-the-db-using-dapper