简体   繁体   English

是否有一种优雅的方式使用 Dapper 将对象或对象集合作为表值参数传递?

[英]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.

相关问题 不使用(Table-Valued-Parameter)类型将集合作为SQL参数添加到SqlCommand - Add collection as SQL parameter to SqlCommand without using (Table-Valued-Parameter) type 通用实现中的表值的Dapper参数 - Dapper for Table Valued parameter in generic implementation Dapper-将整个实体作为查询参数或匿名对象传递 - Dapper - pass whole entity as query parameter or anonymous object 使用 ADO.NET 传递表值参数 - Pass table valued parameter using ADO.NET C#/Dapper 使用表值参数执行存储过程 - C#/Dapper executing stored procedure with table-valued parameter 不支持SQL Server表值参数列。 类型是'对象' - SQL Server Table Valued Parameter column is not supported. The type is 'Object' 将表值参数传递给存储过程 - Pass table-valued parameter to a stored procedure 如何通过Activator.CreateInstance将对象实例化,并使用Dapper将其插入表中 - How to instanciate an object by Activator.CreateInstance an inserting it on a table using Dapper object 中的小巧玲珑 object 有什么方法可以加载到 sfdatagrid 中吗? - Is there some way with dapper object in object to load in sfdatagrid? 在sql_variant列的表值参数中传递类型为'object'的参数 - Passing parameter of type 'object' in table-valued parameter for sql_variant column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM