.Net Core 2 FromSql 问题
.Net Core 2 FromSql issue
您好,我开始学习如何使用 .Net Core。我一直在尝试让 .FromSql() 处理我的一个查询。我遇到的问题是,当查询命中服务器时,当我将@metricID 设置为query.MetricId 时,@metric 被设置为@metricID=N'"100"'。我使用 sql 探查器来查看为什么我总是一无所获,它看起来像这样。
exec sp_executesql N' select * from metrics where rr.metric_id = @metricID',N'@metricID nvarchar(5)',@metricID=N'"100"'
public async Task<List<MetricView>> GetMetricAsync(MetricQuery query)
{
string qry = @"select * from metrics where metricid = @metricID";
SqlParameter metricID = new SqlParameter("@metricID", query.MetricId);
try
{
var metricReturn = await _context.Metrics
.FromSql(qry, metricID).ToListAsync();
return _mapper.Map<List<MetricView>>(metricReturn);(dropdown);
}
catch (Exception e)
{
var t = e.Message;
return null;
}
finally
{
_sqlConnection.Dispose();
}
}
public class MetricQuery
{
public string MetricId { get; set; }
public string MircoRegion { get; set; }
public string Consortia { get; set; }
public string Institution { get; set; }
public string AcademicYear { get; set; }
}
The issue I'm having is that when the query hits the server @metric is getting set to @metricID=N'"100"'
这个问题是op在postman中加了额外的双引号引起的。
从邮递员中删除它应该可以解决这个问题。
您好,我开始学习如何使用 .Net Core。我一直在尝试让 .FromSql() 处理我的一个查询。我遇到的问题是,当查询命中服务器时,当我将@metricID 设置为query.MetricId 时,@metric 被设置为@metricID=N'"100"'。我使用 sql 探查器来查看为什么我总是一无所获,它看起来像这样。
exec sp_executesql N' select * from metrics where rr.metric_id = @metricID',N'@metricID nvarchar(5)',@metricID=N'"100"'
public async Task<List<MetricView>> GetMetricAsync(MetricQuery query)
{
string qry = @"select * from metrics where metricid = @metricID";
SqlParameter metricID = new SqlParameter("@metricID", query.MetricId);
try
{
var metricReturn = await _context.Metrics
.FromSql(qry, metricID).ToListAsync();
return _mapper.Map<List<MetricView>>(metricReturn);(dropdown);
}
catch (Exception e)
{
var t = e.Message;
return null;
}
finally
{
_sqlConnection.Dispose();
}
}
public class MetricQuery
{
public string MetricId { get; set; }
public string MircoRegion { get; set; }
public string Consortia { get; set; }
public string Institution { get; set; }
public string AcademicYear { get; set; }
}
The issue I'm having is that when the query hits the server @metric is getting set to @metricID=N'"100"'
这个问题是op在postman中加了额外的双引号引起的。
从邮递员中删除它应该可以解决这个问题。