将 join 引入存储过程时 Dapper 返回 null
Dapper is returning null when join is introduced to stored procedure
我有一个奇怪的问题,似乎找不到任何答案。好像我在存储过程中引入join的时候,Dapper的映射无法正常工作
我的自定义对象:
public class CustomObject
{
public string ColumnOne { get; set; }
public string ColumnTwo { get; set; }
public DateTime? ColumnThree { get; set; }
public string ColumnFour { get; set; }
}
在我的存储库调用中,customObjects
总是 returning 0 个结果。
使用 Dapper 调用我的存储库:
public IEnumerable<CustomObject> GetTheThings(int? integerParameter)
{
List<CustomObject> customObjects;
using (var connection = "connection string syntax"))
{
string sql = "[storedProcedureName]";
connection.Open();
customObjects = connection.Query<CustomObject>(sql,
new { IntegerParameter = integerParameter },
commandType: CommandType.StoredProcedure).ToList();
}
return customObjects;
}
存储过程的基本语法:
SELECT
t1.column_one AS [ColumnOne], -- column_one is nvarchar data type
t2.column_two_thing AS [ColumnTwo], -- column_two_thing is nvarchar data type
t2.column_three AS [ColumnThree], -- column_three is DateTime data type
t2.column_four AS [ColumnFour] -- column_four is nvarchar data type
FROM
Table1 t1 WITH (NOLOCK)
JOIN
Table2 t2 WITH (NOLOCK) ON t1.Identifier = t2.Identifier -- Identifiers are both uniqueidentifier data types
WHERE
t1.column_one = @IntegerParameter;
当我直接在 SSMS 中执行此存储过程时,它 return 是我期望的值和我期望的格式
ColumnOne
ColumnTwo
ColumnThree
ColumnFour
TextOne
ColumnTwoResultTextOne
2021-12-16 00:00:00.001
ColumnFourResultTextOne
TextOne
ColumnTwoResultTextTwo
2021-12-16 00:00:00.001
ColumnFourResultTextTwo
TextOne
ColumnTwoResultTextThree
2021-12-16 00:00:00.001
ColumnFourResultTextThree
TextOne
ColumnTwoResultTextFour
2021-12-16 00:00:00.001
ColumnFourResultTextFour
当我在运行时使用 Dapper 通过我的应用程序执行时,return 的结果为 0。
出于某种原因,当我删除连接并为依赖于连接的列手动键入值时,我会收到返回的结果。
returns 结果并适当映射的脚本:
SELECT
t1.column_one AS [ColumnOne], -- column_one is nvarchar data type
'Literally anything' AS [ColumnTwo], -- column_two_thing is nvarchar data type
GETDATE() AS [ColumnThree], -- column_three is DateTime data type
'Anything here too' AS [ColumnFour] -- column_four is nvarchar data type
FROM
Table1 t1 WITH (NOLOCK)
WHERE
t1.column_one = @IntegerParameter;
我还尝试用硬编码 'Literally anything' as [ColumnName]
替换每一列,以确保它不仅仅是 属性 在 Dapper 翻译中丢失,但没有结果会 return只要连接在查询中,就可以在运行时从我的方法中获取。
我一直在阅读 Dapper 文档并仔细阅读 Whosebug 和其他资源以获取任何建议,但找不到哪里出错了。
我们将不胜感激任何帮助或建议!
您没有得到任何行的事实表明您的两个表中没有可以连接的结果。
默认情况下,JOIN 语句将是内部联接。这将 return 只有在两个表中具有匹配值的记录。
编辑:在这种情况下,运行 SSMS 和 Dapper 中的查询之间的不一致是因为连接字符串中帐户的权限不同。因此无法访问其中一个表中的数据,因此 JOIN return 没有行。
我有一个奇怪的问题,似乎找不到任何答案。好像我在存储过程中引入join的时候,Dapper的映射无法正常工作
我的自定义对象:
public class CustomObject
{
public string ColumnOne { get; set; }
public string ColumnTwo { get; set; }
public DateTime? ColumnThree { get; set; }
public string ColumnFour { get; set; }
}
在我的存储库调用中,customObjects
总是 returning 0 个结果。
使用 Dapper 调用我的存储库:
public IEnumerable<CustomObject> GetTheThings(int? integerParameter)
{
List<CustomObject> customObjects;
using (var connection = "connection string syntax"))
{
string sql = "[storedProcedureName]";
connection.Open();
customObjects = connection.Query<CustomObject>(sql,
new { IntegerParameter = integerParameter },
commandType: CommandType.StoredProcedure).ToList();
}
return customObjects;
}
存储过程的基本语法:
SELECT
t1.column_one AS [ColumnOne], -- column_one is nvarchar data type
t2.column_two_thing AS [ColumnTwo], -- column_two_thing is nvarchar data type
t2.column_three AS [ColumnThree], -- column_three is DateTime data type
t2.column_four AS [ColumnFour] -- column_four is nvarchar data type
FROM
Table1 t1 WITH (NOLOCK)
JOIN
Table2 t2 WITH (NOLOCK) ON t1.Identifier = t2.Identifier -- Identifiers are both uniqueidentifier data types
WHERE
t1.column_one = @IntegerParameter;
当我直接在 SSMS 中执行此存储过程时,它 return 是我期望的值和我期望的格式
ColumnOne | ColumnTwo | ColumnThree | ColumnFour |
---|---|---|---|
TextOne | ColumnTwoResultTextOne | 2021-12-16 00:00:00.001 | ColumnFourResultTextOne |
TextOne | ColumnTwoResultTextTwo | 2021-12-16 00:00:00.001 | ColumnFourResultTextTwo |
TextOne | ColumnTwoResultTextThree | 2021-12-16 00:00:00.001 | ColumnFourResultTextThree |
TextOne | ColumnTwoResultTextFour | 2021-12-16 00:00:00.001 | ColumnFourResultTextFour |
当我在运行时使用 Dapper 通过我的应用程序执行时,return 的结果为 0。
出于某种原因,当我删除连接并为依赖于连接的列手动键入值时,我会收到返回的结果。
returns 结果并适当映射的脚本:
SELECT
t1.column_one AS [ColumnOne], -- column_one is nvarchar data type
'Literally anything' AS [ColumnTwo], -- column_two_thing is nvarchar data type
GETDATE() AS [ColumnThree], -- column_three is DateTime data type
'Anything here too' AS [ColumnFour] -- column_four is nvarchar data type
FROM
Table1 t1 WITH (NOLOCK)
WHERE
t1.column_one = @IntegerParameter;
我还尝试用硬编码 'Literally anything' as [ColumnName]
替换每一列,以确保它不仅仅是 属性 在 Dapper 翻译中丢失,但没有结果会 return只要连接在查询中,就可以在运行时从我的方法中获取。
我一直在阅读 Dapper 文档并仔细阅读 Whosebug 和其他资源以获取任何建议,但找不到哪里出错了。
我们将不胜感激任何帮助或建议!
您没有得到任何行的事实表明您的两个表中没有可以连接的结果。
默认情况下,JOIN 语句将是内部联接。这将 return 只有在两个表中具有匹配值的记录。
编辑:在这种情况下,运行 SSMS 和 Dapper 中的查询之间的不一致是因为连接字符串中帐户的权限不同。因此无法访问其中一个表中的数据,因此 JOIN return 没有行。