如果使用 Dapper 查询时不需要参数怎么办?
我有一个查询,它可以在不需要参数的情况下进行计数/分组(没有 where 子句)。
使用 dapper 运行无参数查询的语法是什么?
var _results = _conn.Query<strongType>("Select Count(columnA) as aCount, ColumnB, ColumnC from mytable group by ColumnB, ColumnC");
不起作用。
我尝试了几种不同的方法,但仍然不断收到“ArgumentNullException 未由用户代码处理”。
试图自己弄清楚,到处搜索,我放弃了。提前致谢。
编辑:下面是 SqlMapper.cs 中引发错误的代码行。第 1334 行
il.Emit(OpCodes.Newobj, typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
错误详细信息:值不能为空。参数名称:con
I have one query that does a count/group by where I don't need a parameter (there is no where clause).
What is the syntax to run a parameterless query with dapper?
var _results = _conn.Query<strongType>("Select Count(columnA) as aCount, ColumnB, ColumnC from mytable group by ColumnB, ColumnC");
does not work.
I've tried it a few different ways but I still keep getting "ArgumentNullException was unhandled by user code".
Tried to figure it out myself, searched all over and I'm giving up. Thanks in advance.
Edit: Below is the line of code from SqlMapper.cs that throws the error. It's line 1334
il.Emit(OpCodes.Newobj, typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));
The error details: Value cannot be null. Parameter name: con
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将单个结果映射回来效果很好:
如果您的查询以某种方式没有返回结果,您可能会遇到麻烦,例如:
返回 0 个结果,因此在空结果集上执行
Single
是行不通的。Mapping a single result back works just fine:
You may face trouble if somehow your query returns no results, for example:
return 0 results, so doing a
Single
on an empty result set is not going to work.尝试
try
值不能为空。参数名称: con
这是由包括 Dappper、PetaPoco 和 Massive 在内的多个动态 ORM 引发的错误,但通常是相同的问题:确保您在不想包含的属性上使用 [Ignore] 属性。这包括从基类继承的属性。该错误毫无用处,但这就是它的意思。
Value cannot be null. Parameter name: con
This is error is thrown by several dynamic ORMs including Dappper, PetaPoco and Massive but it's usually the same problem: Make sure you're using the [Ignore] attribute on the properties you don't want to include. This includes properties inherited from base classes. The error is useless but that's what it means.
出现此错误的原因是您尝试在返回对象中设置的属性是仅获取。当然,Dapper 需要能够设置所有属性。您可能会考虑拥有一个单独的数据库 DTO 对象,然后在从数据库读取数据后将其转换为正确的不可变域对象。
将此:更改
为:
This error can occur because a property you're trying to set in your return object is get-only. Dapper, of course, requires being able to set all properties. You might consider having a separate database DTO object that then gets converted to your properly immutable domain object after reading from the database.
Change this:
to this: