Dapper>我无法使用子对象检索对象

时间:2015-05-16 17:49:43

标签: dapper

我有以下Quote和Share类:

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();

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

“不支持指定的方法”(???)

如果我通过以下方式修改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";

我收到以下错误:

“Footstock.Domain.Model.Quote实现”

我做错了什么?

1 个答案:

答案 0 :(得分:0)

对于Share类,您需要添加无参数构造函数或仅需要quantity参数的构造函数。

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