将存储过程结果映射到对象内的对象

时间:2015-02-11 20:37:01

标签: c# sql sql-server stored-procedures dapper

如果我有一个具有Dog类属性的Person类:

public class Person{
 public string Name {get;set;}
 public Dog Doggie {get;set;}
}

在单个SP调用中,我想填充狗对象和人物对象,但我无法弄清楚如何做到这一点。

CREATE PROCEDURE Test
 AS
BEGIN
    SET NOCOUNT ON;

    -- Insert statements for procedure here
      SELECT 
        Name                = p.Person_Name,
        Doggie.Name (This fails) = d.Dog_Name

      FROM My fancy join......
END
GO



  public Person GetByUid(Guid uid)
    {
        Person personToReturn;

        using (var connection = this.connectionFactory.GetOpenConnection())
        {
            try
            {
                personToReturn= connection.Query<Person>("ProcedureName",
                    new {  personUid = uid },
                    commandType: CommandType.StoredProcedure).Single();
            }
            catch ()
            {
                return null;
            }

        }

    return personToReturn;
}

有可能吗?

1 个答案:

答案 0 :(得分:2)

使用QueryMultiple,并在一个存储过程中编写2个SELECT语句。

以下是一些帮助链接。

Dapper.NET and stored proc with multiple result sets

Multiple SQL statements in one roundtrip using Dapper.NET