Dapper > 我无法检索带有子对象的对象

Dapper > I cannot retrieve object with child object

我有以下报价和分享类:

public class Quote
{
    public Quote(string ticker, string name, decimal lastPrice)
    {
        this.Ticker = ticker;
        this.Name = name;
        this.LastPrice = lastPrice;
    }

    public string Ticker { get; private set; }

    public string Name { get; private set; }

    public decimal LastPrice { get; private set; }
}

public class Share
{
    public Share(Quote quote, int quantity)
    {
        this.Quote = quote;
        this.Quantity = quantity;
    }

    public Quote Quote { get; private set; }
    public int Quantity { get; private set; }
}

还有这些表:

CREATE TABLE [dbo].[quotes](
[ticker] [char](5) NOT NULL,
[name] [varchar](60) NOT NULL,
[last_price] [money] NOT NULL,
[bid_price] [money] NULL,
[bid_quantity] [int] NULL,
[ask_price] [money] NULL,
[ask_quantity] [int] NULL,
[high] [money] NULL,
[low] [money] NULL,
[previous_close] [money] NULL,
[created_by] [varchar](12) NULL,
[created_date] [datetime] NULL,
[modified_by] [varchar](12) NULL,
[modified_date] [datetime] NULL,
CONSTRAINT [PK_quotes] PRIMARY KEY CLUSTERED 
(
[ticker] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  =     ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[shares](
[ticker] [char](5) NOT NULL,
[broker_id] [char](12) NOT NULL,
[quantity] [int] NOT NULL,
[created_by] [varchar](12) NOT NULL,
[created_date] [datetime] NOT NULL,
[modified_by] [varchar](12) NULL,
[modified_date] [datetime] NULL,
CONSTRAINT [PK_shares] PRIMARY KEY CLUSTERED 
(
[ticker] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,            IGNORE_DUP_KEY        =     OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)     ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[shares]  WITH CHECK ADD  CONSTRAINT [FK_shares_quotes]     FOREIGN KEY([ticker])
REFERENCES [dbo].[quotes] ([ticker])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[shares] CHECK CONSTRAINT [FK_shares_quotes]
GO

最后是这段代码:

        string sql = "SELECT quantity, s.ticker, name, last_price FROM shares s " +
                     "INNER JOIN quotes q ON q.ticker = s.ticker " +
                     "WHERE s.ticker=@ticker AND broker_id = @broker_Id";

        IEnumerable<Share> shares = connection.Query<Share, Quote, Share>(sql,
                                        (s, q) =>
                                        {
                                            Share share = new Share(q, s.Quantity);
                                            return share;
                                        },
                                        new { ticker = ticker, broker_Id = brokerId }
                                        , splitOn: "ticker");

        return shares.FirstOrDefault();

当我执行该查询时,出现以下错误:

"Specified method is not supported" (???)

如果我通过以下方式修改 sql 语句(使用 "last_price as lastPrice"):

string sql = "SELECT quantity, s.ticker, name, last_price as lastPrice FROM shares s " +
                     "INNER JOIN quotes q ON q.ticker = s.ticker " +
                     "WHERE s.ticker=@ticker AND broker_id = @broker_Id";

我收到以下错误:

"A parameterless default constructor or one matching signature (System.String ticker, System.String name, System.Decimal lastPrice) is required for Footstock.Domain.Model.Quote materialization"

我做错了什么?

对于 Share class,您需要添加一个无参数构造函数或只接受一个 quantity 参数的构造函数。

在 Dapper 调用组成要返回的结果的方法之前,它需要构建参数以传递给 (s, q) => ... 在您的情况下,您需要一个 Share 和一个 Quote class,两者都必须有一个可以使用Dapper拥有的数据调用的构造函数。对于 Quote,构造函数匹配查询中的属性。对于 Share,Dapper 只有 quantity,还没有 Quote 对象。