Dapper SQL调用返回null和0值

发布于 2025-01-27 01:23:10 字数 1517 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听不够的曲调 2025-02-03 01:23:10

不确定我在做什么错

jsonpropertyname是用于json serialize而不是dapper orm的

因为
@Yong Shun as Say修改脚本别名名称,该名称与您的C#属性(敏感)

public class Products
{
    public string product_name { get; set; }

    public int model_year { get; set; }

    public double list_price { get; set; }
}

我认为我们可能需要使用我们的客户作为此链接 columnAttribute 而不是jsonpropertyname

我们的客户映射需要实现sqlmapper.itypemap

public class Products
{
    [Column("product_name")]
    public string ProductName { get; set; }

    [Column("model_year")]
    public int ModelYear { get; set; }

    [Column("list_price")]
    public double ListPrice { get; set; }

}

在这里也是链接 v2:[column] and [table] and [table]属性#722 dapper官方官员讨论列Dapper的映射属性。

Not sure what I am doing wrong

Because JsonPropertyName is for Json serialize instead of Dapper ORM

so 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)

public class Products
{
    public string product_name { get; set; }

    public int model_year { get; set; }

    public double list_price { get; set; }
}

I think we might need to use our customer as this link ColumnAttribute instead of JsonPropertyName

Our customer map needs to implement SqlMapper.ITypeMap

public class Products
{
    [Column("product_name")]
    public string ProductName { get; set; }

    [Column("model_year")]
    public int ModelYear { get; set; }

    [Column("list_price")]
    public double ListPrice { get; set; }

}

Also here is a link V2: [Column] and [Table] Attributes #722 which dapper official discusses Column mapping attributes for dapper.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文