本文介绍了使用 Dapper 执行插入和更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 Dapper 感兴趣 - 但据我所知,它只支持查询和执行.我没有看到 Dapper 包含插入和更新对象的方法.

I am interested in using Dapper - but from what I can tell it only supports Query and Execute. I do not see that Dapper includes a way of Inserting and Updating objects.

鉴于我们的项目(大多数项目?)需要进行插入和更新,与 dapper 一起进行插入和更新的最佳实践是什么?

Given that our project (most projects?) need to do inserts and updates, what is the best practice for doing Inserts and Updates alongside dapper?

最好我们不必求助于参数构建等的 ADO.NET 方法.

Preferably we would not have to resort to the ADO.NET method of parameter building, etc.

此时我能想到的最佳答案是使用 LinqToSQL 进行插入和更新.有更好的答案吗?

The best answer I can come up with at this point is to use LinqToSQL for inserts and updates. Is there a better answer?

推荐答案

我们正在考虑构建一些帮助程序,仍在决定 API 以及这是否属于核心.请参阅:https://code.google.com/archive/p/dapper-dot-net/issues/6 取得进展.

We are looking at building a few helpers, still deciding on APIs and if this goes in core or not. See: https://code.google.com/archive/p/dapper-dot-net/issues/6 for progress.

同时,您可以执行以下操作

In the mean time you can do the following

val = "my value";
cnn.Execute("insert into Table(val) values (@val)", new {val});

cnn.Execute("update Table set val = @val where Id = @id", new {val, id = 1});

等等

另见我的博文:那个烦人的插入问题

更新

正如评论中所指出的,现在 Dapper.Contrib 中有几个扩展可用 项目以这些IDbConnection 扩展方法的形式:

As pointed out in the comments, there are now several extensions available in the Dapper.Contrib project in the form of these IDbConnection extension methods:

T Get<T>(id);
IEnumerable<T> GetAll<T>();
int Insert<T>(T obj);
int Insert<T>(Enumerable<T> list);
bool Update<T>(T obj);
bool Update<T>(Enumerable<T> list);
bool Delete<T>(T obj);
bool Delete<T>(Enumerable<T> list);
bool DeleteAll<T>();

这篇关于使用 Dapper 执行插入和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 10:26