Dapper查询单次导致EndOfStreamException

问题描述 投票:-1回答:1

对于非常具体的查询,我收到了流异常的结尾。

同一查询总是会发生-该查询可以很容易地作为SQL查询执行,但是

但是使用dapper及其连接查询却以某种方式失败

我得到EndOfStreamException: Attempted to read past the end of the stream.

This exception was originally thrown at this call stack:
    Npgsql.NpgsqlReadBuffer.Ensure.__EnsureLong|0()

我不确定为什么会收到此异常以及它出了错的地方-对于具有一组特定参数的一个特定查询,它只会返回错误。

我不确定我是否理解此错误是由实际的SQL查询引起的,还是由解析给它的参数引起的,因为我的querySingle执行为

    IDictionary<string, object> insertedEntityRegistration = connection.QuerySingle(insertNewRegistrationSql, parameters);

完整的堆栈跟踪:

   at Npgsql.NpgsqlReadBuffer.<>c__DisplayClass34_0.<<Ensure>g__EnsureLong|0>d.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at Npgsql.NpgsqlDataReader.<NextResult>d__44.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Npgsql.NpgsqlDataReader.NextResult()
   at Npgsql.NpgsqlCommand.<ExecuteReaderAsync>d__102.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   at Dapper.SqlMapper.ExecuteReaderWithFlagsFallback(IDbCommand cmd, Boolean wasClosed, CommandBehavior behavior) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1051
   at Dapper.SqlMapper.QueryRowImpl[T](IDbConnection cnn, Row row, CommandDefinition& command, Type effectiveType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1177
   at Dapper.SqlMapper.QuerySingle[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 781
   at Dapper.SqlMapper.QuerySingle(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 687

我完全不知道这里出了什么问题-因为查询可以在没有dapper的情况下执行而没有任何问题,我不明白为什么在用dapper执行时会导致此问题。

所有参数都在那里-它们都有值,都被映射到正确的一个。

是否可以通过某种方式调试它生成的查询?什么会导致此问题?

查询本身类似于

INSERT INTO table (a,b,c,d,e,f,g,h)
                    VALUES (1, tsrange('0001-01-01T00:00:01', '9999-01-01T00:00:02'), 'Application', tsrange('0001-01-01T00:00:01', '9999-01-01T00:00:02'),
                'name','mail',4511,4511)
                    RETURNING a,b,c,d,e,f,g,h;
c# postgresql dapper
1个回答
0
投票

对于INSERT语句,您需要使用Dapper Execute,而不是QuerySingle()

例如,您的情况:

var insertSqlStatement = "your INSERT query here";
var affectedRows = connection.Execute(insertSqlStatement,  new {ParameterName = "ParemeterValue"});
© www.soinside.com 2019 - 2024. All rights reserved.