[英]Is there a elegant way to pass object or object collection as table-valued-parameter using Dapper?
I'm just getting my hands dirty with Dapper and loving its simplicity.我只是用 Dapper 弄脏了我的手,并喜欢它的简单性。 I love that I can easily map the results of a SELECT procedure to a class, which was a pain to do in straight ADO and overkill using EF.
我喜欢我可以轻松地将 SELECT 过程的结果映射到一个类,这在直接 ADO 中很痛苦,而使用 EF 则矫枉过正。
Now I'm trying to figure out how best to go the other way - to take an instance of that same class and pass it back to a stored procedure to perform and UPDATE or INSERT.现在,我试图找出最好的方法——采用同一个类的实例并将其传递回存储过程以执行和更新或插入。
I know that Dapper supports table-valued parameters, which is good, but everything I see out there suggests that I first must construct a DataTable or SqlDataRecord collection.我知道 Dapper 支持表值参数,这很好,但是我在那里看到的一切都表明我必须首先构造一个 DataTable 或 SqlDataRecord 集合。 It's easy enough to create a helper class that will accomplish this, but it feels clunky to me.
创建一个帮助类来实现这一点很容易,但对我来说感觉很笨重。 A better way?
更好的方法?
Right now: no, not really.现在:不,不是真的。 However, I am interested in investigating whether we can do something by recognizing something like value-tuple arrays, so that this works:
但是,我有兴趣调查我们是否可以通过识别值元组数组之类的东西来做某事,以便这样做:
var args = new {
id = 1, name = "abc",
someTVP = new[] {
(1, "abc", 42),
(6, "def", 84),
});
... use args with dapper
(note that this would be strictly positional - any names in the call-site are not visible to the receiver, a fundamental limitation with value-tuples) (请注意,这将是严格的位置 - 调用站点中的任何名称对接收器都是不可见的,这是值元组的基本限制)
But: to emphasize, no: this doesn't exist today.但是:强调一下,不:这在今天不存在。 Your best bet is
DataTable
.您最好的选择是
DataTable
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.