Dapper System.Data.SqlClient.SqlException: '必须声明标量变量
Dapper System.Data.SqlClient.SqlException: 'Must declare the scalar variable
我正在尝试查询具有 ID 的特定记录并正在声明 ID 的值,但仍然出现错误。
首先我调用了数据库函数:
specificEntity = await _db_FinancialEntityDb.GetFinancialEntity(id);
然后:
public Task<FinancialEntity_Model> GetFinancialEntity(int financialEntityID)
{
string sql = @"select fe.Id,
fe.EntityTypeID,
fe.Balance,
fe.InterestRate,
fe.OpenDate,
fe.MinimumPayment,
fe.APY,
fe.EntityName,
fe.InitialAmount,
fet.EntityType
from dbo.Financial_Entity fe
left outer join Financial_Entity_Type fet on fe.EntityTypeID = fet.Id
where fe.Id = @financialEntityID";
return _db.LoadDataObject<FinancialEntity_Model, dynamic>(sql, new { Id = financialEntityID });
}
我正在使用 QueryFirstAsync:
var data = await connection.QueryFirstAsync<T>(sql, parameters);
但是我一直收到这个错误:
System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@financialEntityID".'
是QueryFirstAsync
的问题吗?
@financialEntityID 应该是@Id,因为这是您传递给 Dapper 的名称。
当您将参数作为匿名类型传递时,属性 名称应与参数名称相匹配。例如
new { Id = financialEntityID }
会将名为 @Id
的参数与 financialEntityID
的值绑定。
我正在尝试查询具有 ID 的特定记录并正在声明 ID 的值,但仍然出现错误。
首先我调用了数据库函数:
specificEntity = await _db_FinancialEntityDb.GetFinancialEntity(id);
然后:
public Task<FinancialEntity_Model> GetFinancialEntity(int financialEntityID)
{
string sql = @"select fe.Id,
fe.EntityTypeID,
fe.Balance,
fe.InterestRate,
fe.OpenDate,
fe.MinimumPayment,
fe.APY,
fe.EntityName,
fe.InitialAmount,
fet.EntityType
from dbo.Financial_Entity fe
left outer join Financial_Entity_Type fet on fe.EntityTypeID = fet.Id
where fe.Id = @financialEntityID";
return _db.LoadDataObject<FinancialEntity_Model, dynamic>(sql, new { Id = financialEntityID });
}
我正在使用 QueryFirstAsync:
var data = await connection.QueryFirstAsync<T>(sql, parameters);
但是我一直收到这个错误:
System.Data.SqlClient.SqlException: 'Must declare the scalar variable "@financialEntityID".'
是QueryFirstAsync
的问题吗?
@financialEntityID 应该是@Id,因为这是您传递给 Dapper 的名称。
当您将参数作为匿名类型传递时,属性 名称应与参数名称相匹配。例如
new { Id = financialEntityID }
会将名为 @Id
的参数与 financialEntityID
的值绑定。