[英]Persisting complex objects with Dapper ORM
I have the following complex models setup on my Dapper ORM and it refuses to persist: 我在Dapper ORM上设置了以下复杂模型并拒绝持久化:
const string sqlQuery = "INSERT INTO Reports (created_at, post_name) VALUES (@CreatedAt, @PostData.PostName)";
_connection.Execute(sqlQuery, report);
namespace Foobar.Models
{
public class Report
{
public int Id { get; set; }
public DateTime CreatedAt { get; set; }
public PostData PostData { get; set; }
}
public class PostData
{
public string title { get; set; }
public double index { get; set; }
}
}
This is the error i'm getting: 这是我得到的错误:
The member PostData of type Foobar.Models.PostData cannot be used as a parameter value
Try using an anonymous object: 尝试使用匿名对象:
const string sqlQuery =
"INSERT INTO Reports (created_at, post_name) VALUES (@CreatedAt, @PostName)";
_connection.Execute(sqlQuery, new { CreatedAt = report.CreatedAt,
PostName = report.PostData.PostName });
The problem with what you have is that you are effectively trying to pass the PostData
object as a value for an SQL parameter called PostData.PostName
. 您所拥有的问题是您有效地尝试将PostData
对象作为名为PostData.PostName
的SQL参数的值PostData.PostName
。
Bear in mind that the "@parameter"
syntax is raw SQL, not a feature of Dapper itself. 请记住, "@parameter"
语法是原始SQL,而不是Dapper本身的功能。 There's no stage of the mapping process that involves interpreting the SQL parameter name as an expression against the object that you pass. 映射过程没有阶段涉及将SQL参数名称解释为针对您传递的对象的表达式。 The mapping works by simply matching a parameter name to a property name and injecting the corresponding value from the object you pass. 映射通过简单地将参数名称与属性名称匹配并从您传递的对象中注入相应的值来工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.