Dapper SQL调用返回null和0值
与Dapper一起进行SQL调用并将其映射到对象。 我正在使用SQL Dapper调用问题,而不是正确地映射到C#类。
使用ASP.NET Core API,并具有一个控制器和一个类。
Controller类
public class DapperController : Controller
{
private const string connectionString = @"";
[HttpGet("")]
public async Task<IActionResult> Index()
{
var sql = @"SELECT product_name, model_year,list_price
FROM [production].[products]";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
var product = await connection.QueryAsync<Products>(sql);
return Ok(product);
}
}
}
产品类
public class Products
{
[JsonPropertyName("product_name")]
public string ProductName { get; set; }
[JsonPropertyName("model_year")]
public int ModelYear { get; set; }
[JsonPropertyName("list_price")]
public double ListPrice { get; set; }
}
获取以下数据,但这是不正确的,因为数据库
[
{
"product_name": null,
"model_year": 0,
"list_price": 0
},
{
"product_name": null,
"model_year": 0,
"list_price": 0
}
]
使用上述数据时确实具有数据,而无需映射到类I获得正确的数据。不知道我在做什么错
[
{
"product_name": "Trek 820 - 2016",
"model_year": 2016,
"list_price": 379.99
},
{
"product_name": "Ritchey Timberwolf Frameset - 2016",
"model_year": 2016,
"list_price": 749.99
}
]
Working with Dapper on making SQL calls and mapping them to objects.
I am having an issue with SQL Dapper call not mapping correctly to C# Class.
Using ASP.NET Core API and have one controller and one class.
Controller Class
public class DapperController : Controller
{
private const string connectionString = @"";
[HttpGet("")]
public async Task<IActionResult> Index()
{
var sql = @"SELECT product_name, model_year,list_price
FROM [production].[products]";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
var product = await connection.QueryAsync<Products>(sql);
return Ok(product);
}
}
}
Products Class
public class Products
{
[JsonPropertyName("product_name")]
public string ProductName { get; set; }
[JsonPropertyName("model_year")]
public int ModelYear { get; set; }
[JsonPropertyName("list_price")]
public double ListPrice { get; set; }
}
Getting back the following data but that is not correct since the database does have data
[
{
"product_name": null,
"model_year": 0,
"list_price": 0
},
{
"product_name": null,
"model_year": 0,
"list_price": 0
}
]
When using the above without mapping to class I get the correct data. Not sure what I am doing wrong
[
{
"product_name": "Trek 820 - 2016",
"model_year": 2016,
"list_price": 379.99
},
{
"product_name": "Ritchey Timberwolf Frameset - 2016",
"model_year": 2016,
"list_price": 749.99
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jsonpropertyname
是用于json serialize而不是dapper orm的因为
@Yong Shun as Say修改脚本别名名称,该名称与您的C#属性(敏感)
我认为我们可能需要使用我们的客户作为此链接
columnAttribute
而不是jsonpropertyname
我们的客户映射需要实现
sqlmapper.itypemap
在这里也是链接 v2:[column] and [table] and [table]属性#722 dapper官方官员讨论列Dapper的映射属性。
Because
JsonPropertyName
is for Json serialize instead of Dapper ORMso that we might need to modify class property as below code or
@Yong Shun as say modify the script alias name which aligns with your c# property (case sensitive)
I think we might need to use our customer as this link
ColumnAttribute
instead ofJsonPropertyName
Our customer map needs to implement
SqlMapper.ITypeMap
Also here is a link V2: [Column] and [Table] Attributes #722 which dapper official discusses Column mapping attributes for dapper.