Dapper - 如何将结果集从 Postgres 函数结果集列映射到 Class 属性
Dapper - How to Map Resultset From A Postgres Function Resultset Column To A Class Property
我正在尝试学习如何将 C# 与 dapper 一起使用,主要是调用 postgreSQL 函数。我正在努力将结果集中的列映射到 class 属性.
我的Class定义为:
public class CourseUnit
{
public int unitId { get; set; }
public string description { get; set; }
}
存储过程被调用,如下面的清单草稿所示。存储过程结果集包含两列:
- UnitID
- 单位名称
我在将 UnitName 从存储过程映射到我的 class 的描述 属性 时遇到困难。该代码调用该过程并生成一个包含 UnitID 值但描述值为空的结果集。如何将 UnitName 映射到我的 class 的描述 属性?
public async Task<IEnumerable<CourseUnit>> GetCourseUnitsAsync(int courseId)
{
using (var connection = new NpgsqlConnection(_connectionString))
{
connection.Open();
Console.WriteLine($"About to execute the course units query for course {courseId}");
try
{
var p = new DynamicParameters();
p.Add("@course_id", courseId);
var result = await connection.QueryAsync<CourseUnit>(@"coursemanagement.select_course_units",
p,
commandType: CommandType.StoredProcedure);
Console.WriteLine($"Total results of course units are {result.AsList().Count}");
return result;
}
catch(Exception)
{
throw new KeyNotFoundException($"No Units found for course {courseId}");
}
throw new KeyNotFoundException();
}
}
我试过如下调用存储过程:
var result = await connection.QueryAsync<CourseUnit>(@"SELECT 'UnitID' as unitid, 'UnitName' as description FROM coursemanagement.select_course_units",
new { courseId });
Console.WriteLine($"Total results of course units are {result.AsList().Count}");
return result;
但是,我收到一个异常 =>
Exception encountered while retrieving course units 42P01: relation "coursemanagement.select_course_units" does not exist
看起来问题不在于列名,而在于您使用的 table 名称。在您的示例中,它假设数据库是 coursemanagement 并且 table 是 select_course_units?这是正确的吗,通常也可以在两者之间有一个模式,通常是 dbo 所以你可能需要使用 coursemanagement.dbo.select_course_units 或 coursemanagement.. select_course_units 从 table 中正确获取数据
我去更新存储过程以引入与 POCO class 属性.
同名的列别名
我已将 link 添加到以下 Whosebug resource 中,详细说明了其他可能性:
- 此 post 建议使用 LINQ 的动态类型查询在结果集和 POCO 之间进行映射 class。
- 使用 Dapper 自定义 属性 键入详细的映射器 here
PostgreSQL 允许函数的结果集是 queried,例如
SELECT UnitID, UnitName from myStoredProc(parameterValue);
如果使用 PostgreSQL 那么可以使用存储过程中的 ExecuteReader class 到 运行 原始 SQL 代码到 select并准备了DataTable,但并不理想
我正在尝试学习如何将 C# 与 dapper 一起使用,主要是调用 postgreSQL 函数。我正在努力将结果集中的列映射到 class 属性.
我的Class定义为:
public class CourseUnit
{
public int unitId { get; set; }
public string description { get; set; }
}
存储过程被调用,如下面的清单草稿所示。存储过程结果集包含两列:
- UnitID
- 单位名称
我在将 UnitName 从存储过程映射到我的 class 的描述 属性 时遇到困难。该代码调用该过程并生成一个包含 UnitID 值但描述值为空的结果集。如何将 UnitName 映射到我的 class 的描述 属性?
public async Task<IEnumerable<CourseUnit>> GetCourseUnitsAsync(int courseId)
{
using (var connection = new NpgsqlConnection(_connectionString))
{
connection.Open();
Console.WriteLine($"About to execute the course units query for course {courseId}");
try
{
var p = new DynamicParameters();
p.Add("@course_id", courseId);
var result = await connection.QueryAsync<CourseUnit>(@"coursemanagement.select_course_units",
p,
commandType: CommandType.StoredProcedure);
Console.WriteLine($"Total results of course units are {result.AsList().Count}");
return result;
}
catch(Exception)
{
throw new KeyNotFoundException($"No Units found for course {courseId}");
}
throw new KeyNotFoundException();
}
}
我试过如下调用存储过程:
var result = await connection.QueryAsync<CourseUnit>(@"SELECT 'UnitID' as unitid, 'UnitName' as description FROM coursemanagement.select_course_units",
new { courseId });
Console.WriteLine($"Total results of course units are {result.AsList().Count}");
return result;
但是,我收到一个异常 =>
Exception encountered while retrieving course units 42P01: relation "coursemanagement.select_course_units" does not exist
看起来问题不在于列名,而在于您使用的 table 名称。在您的示例中,它假设数据库是 coursemanagement 并且 table 是 select_course_units?这是正确的吗,通常也可以在两者之间有一个模式,通常是 dbo 所以你可能需要使用 coursemanagement.dbo.select_course_units 或 coursemanagement.. select_course_units 从 table 中正确获取数据
我去更新存储过程以引入与 POCO class 属性.
同名的列别名我已将 link 添加到以下 Whosebug resource 中,详细说明了其他可能性:
- 此 post 建议使用 LINQ 的动态类型查询在结果集和 POCO 之间进行映射 class。
- 使用 Dapper 自定义 属性 键入详细的映射器 here
PostgreSQL 允许函数的结果集是 queried,例如
SELECT UnitID, UnitName from myStoredProc(parameterValue);
如果使用 PostgreSQL 那么可以使用存储过程中的 ExecuteReader class 到 运行 原始 SQL 代码到 select并准备了DataTable,但并不理想