EFCore 2.1 动态 SQL 参数
EFCore 2.1 Dynamic SQL Params
我认为我在这方面走在正确的轨道上,但老实说,遇到一些事情让我很困惑。
我有一个 table 地理,所以现在,在 EFCore 提供地理支持之前,我正在为特定 table 构建自己的 SQL 查询。它实际上是一个搜索,因此它基于一组可以传递到端点的查询参数动态构建。
因此,我使用反射来迭代 DTO 并根据具有值的属性构建 SQL 查询。我从我的查询构建器返回一个元组,其中包含 List<SqlParameter>
和 List<string>
,后者是原始 sql,前者是参数。
然后我做一个汇总将所有这些放在一起。
我遇到以下问题:
//This is inside my SQL Param builder method which returns a
//Tuple of sqlQuery and sqlParams
if (!string.IsNullOrEmpty(search.Municipality))
{
sqlQuery.Add("Municipality LIKE %@Municipality%");
sqlParams.Add(new SqlParameter("@Municipality", search.Municipality));
}
//Thi lines aggregates the result to build a SELECT * FROM query
sql = sqlParams.Item2.Aggregate(sql, (current, t) => current + " AND " + t);
//This executes the query
return DbSet.FromSql(sql, sqlParams.Item1.ToArray()).ToListAsync();
//This is the generated SQL string that is printed from the sql variable above
SELECT * FROM AirportData WHERE Municipality LIKE %@Municipality%
//This is the error I get from EFCore
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@Municipality'.
我现在只使用 1 个参数来让它工作。
关于为什么不将 SQL 参数转换为查询值的任何想法?我在这里完全偏离基地了吗?在此先感谢您的关注和任何建议?
错误不是来自 EF Core,而是来自 SqlServer,因为 %@Municipality%
不是有效的 SQL 表达式。
它应该类似于 '%' + @Municipality + '%'
或 N'%' + @Municipality + N'%'
,因此相应地修改您的 SQL 生成器,例如
sqlQuery.Add("Municipality LIKE '%' + @Municipality+ '%'");
我猜 EF 核心不直接支持 sql 参数,所以我使用这个示例。 FromSql(string,params object[])
字符串:sql 查询
参数:sql 参数(@key 或 {0})
SqlCommand command = new SqlCommand
{
CommandText = @"SELECT DISTINCT * FROM F_SearchQuery(@keys)"
};
SqlParameter k = new SqlParameter("@keys", keys ?? (object)DBNull.Value);
return _repositoryCustom.JobSearchQuering.FromSql(command.CommandText, k).ToList();
我认为我在这方面走在正确的轨道上,但老实说,遇到一些事情让我很困惑。
我有一个 table 地理,所以现在,在 EFCore 提供地理支持之前,我正在为特定 table 构建自己的 SQL 查询。它实际上是一个搜索,因此它基于一组可以传递到端点的查询参数动态构建。
因此,我使用反射来迭代 DTO 并根据具有值的属性构建 SQL 查询。我从我的查询构建器返回一个元组,其中包含 List<SqlParameter>
和 List<string>
,后者是原始 sql,前者是参数。
然后我做一个汇总将所有这些放在一起。
我遇到以下问题:
//This is inside my SQL Param builder method which returns a
//Tuple of sqlQuery and sqlParams
if (!string.IsNullOrEmpty(search.Municipality))
{
sqlQuery.Add("Municipality LIKE %@Municipality%");
sqlParams.Add(new SqlParameter("@Municipality", search.Municipality));
}
//Thi lines aggregates the result to build a SELECT * FROM query
sql = sqlParams.Item2.Aggregate(sql, (current, t) => current + " AND " + t);
//This executes the query
return DbSet.FromSql(sql, sqlParams.Item1.ToArray()).ToListAsync();
//This is the generated SQL string that is printed from the sql variable above
SELECT * FROM AirportData WHERE Municipality LIKE %@Municipality%
//This is the error I get from EFCore
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@Municipality'.
我现在只使用 1 个参数来让它工作。
关于为什么不将 SQL 参数转换为查询值的任何想法?我在这里完全偏离基地了吗?在此先感谢您的关注和任何建议?
错误不是来自 EF Core,而是来自 SqlServer,因为 %@Municipality%
不是有效的 SQL 表达式。
它应该类似于 '%' + @Municipality + '%'
或 N'%' + @Municipality + N'%'
,因此相应地修改您的 SQL 生成器,例如
sqlQuery.Add("Municipality LIKE '%' + @Municipality+ '%'");
我猜 EF 核心不直接支持 sql 参数,所以我使用这个示例。 FromSql(string,params object[])
字符串:sql 查询
参数:sql 参数(@key 或 {0})
SqlCommand command = new SqlCommand
{
CommandText = @"SELECT DISTINCT * FROM F_SearchQuery(@keys)"
};
SqlParameter k = new SqlParameter("@keys", keys ?? (object)DBNull.Value);
return _repositoryCustom.JobSearchQuering.FromSql(command.CommandText, k).ToList();