Dapper处理返回空结果集

Joh*_*ate 8 datareader dapper

我们正在使用Dapper来映射我们的sql数据,到目前为止它已经运行得很好.我有一个案例,虽然我们在做类似的事情:

someObject = con.Query<T>("GetInfoSproc", p, commandType: CommandType.StoredProcedure).Single();
Run Code Online (Sandbox Code Playgroud)

只要我正在调用的存储过程返回数据,这就很有效.有时存储过程可能不会返回结果并在out参数中返回错误.这似乎导致Dapper出现问题,因为dapper会抛出错误:

"当使用多映射API时,如果您的ID不是Id,请确保设置splitOn参数"

有没有办法编写查询,以便它可以正确处理返回空结果的情况或这是Dapper的限制?

Bre*_*tra 6

SingleOrDefault() 你的朋友在这里吗

尝试这个:

someObject = con.Query<T>("GetInfoSproc", p, commandType: CommandType.StoredProcedure).SingleOrDefault();
if (someObject != null)
{
  // operate on your results here
}
return someObject;
Run Code Online (Sandbox Code Playgroud)

你还需要确保T是Nullable